Computer Science Canada passing argument 1 of ?find? from incompatible pointer type |
Author: | DtY [ 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:
I'm getting the warning from both lines that call that function. |
Author: | chrisbrown [ 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. |
Author: | DtY [ 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? |
Author: | chrisbrown [ 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:
|
Author: | DtY [ 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? |
Author: | chrisbrown [ 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:
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] |
Author: | DtY [ 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? |
Author: | chrisbrown [ 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. |
Author: | DtY [ 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 |