开发者

Conflicting types Warning when passing String to function in C

I haven't programmed in C for awhile and having an issue with passing a string to a function. The code works however I get warnings from gcc.

I call the function in my main with:

copyToCode(code, section1, section2);

The function is:

void copyToCode (char **code, char *loc, char *data ){}

I get "conflicting types for copyToCode" on the line containing the function and "previous implicit declaration of copyToCode was here" warning on the line calling the function.

I have declared the variables:

char *code = malloc (32*1000* sizeof(char));

char *section1 = malloc(8*sizeof(char)), *section2 = malloc(8*sizeof(char));

I also tried this : char *section1[8];

As a side question - which is correct?

The section1 and section2 are meant to be Strings, and the c开发者_Python百科ode is meant to be an array of strings.

Thanks for reading, I appreciate any help. Gareth


You need to declare the function before you call it, otherwise the compiler will try to work out what the function prototype is on your behalf.

The message

previous implicit declaration of copyToCode

is telling you this. An implicit declaration is one that the compiler makes because you haven't yet given it an explicit declaration.


In your update to the question you say that code is intended to be an array of strings but you define it as:

char *code = malloc (32*1000* sizeof(char));

That allocates a single string. An array of strings would be held in a char**, just like argv. You would need to allocate the array first, which would contain n strings, each being a char*. Then you'd have to allocate each char* one by one in a loop.

This sort of coding is so much easier in C++ with the standard library string and vector classes.


This warning means that you have your declaration differs from the implementations. Or you have two different declarations of the function.

Because the warning is mentioning an implicit declaration it means that the declaration was deduced by gcc because you used the function before declaring it. GCC will use the parameters of the function call to deduce the declaration, which can lead to nasty problems.

If you declare the function, you will probably still get an error, but it should be much more specific.

Side note: If you are working with gcc, always compile as gcc -std=c99 -pedantic -Wall -Wextra -Wwrite-strings source.c

To your edit: Your variables are wrong. code is char * but your function takes char **.


Can you give us more detail about the variables you are using as parameters of the function?

Also worth to note is the fact that the first parameter of copyToCode is a pointer to a pointer of chars, what can be used as an array of strings.

The line:

void copyToCode (char **code, char *loc, char *data ){}

is the declaration of your function? In that case, you should write it as this:

void copyToCode (char **code, char *loc, char *data );


To define an array of strings, you'd need something like the following:

char **code;
int i;
code = malloc (32*sizeof(*code));
for (i=0; i<32; i++) {
  code[i]=malloc(1000*sizeof(**code));
}

Note that this doesn't include error checking on the malloc results, which you should do. And it creates an array of size 32 containing strings of size 1000, which you shouldn't be hard coding.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜