I've been given function called statPrint to handle printing of the system call stat(). The function is provided with another .o file. I'm getting errors when compiling my implementation with that function:
In function ‘main’:
statcall.c:9:19: error: expected expression before ‘,’ token
statPrint(argv[1]*,sb*);
^
statcall.c:9:19: error: incompatible type for argument 2 of ‘statPrint’
statcall.c:4:8: note: expected ‘struct stat *’ but argument is of type ‘struct stat’
extern statPrint(char*,struct stat*);
Here is my code:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
extern statPrint(char∗,struct stat∗);
int main(int argc, char *argv[])
{
struct stat sb;
stat(argv[1],&sb); ///argv[1] contains input from the terminal/shell
statPrint(argv[1]*,sb*);
}
I compile it with(libstat contains the external function):
gcc -o statcall statcall.c libstat.o
How do I get rid of the errors?
This line makes no sense:
statPrint(argv[1]*,sb*);
There's no valid syntax that ends with *.
I think you want:
statPrint(argv[1], &sb);
Recommend you read up on addresses of variables and pointers.
Your function expects char * please provide it
statPrint(argv[1],sb);
I really didn't get what is argv[1]*
Related
This topic ought to have been flogged to death. I just spent 30 minutes locating what ended up being a missing semicolon at the end of a function prototype in a header file:
void foo(void);
void bar(void) // <<< Error on this line
void squee(void);
This is a common typo caused by copy-pasting the prototype from the C file. Of course according to the compiler the universe just fell apart, with an endless stream of absolutely nonsensical errors, none of them helpful.
This could be avoided by having an optional parsing phase to check for this condition in .h files then report a warning (promoted to error if settings mandate). This would require some restrictions on what you put in header files (no code, consistent format for prototypes, etc). But that's an easy compromise.
I can write my own SW tool to do this, but it would be more helpful to run it as a part of the build process. I use GCC in Eclipse. Any advice on where you'd start with this? Or anything pre-existing / off the shelf? Or perhaps just a better way to approach it?
Thank you.
it's far more common and more difficult to guess the following problem (in a header file):
struct something {
type1 var1;
type2 var2;
}
/* EOF */
and when you #include "header.h" into hello.c
#include <stdio.h>
#include "header.h"
int main(int argc, char **argv)
{
printf("Hello, world\n");
}
you get an error e.g.
lcu#europa:~$ gcc main.c
main.c:4:1: error: expected ';', identifier or '(' before 'int'
4 | int main(int argc, char **argv)
| ^~~
and the compiler has got out of header.h to signal the error in the line of main function. The thing can be worse if you happen to use legacy code and declare main() the old way:
main(argc, argv)
char **argv;
{
...
because then, the struct is a valid type and it is taken as the type returned by main() and you get (if you get it) the error far below (or no error at all, just a warning):
lcu#europa:~$ gcc main.c
main.c: In function 'main':
main.c:4:1: warning: type of 'argc' defaults to 'int' [-Wimplicit-int]
4 | main(argc, argv)
| ^~~~
In this case, the contents of main.c were:
#include <stdio.h>
#include "header.h"
main(argc, argv)
char **argv;
{
printf("Hello, world\n");
}
which is still valid c code.
(these examples were made by gcc, because clang ---the native compiler of freebsd--- detects the EOF in the header file and shows a warning stating that the type was not ended before the end of the include file. But this only happens if the type definition is the last of the file.
Note:
if you declare main as:
#include <stdio.h>
#include "header.h"
main(argc, argv)
int argc;
char **argv;
{
printf("Hello, world\n");
}
you get a complete compilation, without even a warning.
I am supposed to take in arguments from the command line, to build a sample Unix-style 'ls' style program that lists the directory contents.
I have pre-built functions given to me and I have to separate the code down into modularized header and c files for each individual function and create a makefile.
The makefile will run and gives these warnings:
-bash-3.2$ make run
gcc -c main.c
main.c: In function ‘main’:
main.c:18: warning: passing argument 1 of ‘do_ls’ from incompatible pointer type
Here is do_ls.h:
'''
#ifndef DO_LS
#define DO_LS
void do_ls( char*[] );
#endif
'''
Errors:
-bash-3.2$ gcc main.c
main.c: In function ‘main’:
main.c:18: warning: passing argument 1 of ‘do_ls’ from incompatible pointer type
main.c:23: warning: passing argument 1 of ‘do_ls’ from incompatible pointer type
/tmp/cc8Q7153.o: In function `main':
main.c:(.text+0x1b): undefined reference to `do_ls'
main.c:(.text+0x47): undefined reference to `do_ls'
collect2: ld returned 1 exit status
main:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include "do_ls.h"
int main(int ac, char *av[])
{
if ( ac == 1 )
do_ls(".");
else
while ( --ac ){
printf("%s:\n", *++av );
do_ls( *av );
}
}
The do_ls function expects an array of char *, but when you call it you only pass in a single char *. This is what the warning in the initial call to make is complaining about. This argument mismatch invokes undefined behavior.
Try calling it like this:
if ( ac == 1 ) {
char *args[] = { ".", NULL };
do_ls(args);
} else {
do_ls(av+1);
}
I'm stuck with what seems a beginner's compilation error:
My simple program:
#include <stdlib.h>
#include <stdio.h>
#include "Tiles_Circular_Linked_List.h"
#define MAX_LINE_LENGTH 128;
int main(int argc, char **argv) {
struct node *head_tail;
FILE *file;
/*char filename[] = "/home/student/Desktop/Studies/C/testing_fodder/tiles";*/
argv++; /*go to second character-array argument*/
file = fopen(*argv, "r");
char *curr_line;
fgets(curr_line, MAX_LINE_LENGTH, file);
return 0;
}
I try to compile it using this command:
gcc -g -Wall -ansi launch_tiles.c -o tiles_prog
and get these errors:
launch_tiles.c: In function ‘main’:
launch_tiles.c:17:19: error: expected ‘)’ before ‘;’ token
launch_tiles.c:17:19: error: too few arguments to function ‘fgets’ /usr/include/stdio.h:628:14: note: declared here
launch_tiles.c:9:8: warning: variable ‘file’ set but not used [-Wunused-but-set-variable]
launch_tiles.c:8:15: warning: unused variable ‘head_tail’ [-Wunused-variable]
I am interested about the errors, not the warnings.
I can count three arguments that I pass to fgets and don't understand where do I miss parentheses so what's the problem?
Thanks!
change
#define MAX_LINE_LENGTH 128;
to
#define MAX_LINE_LENGTH 128
(without the ;). Easy mistake to make.
The C preprocessor does very simple, textual substitution, so with the wrongly-defined macro, you end up with
fgets(curr_line, 128;, file);
which is obviously a syntax error.
The following is my file named crack.c:
#include <stdio.h>
#define _XOPEN_SOURCE
#include <unistd.h>
void execute(char *alpha)
{
char *beta = crypt(alpha);
printf("%s", beta);
}
int main(int argc, string argv[]){
....
execute(argv[1]);
else{
printf("You submitted %d command line arguments. That's an issue. You need to submit exactly one.", argc);
return 1;
}
}
The following is what I type into the command line:
jharvard#appliance (~/Dropbox/hacker2): clang -o crack -lcrypt crack.c
The following is what the command line spits back out at me:
crack.c:8:19: warning: implicit declaration of function 'crypt' is
invalid in
C99 [-Wimplicit-function-declaration]
string beta = crypt(alpha);
^ crack.c:8:12: warning: incompatible integer to pointer conversion initializing
'string' (aka 'char *') with an expression of type 'int'
[-Wint-conversion]
string beta = crypt(alpha);
^ ~~~~~~~~~~~~ 2 warnings generated.
Anyone know what's going on?
I had the same problem, and I realized that changing the header
#define _XOPEN_SOURCE
#include <unistd.h>
by
#define _GNU_SOURCE
#include <crypt.h>
makes disapear the error in compilation time
The function signature of crypt is:
char * crypt (const char *key, const char *salt)
It seems that you forgot one parameter! So your line:
string beta = crypt(alpha);
Should be something like that:
string beta = crypt(alpha, salt);
Make sure you have the define and include for crypt as the very first lines at the top of your file before any other includes.
#define _XOPEN_SOURCE
#include <unistd.h>
I have a C header file which defines the following function:
void my_func(pthread_t tid);
This is defined by another function:
void my_func(pthread_t tid) {
...
When I compile, it says:
****.h:2: error: expected specifier-qualifier-list before ‘pthread_t’
Any ideas what I'm doing wrong?
You need to #include <pthread.h> in the header file so that pthread_t is in scope for the my_func() prototype.
Without the #include the compiler does not recognize pthread_t as a type, but it needs a type before the argument.
The error expected specifier-qualifier-list before ‘pthread_t’ is saying just that. You need a type (specifier-qualifier-list) before a parameter (‘pthread_t’).