How to reference **ptr_xx to ptr_yy [xx][xx]
Hi All,
Good Morning to everyone here. :P
May i know anyone here can help to answer one of my question. I would like to know how to reference double dimension array with double dimension pointer. ( i think this may sounds silly to someone here :) )
I have tried to reference one dimension array with pointer without problem,
e.g. char xx[100]; char *yy;
yy = xx;
sprintf(yy, "You are a nice guy");
// good result
However, if i try with 2 dimension pointer to 2 dimension array
char xx[100][500];
char **yy;
yy = xx;
// not working - cannot compile
char *xx[100];
char **yy;
yy =xx;
// can compile - but it gives error during excution
Is there any method to hv the address of pointer same as array declared for 2 dimension memory.
I do not/cant use "malloc" and "new" due to some reason, can anyone help to answer to my doubt?
Thanks a millions time.
James Goh
Re: How to reference **ptr_xx to ptr_yy [xx][xx]
First, look at "std::vector" which is much easier and safer to work with than raw C-style arrays. In any case, an array name always decays to a pointer to the first element of the array. Given your example then:
char xx[100][500];
What is stored in this array? Well, another array actually. That is, there are 100 elements in your array, each of which is an array of 500 chars. So, since the name "xx" decays to a pointer to the first element of the array as mentioned above, and that element is an array of 500 chars, then you're looking at this:
char (*yy)[500] = xx;
"yy" is a pointer to an array of 500 chars. The syntax is brutal but the underlying idea of an array that stores another array is elegant (and you can carry this idea to any number of dimensions). It takes a little practice to think about it this way (draw your elements out on paper to see) but it becomes self-evident after a short time.
Re: Re: How to reference **ptr_xx to ptr_yy [xx][xx]
Quote:
Originally posted by Sef
First, look at "std::vector" which is much easier and safer to work with than raw C-style arrays. In any case, an array name always decays to a pointer to the first element of the array. Given your example then:
char xx[100][500];
What is stored in this array? Well, another array actually. That is, there are 100 elements in your array, each of which is an array of 500 chars. So, since the name "xx" decays to a pointer to the first element of the array as mentioned above, and that element is an array of 500 chars, then you're looking at this:
char (*yy)[500] = xx;
"yy" is a pointer to an array of 500 chars. The syntax is brutal but the underlying idea of an array that stores another array is elegant (and you can carry this idea to any number of dimensions). It takes a little practice to think about it this way (draw your elements out on paper to see) but it becomes self-evident after a short time.
Thanks Sef.
I managed to solve the prob with another approach this afternoon by using casting.
eg.
char xx[100][500];
char **yy;
yy = (char **) xx;
yy[0] = "You are a nice guy";
yy[1] = "Thanks for answer to my question";
printf("%s", yy[0]);
printf("%s", yy[1]);
// it works so far.
so far so good :)
Btw, if i am require to declare a very huge variable , such as e.g. char xx[9000][9000], how could i done with it? The compiler hv no problem to compile . However, when comes to running the exection file, i enconter error such as "cannot write to 0x0xxxxx location". Is it the RAM memory size not enough ? What or how can i increase my memory space for my huge 2 dimension array??
Million thanks and BestRegards,
James Goh
Re: Re: Re: How to reference **ptr_xx to ptr_yy [xx][xx]
Quote:
Originally posted by James Goh
Thanks Sef.
I managed to solve the prob with another approach this afternoon by using casting.
eg.
char xx[100][500];
char **yy;
yy = (char **) xx;
yy[0] = "You are a nice guy";
yy[1] = "Thanks for answer to my question";
printf("%s", yy[0]);
printf("%s", yy[1]);
// it works so far.
so far so good :)
Oh, I know why your code seems to work. That is because you re-assign the pointers to point to the constant strings. But your program certainly does not do what you think it does. Try inseting the following lines at the end of your program:
Code:
printf("%c", x[0][0]);
printf("%c", x[1][0]);
You'll see that you print junk values. See my previous post as to why!
- Kevin