1

Here is my code:

void doSomething(){
    char arr[5][10];
    arr[1] = "Bob";
    arr[2] = "Steve";
    arr[3] = "Tim";
    arr[4] = "Ruth";
    arr[5] = "Heather";
    init(arr);

void init(char *array){
    int i;
    newArr[5][10];
    for(i=0; i<5; i++){
        newArr[i] = *(array + i);
    }
}

I keep getting an error saying:

warning: passing argument 1 of ‘init’ from incompatible pointer type [enabled by default] note: expected ‘char ’ but argument is of type ‘char ()[10]’

1
  • 7
    Your compiler is telling you the truth. You have declared array to be of type char * but you are using it as if it were declared to be of type char *[]. Commented Oct 24, 2012 at 19:45

3 Answers 3

4

Your data is a 2-dimensional array, so your function needs to take an array of pointers account for this (i.e. char (*array)[10] or char array[][10]).

Also, in your init function, you can't just copy the strings in to arrays, you either need to copy all the data (as strings with a strcpy or character by character with a second loop) or just copy the pointers to the strings (so make your newArr variable a char *newArr[5]).

If none of this makes any sense, then you should probably brush up on your C Pointer knowledge by reading through the C FAQ on this topic.

Sign up to request clarification or add additional context in comments.

8 Comments

@AdamRosenfield I never said that an array is a pointer. If you want to pass an array to a function, you list the argument as a pointer, and your passed in array decays into a pointer.
An array decays to a pointer, but only at the first level. A 2D array does not decay into a pointer to a pointer: it decays into a pointer to an array. So char [5][10] decays into char (*)[10], but it does not decay to char**.
@texasbruce Actually after thinking about it, he is right. I agree that he's being dumb for downvoting everything though. Maybe I'll change my answer to be more fitting.
I downvoted answers because they are wrong. You cannot pass a char [5][10] to a function taking char **. Try it. You get exactly the compiler warning from OP. And if you try to run it despite the warning, it will crash and not do what you want for the fundamental reason that arrays are not pointers. I cannot stress that enough. See the C FAQ.
@NiklasR You get the same warning as in the original post: warning: passing argument 1 of ‘test’ from incompatible pointer type That site might not show warnings.
|
2

1) There is no character array assignment in C use strcpy() and arrays start at 0 not 1:

#include <string.h>
void doSomething(){
    char arr[5][10];
    strcpy(arr[0], "Bob");
    strcpy(arr[1], "Steve");
    strcpy(arr[2], "Tim");
    strcpy(arr[3], "Ruth");
    strcpy(arr[4], "Heather");
    init(arr);
}

2) init() takes a pointer to char arrays; newArr[][] is not declared, add char. Add a declaration for init() at the beginning of the source. And last but not least: again replace assignment with strcpy().

void init(char (*array)[10]);

void doSomething() {...}

void init(char (*array)[10]){
    int i;
    char newArr[5][10];
    for(i=0; i<5; i++){
        strcpy(newArr[i], array[i]);
    }
}

Finally, it might be boring, but take a look at https://stackoverflow.com/tags/c/info and the C FAQ and pick up a book. This will serve you far better and longer than I can.

1 Comment

thanks for the help, I am just learning C and struggling a bit. As far as arrays starting at 0 I was aware of this and not sure why I started at 1 could have been that I was in a rush. Thanks for the help again.
-1

That's not an error. Its a a warning. As you can see your arr in doSomething is a double array (which can be represented using a double pointer) but in init you only take a single pointer. Change that to either a double pointer **array or array[][]*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.