Double pointers and two dimensional arrays in C
If a function takes a double pointer like this:
find(int **array){.....} to call this function I have Code:
int height = 3; How can I fix this? I'm little confused about the two dimenstional array and double pointers. Thank you, James |
try making your array like
Code:
int **array=new (int *)[width] |
thanks but i get an error at this line
int **array=new (int *)[width]; |
try making width and height const. What the errors are is more important than just the line number. Post them!
|
tuxdev please look at the title for this thread. C doesn't have the new operator, this was introduced in C++.
|
so i also tried int **array=(int *)[width]; but it still doesn't work.
|
and the error i get is " syntax error before '[' token"
|
Quote:
I havn't post the answer yet,because im trying to figure it myself, its a long time since i used C. |
I haven't tried to compile this, but please correct me if I'm wrong. Your assignment is not doing what you think.
Code:
b[3][3] = {{1,1,0},{0,0,1},{1,0,1}}; Second, "b[3][3]" is a reference to a single int. "{{1,1,0},{0,0,1},{1,0,1}}" is a literal 3x3 integer array. You can't store an array in an int. You'll need to reexamine how you're approaching your data. There looks like some confusion exists on how to deal with pointers, arrays, or both. |
that quite true Dark_Helmet
@kponenation there is nothing wrong with your creation of the array Code:
int height = 3; Code:
int b[3][3] = { {1,1,0}, {0,0,1}, {1,0,1} }; |
i'm pretty sure b[3][3] = {{1,1,0},{0,0,1},{1,0,1}};
is correct for 1 1 0 0 0 1 1 0 1 |
Quote:
what you are trying to do here Dark_Helmet has pointed(haha) out is incorrect. |
then how do i assign values to the 2 dimensional array?
and thanks for your help |
it depends on where the values come from, but in my experience:
b[0][0] = 1; b[0][1] = 1; b[0][2] = 0; if anybody know of another way(not including loops etc) i would like to hear it. |
Code:
b[0][0]=1; |
i have a 20x20 array that i need to assign values (can't be random values)
that's why i want to know if i could do a[] []={{........}} but i guess I need to type in 400 values by typing... Thank you dmail and tuxdev. If you have any suggestion please reply me. James |
well as an alternative create and intailise the array in a header file and make it global, you then dont need to pass the address of the array from func to func and neither do you have to create the array using memory funcs.
|
As far as I know the array[i][j] means *(*array+i)+j correct me if i'm wrong and this is a simple int value. As they told you cannot store a 2 dimensional array in an int. But the
Code:
{ |
May be like this
Code:
int m[][3]={{1,2,3},{5,6,7},{8,9,10}}; |
sorry my bad. Could you explain shortly why is it working? This pointer stuff is not totaly clear for me.
|
Code:
b[3][3] = {{1,1,0},{0,0,1},{1,0,1}}; I.e. when you declare the variable. Not at runtime. |
2-D Array Initializing in C Programming
One can initialize a 2-D integer Array in C as below :
int b[3][3] = {1,1,0,0,0,1,1,0,1}; Pls. note that in all there are 9 elements (3 rows and 3 cols). I know this post is quite old but just in case somebody still needs it. |
You do realize the original poster could have completed both a bachelor's and a master's in the time that's passed since 2005...
Kevin Barry |
The key to this problem is that C does not have multidimensional arrays, only one dimensional ones. The declaration
int **b; creates a pointer to an array, each element of which is a pointer to a one dimensional array. This structure could be set up by b = (int **)malloc(3*sizeof(int *)); for (i=0;i<3;i++) b[i] = (int *)malloc(3*sizeof(int)); Now b[3][3]=1; sets the last element of the array. Note that b[3][3] = {{1,1,0},{0,0,1},{1,0,1}}; was wrong on two counts: b[3][3] would be an integer variable if it had existed, which it did not; in any case it would be out of range, since arrays are zero-indexed. Gnu C has an extension: a declaration like int b[3][3]; sets up a local 3 by 3 array, which may be initialised by int b[3][3] = {{1,1,0},{0,0,1},{1,0,1}}; but observe that this is an initialisation, performed at compile time, not an assignment. Notice also that both dimensions must be given explicitly. I suspect that b in this case is really an array of 9 integers which is treated as two dimensional, but I cannot be sure. It may be possible to assign (in either method) something like b[1]={0,0,1}; I cannot say without trying it, not possible on a borrowed MAC without Linux. There is a fundamental difference between the original C version and the Gnu extension. Malloc creates space on the heap, which is global, whereas the extension allocates local space, scoped to the function in which b is declared. |
All times are GMT -5. The time now is 05:34 AM. |