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 code 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
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
codeis intended to be an array of strings but you define it as:That allocates a single string. An array of strings would be held in a
char**, just likeargv. You would need to allocate the array first, which would containnstrings, each being achar*. Then you’d have to allocate eachchar*one by one in a loop.This sort of coding is so much easier in C++ with the standard library string and vector classes.