1

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

gskspurs
  • 186
  • 1
  • 11

4 Answers4

3

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I fixed the code by adding `void copyToCode (char **code, char *loc, char *data );` above the main function. Thanks! Was a stupid mistake! – gskspurs Apr 27 '11 at 13:14
  • @gskspurs That won't really fix the problem, but it is a step in the right direction. – Šimon Tóth Apr 27 '11 at 13:16
0

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 );
Nicolás Ozimica
  • 9,481
  • 5
  • 38
  • 51
0

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 **.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • Then how exactly do I produce an array of characters, as code, which I want? – gskspurs Apr 27 '11 at 13:18
  • @gskspurs What do you want? You didn't specify that. You didn't specify why is the function taking a pointer to pointer. It's impossible to answer with the amount of information you are giving. – Šimon Tóth Apr 27 '11 at 13:21
  • Sorry, I'll try and explain a bit better: I would like the variable `code` to be a large array of characters of length 32. How do I declare this? Currently I have: `char *code = malloc (32*1000* sizeof(char));` is something such as char `*code[32]` correct? – gskspurs Apr 27 '11 at 13:26
  • @gskspurs You mean array of arrays of length 32? Then `char **` is correct, but your `malloc` isn't. – Šimon Tóth Apr 27 '11 at 13:27
0

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.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 1
    Great, thanks, I actually wanted to achieve an array of 1000 strings of size 32. So I simply change them values to something like: `code char **code; int i; code = malloc (1000*sizeof(*code)); for (i=0; i<1000; i++) { code[i]=malloc(32*sizeof(**code)); }` – gskspurs Apr 27 '11 at 13:27
  • That's it! But C++ string and vector etc. is really so much easier – David Heffernan Apr 27 '11 at 14:53
  • Perhaps, but the question is for `C`, not `C++`. – BMitch Apr 27 '11 at 18:58