Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 passing argument 1 of ?find? from incompatible pointer type
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DtY




PostPosted: Fri Feb 26, 2010 8:25 pm   Post subject: passing argument 1 of ?find? from incompatible pointer type

I'm getting this warning from from my compiler, but as far as I can tell, all is right. When I run the program it segfaults.

Here are the four important lines:
c:
int find(char **arr, char *se) {
/***/
char names[10][15];
/***/
thisId = find(names, thisName);
whoId = find(names, who);


I'm getting the warning from both lines that call that function.
Sponsor
Sponsor
Sponsor
sponsor
chrisbrown




PostPosted: Sat Feb 27, 2010 2:03 am   Post subject: RE:passing argument 1 of ?find? from incompatible pointer type

You can only have one dynamic length when passing multidimensional arrays in C. I'm wondering what you're trying to do, though. If names a one-dimensional array of strings (of max size 14?), change char names[10][15] to *names[10]. If it's a 2D array of chars, you could change **arr to *arr[15], which is the type of names.
DtY




PostPosted: Sat Feb 27, 2010 11:59 am   Post subject: RE:passing argument 1 of ?find? from incompatible pointer type

Changing the argument to char arr[10][15] worked, thanks.

Why can't you pass a multidemsnional array with an unknown size? Isn't it just an array of pointers?
chrisbrown




PostPosted: Sat Feb 27, 2010 1:03 pm   Post subject: Re: passing argument 1 of ?find? from incompatible pointer type

Not the way you've defined it. When you use the var[x][y] notation, the compiler allocates x * y space in one contiguous block, and treats it as an array of pointers to arrays of size y.

If you used *var[x] instead, it would allocate x space and you could assign each element to point to some other array, but you would have to keep track of the size of each. You could, however, still call var[i][j] and get a meaningful value as long as you are within your array bounds.

Here's an example of the difference:
c:
#include <stdio.h>

int main() {
       
        int i[3][3];
       
        int *j[3];
        int a[3], b[3], c[3];
        j[0] = a;
        j[1] = b;
        j[2] = c;
       
        // i and j work in the same way:
        i[0][0] = 111;
        j[0][0] = 222;
        printf("i[0][0] = %d\nj[0][0] = %d\n", i[0][0], j[0][0]); // Prints 111 and 222
       
       
        // Except:
       
        i[1][0] = 333;
        printf("i[0][3] = %d\n", i[0][3]);   // Prints 333
        // So i[0][3] = i[1][0]
       
        j[1][0] = 444;
        printf("j[0][3] = %d\n", j[0][3]);   // Does NOT print 444
        // So j[0][3] != j[1][0]
       
        return 0;
}
DtY




PostPosted: Sat Feb 27, 2010 2:09 pm   Post subject: RE:passing argument 1 of ?find? from incompatible pointer type

So when I create an array [5][3], it does not create an array with five pointers, pointing to an array with a length three, it creates a single array with size 5*3?

(If I'm understanding that right)

Is there any way to "trick" the compiler, so if I were to malloc(5*3*sizeof(int)), I could access it as [a][b], instead of [5*a+b]?

[edit] Is this part of Ansi C, or is it an optimization that compilers tend to do?
chrisbrown




PostPosted: Sat Feb 27, 2010 4:05 pm   Post subject: Re: RE:passing argument 1 of ?find? from incompatible pointer type

Quote:
So when I create an array [5][3], it does not create an array with five pointers, pointing to an array with a length three, it creates a single array with size 5*3?

Yes, as far as I understand it. I believe the compiler basically does you a favour by doing the math for you when using more than one dimension.

Quote:
Is there any way to "trick" the compiler, so if I were to malloc(5*3*sizeof(int)), I could access it as [a][b], instead of [5*a+b]?

Well, it's not pretty, but you could cast it to an array of second dimension size 3:
c:
int **a = malloc(5*3*sizeof(int));
( (int(*)[3]) a ) [i][j] = 123;

// You can cast to any size and/or any dimension. C will do the math but it won't check bounds for you so it's not the best solution.
( (int(*)[2][3][4]) a ) [i][j][x][y] = 123;


Quote:
Is this part of Ansi C, or is it an optimization that compilers tend to do?

I'm fairly sure it's standard. IIRC, the [] notation allocates on the stack, while * indicates heap allocation (through malloc'ing).[/quote]
DtY




PostPosted: Sat Feb 27, 2010 4:29 pm   Post subject: RE:passing argument 1 of ?find? from incompatible pointer type

So is all memory that's not malloc()ed stored on the stack? Is that why it's suggested to create all arrays that are bigger than 1kB with malloc()?

Btw, the heap is just memory, right, and 'the stack' is referring to the call stack?
chrisbrown




PostPosted: Sat Feb 27, 2010 4:35 pm   Post subject: RE:passing argument 1 of ?find? from incompatible pointer type

Yes and yes. That's also why you don't need to free anything on the stack, that space is dynamically deallocated when the function returns.
Sponsor
Sponsor
Sponsor
sponsor
DtY




PostPosted: Sat Feb 27, 2010 4:59 pm   Post subject: RE:passing argument 1 of ?find? from incompatible pointer type

Thanks for all the help, it's appreciated
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: