LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Double pointers and two dimensional arrays in C (http://www.linuxquestions.org/questions/programming-9/double-pointers-and-two-dimensional-arrays-in-c-385862/)

kponenation 11-23-2005 11:50 AM

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;
int width = 3;
int **b;
  int total,i,j;
  b=(int **)malloc(height*(sizeof(int *)));
  for (i=0; i<height; i++){
    b[i]=(int *)malloc(width*sizeof(int));
    b[3][3] = {{1,1,0},{0,0,1},{1,0,1}}; // I GET AN ERROR here
    find(b);
  };

when I compile this i get an error at this line "b[3][3] = {{1,1,0},{0,0,1},{1,0,1}}; "
How can I fix this?

I'm little confused about the two dimenstional array and double pointers.

Thank you,
James

tuxdev 11-23-2005 11:57 AM

try making your array like
Code:

int **array=new (int *)[width]
for(int i=0;i<width;i++)array[i]=new int[height];


kponenation 11-23-2005 12:03 PM

thanks but i get an error at this line

int **array=new (int *)[width];

tuxdev 11-23-2005 12:19 PM

try making width and height const. What the errors are is more important than just the line number. Post them!

dmail 11-23-2005 12:24 PM

tuxdev please look at the title for this thread. C doesn't have the new operator, this was introduced in C++.

kponenation 11-23-2005 12:27 PM

so i also tried int **array=(int *)[width]; but it still doesn't work.

kponenation 11-23-2005 12:29 PM

and the error i get is " syntax error before '[' token"

dmail 11-23-2005 12:35 PM

Quote:

Originally posted by kponenation
so i also tried int **array=(int *)[width]; but it still doesn't work.
if you want to dynamicly create it C you need to use malloc,calloc etc.
I havn't post the answer yet,because im trying to figure it myself, its a long time since i used C.

Dark_Helmet 11-23-2005 12:37 PM

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}};
First of all, index '3' is out of bounds. For a height and width of 3, the row and column indices range in value from 0 to 2.

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.

dmail 11-23-2005 12:55 PM

that quite true Dark_Helmet
@kponenation there is nothing wrong with your creation of the array
Code:

        int height = 3;
        int width = 3;
        int i;
        int**b;
        b=(int **)malloc( height * ( sizeof(int*) ) );

        for (i=0; i<height; i++)
                { b[i] = (int*)malloc( width * sizeof(int) ); }

if you hadn't of already created the array, you could create and fill on the fly like so:
Code:

int b[3][3] = { {1,1,0}, {0,0,1}, {1,0,1} };

kponenation 11-23-2005 12:57 PM

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

dmail 11-23-2005 01:04 PM

Quote:

Originally posted by kponenation
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

Im afraid its not. you have already created the array like i said, if you hadn't then you could do it by declaring and initialising in the same line like the above post.
what you are trying to do here Dark_Helmet has pointed(haha) out is incorrect.

kponenation 11-23-2005 01:05 PM

then how do i assign values to the 2 dimensional array?

and thanks for your help

dmail 11-23-2005 01:12 PM

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.

tuxdev 11-23-2005 01:13 PM

Code:

b[0][0]=1;
b[0][1]=1;
b[0][2]=0;
b[1][0]=0;
...

I don't know of any elegant way of doing it.


All times are GMT -5. The time now is 06:24 AM.