I keep getting error when using get_string CS50 - c

when i am using 'get_string' i am recieveing this error message. i have included <cs50.h> ! please could anyone helpas i cant figure out what I'm doing wrong
adability.c -lcrypt -lcs50 -lm -o readability
readability.c:9:19: error: initializer element is not a compile-time constant
string text = get_string ("Text: ") ;
^~~~~~~~~~~~~~~~~~~~~
/usr/include/cs50.h:109:25: note: expanded from macro 'get_string'
#define get_string(...) get_string(NULL, __VA_ARGS__)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Related

C compiler error: expected parameter declarator

https://github.com/noyesno/awka
For the above repo, I got the following error when I try to compile it on macOS (gcc is just clang). I have no idea how to fix the problem according to the error message. It compiles fine on Linux.
I also tried the real gcc from Homebrew to compile the package. It also show the same error. How can I fix this problem on macOS?
$ ./configure
$ make
...
gcc -O -Dawka_LIBDIR=\"/usr/local/lib\" -Dawka_INCDIR=\"/usr/local/include\" -c -o print.o print.c
print.c:52:11: error: expected parameter declarator
int PROTO(sprintf, (char *, const char *,...)) ;
^
print.c:52:11: error: expected ')'
print.c:52:11: note: to match this '('
print.c:52:11: error: conflicting types for '__builtin___sprintf_chk'
int PROTO(sprintf, (char *, const char *,...)) ;
^
print.c:52:11: note: '__builtin___sprintf_chk' is a builtin with type 'int (char *, int, unsigned long, const char *, ...)'
3 errors generated.
make[1]: *** [<builtin>: print.o] Error 1
make[1]: Leaving directory '/private/tmp/awka/awka'
make: *** [Makefile:48: awka_exe] Error 2
I'm not going to spend ages on this, but it looks as though configure is gripping stdio.h looking for sprintf. It is unable to find it in the header, and so it adds the #define:
NO_SPRINTF_IN_STDIO
which it sets to 1, and uses it to add its own prototype for sprintf. Unfortunately, this appears to be a macro in this case, which replaces sprintf with '__builtin___sprintf_chk' instead (which has additional string length checks by the looks of it).
Possible solutions:
Comment out the line in print.c, and make sure stdio.h is included somewhere.
After running configure, search for where it defines NO_SPRINTF_IN_STDIO and set that var to 1?
Fix the configure to so a more rigorous test?

Error: ‘AES_BLOCK_SIZE’ undeclared. Unable to compile OpenSSL with C in Linux

I have installed latest version of OpenSSL . I just try to compile and run the program OpenSSL_aes.
While compiling with gcc -Wall openssl_aes.c -lcrypto I got following error. I tried my best solve this problem , but unable to solve this compiler error.
openssl_aes.c: In function ‘aes_encrypt’:
openssl_aes.c:51:22: error: ‘AES_BLOCK_SIZE’ undeclared (first use in this function)
int c_len = *len + AES_BLOCK_SIZE, f_len = 0;
^
openssl_aes.c:51:22: note: each undeclared identifier is reported only once for each function it appears in
openssl_aes.c: In function ‘aes_decrypt’:
openssl_aes.c:75:45: error: ‘AES_BLOCK_SIZE’ undeclared (first use in this function)
unsigned char *plaintext = malloc(p_len + AES_BLOCK_SIZE);
^
Edit :
while I add #include<openssl/aes.h> as per # martin, the compilation problem is solved.
now, gcc -Wall openssl_aes.c -lcrypto is successfully compiled.
But, when I try to run the program ( to run i used - ./a.out ), I got following error
Segmentation fault (core dumped)
Can anyone help me to solve this and run my program? I just want to perform simple aes encryption/decryption using OpenSSL. I am using GCC under Fedora 19 .
Thanks in advance.
You are probably not including openssl/aes.h, as AES_BLOCK_SIZE is defined in there as: #define AES_BLOCK_SIZE 16. So make sure you have:
#include <openssl/aes.h>
in your file.

Why do I get "Invalid conversion from "void *" to "int**" error?

my code shows this warnings when compiling with "g++ -Wall -pedantic -Wno-long-long -c main.c". I have to compile in this mode, becouse its a homework and we have an application that corrects them and it uses this compile mode.
Error: invalid conversion from "void" to "int** " [-fpermissive]
Error: invalid conversion from "void" to "int* " [-fpermissive]
Error: invalid conversion from "void" to "main(int, char*)::VYSLEDEK" [-fpermissive]
the same errors continue as i realloc quite a lot in my program. I tried to change almost everything in that realloc, it is still the same.
Parts of the code :
struct VYSLEDEK
{
int sirka;
int vyska;
int zacatek_x;
int zacatek_y;
int soucet;
} *vysledek;
int **matice,**soucty;
.....
matice=(int**)malloc(1*sizeof(int*));
matice[0]=(int*)malloc(1*sizeof(int));
soucty=(int**)malloc(1*sizeof(int*));
soucty[0]=(int*)malloc(1*sizeof(int));
.....
1. matice=realloc(matice,naalokovano*2*sizeof(int*));
2. soucty=realloc(soucty,naalokovano*2*sizeof(int*));
.....
for (i=0;i<(naalokovano*2);i++)
{
3. matice[i]=realloc(matice[i],sizeof(int));
4. soucty[i]=realloc(soucty[i],sizeof(int*));
};
.....
5. vysledek=realloc(vysledek,vysledku*sizeof(struct VYSLEDEK*));
Thank you for your help.
You already did cast the result of malloc to the right type. Do the same for the realloc calls, too.
BTW: Don’t complain that they force you to switch on the warnings. I think it’s the most sensible thing to do by default, I always use at least -Wall -Werror.

Error when connecting to Postgres database in C - using libpq-fe.h

Hey I am trying to connect to a database using postgres
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
int main(int argc, char* argv[])
{
//Start connection
PGconn* connection = PQconnectdb("host=webcourse.cs.nuim.ie dbname=cs621 sslmode=require user=ggales password=1234");
if (PQstatus(connection) ==CONNECTION_BAD)
{
printf("Connection error\n");
PQfinish(connection);
return -1; //Execution of the program will stop here
}
printf("Connection ok\n");
//End connection
PQfinish(connection);
printf("Disconnected\n");
return 0;
}
And I keep getting this compile error:
main.c: In function ‘main’:
main.c:9:35: warning: missing terminating " character [enabled by default]
main.c:9:2: error: missing terminating " character
main.c:10:2: error: ‘dbname’ undeclared (first use in this function)
main.c:10:2: note: each undeclared identifier is reported only once for each function it appears in
main.c:10:9: error: ‘cs621’ undeclared (first use in this function)
main.c:10:15: error: expected ‘)’ before ‘sslmode’
main.c:10:56: warning: missing terminating " character [enabled by default]
main.c:10:15: error: missing terminating " character
main.c:16:1: error: expected ‘,’ or ‘;’ before ‘}’ token
main.c:16:1: error: expected declaration or statement at end of input
Can anyone see why this is happening?
Thanks.
Your code compiles just fine. If I paste it into x.c I can compile it with no problems:
gcc -I /usr/pgsql-9.2/include -L /usr/pgsql-9.2/lib x.c -lpq
(paths may differ on your system).
you may use the 64-bit libpq.lib in a 32-bit program.
you can use a 32-bit libpq.lib or change you platform to x64.
a 32-bit client + 64-bit server can not work well.

Linking a library in g++ doesn't work

I am trying to compile a .cpp-file which uses a matrix-library. The library-files libnewmat.a and libnewmat.so are in the path /usr/lib64 . The include-files are in path /usr/include/newmat , so I tried (several ways) to compile i.e. with:
g++ -I/usr/include -L/usr/lib64 -lnewmat new.cpp -o new3
but the compiler doesn't find the library. The content of the .cpp is:
#include <iostream>
#include <newmat/newmat.h>
#include <newmat/newmatio.h>
using namespace std;
int main()
{
Matrix A(2,2);
Real b[] = {1,2,3,4};
A << b;
cout << A << endl;
return 0;
}
The compiler says:
test.cpp: In function ‘int main()’:
test.cpp:9: error: ‘Matrix’ was not declared in this scope
test.cpp:9: error: expected ‘;’ before ‘A’
test.cpp:10: error: ‘Real’ was not declared in this scope
test.cpp:10: error: expected ‘;’ before ‘b’
test.cpp:11: error: ‘A’ was not declared in this scope
test.cpp:11: error: ‘b’ was not declared in
this scope
Could You provide me with the correct c++ code, or the correct command line instruction?
Thanks, Kepler
If you recently installed this library yourself you probably need to run sudo ldconfig to load the so into the linker cache.
EDIT: As Kevin said not a linking error that you're getting.
Perhaps it's a name space issue?
using namespace NEWMAT;
according to this: http://www.robertnz.net/nm10.htm#namesp
This isn't a library problem - it's a compiler problem - it can't find any definition for Matrix (probably in your include files, but we can't determine that with the information given)
[edit]
Ascertain if your classes in the include files are being referenced correctly
[/edit]

Resources