multi dimensional array with pointer ??
-
Hello, I am looking a prof's code. At some point, I found codes like following.
char **myob[2]; int **obj_n[10][2];could anyone tell me please what does the first and second codes mean. I understand that char** carray is same like char* carray[] i.e. array of string. But I do not know what char ** myob[2] mean. I also do not know how I can get access to the elements of this object.
Similarly, at other place I see code liketemplate <class T> int DeAlloc2DMemory(T ***ppArray, int nr, int nc);What does the three stars means and how can I call it? Could anyone can answer these two question please?
-
char **myob[2] is a array of two pointers to pointer to char.
i think this site can help you to understand the pointer structures: http://cdecl.ridiculousfish.com/?q=char+**+myob[2]
the function must be called in this way:
... int **obj_n[10][2]; //obj_n as array 10 of array 2 of pointer to pointer to int DeAlloc2DMemory(&obj_n, 10, 2);the three stars means a pointer to a pointer to a pointer
you should read something about pointers
-
euklid schrieb:
I understand that char** carray is same like char* carray[] i.e. array of string.
Which is wrong in most cases. This might help you to understand why: http://c-faq.com/aryptr/aryptr2.html
-
thank you so much for the answsers!
char** myob[2]; printf("size of aaray: %d\n",sizeof(myob)); myob[0][0] ="a"; myob[0][1] ="b"; int i; for( i=0; i <2; i++) { printf("char %s \n",myob[0][0]); }The size of mbob is it is showing 16. I do not understand why sizeof(char)=1 but sizeof(char*)=8. Similarly I do not know where is wrong in my above test code.
could you please help me in some way.
thank you so much in advance !
-
euklid schrieb:
The size of mbob is it is showing 16. I do not understand why sizeof(char)=1 but sizeof(char*)=8.
Read something about pointers. A pointer points to an address on your system, and therefore the type it points to has usually* no influence on its size.
* Although this is not required by the language, you would have trouble to find a system where sizeof(int*) != sizeof(double*).
http://stackoverflow.com/questions/1241205/are-all-data-pointers-of-the-same-size-in-one-platform
-
euklid schrieb:
char **myob[2]; int **obj_n[10][2];could anyone tell me please what does the first and second codes mean.
Try http://cdecl.org/
Keep in mind that postfix operators/declarators have a higher precedence than prefix operators. So, your declarations are equivalent tochar *(*(myob[2])); int *(*((obj_n[10])[2]));(added parens for explicit grouping)
You can practically read the declaration from the inside to the outside. Example:
myob is a 2-element array of pointers to pointers to char.euklid schrieb:
Similarly, at other place I see code like
template <class T> int DeAlloc2DMemory(T ***ppArray, int nr, int nc);What does the three stars means
It means that you found very ugly C++ code written by a person that does not know what proper encapsulation is and how to use it effectivly.
euklid schrieb:
and how can I call it?
Probably like this:
const int nr = 42; const int nc = 23; int** ugly = 0; Alloc2DMemory(&ugly,nr,nc); ... use it ... DeAlloc2DMemory(&ugly,nr,nc);Of course, this is bad because
uglywould be the only reference to the dynamically allocated memory while it is just a stupid pointer that does not feel responsible for managing this resource. This kind of coding style will eventually bite you in the a**. It's not exception-safe and puts the burden of managing the resource on the user instead of delegating this responsibility to some smart object. Also, this memory layout is at least questionable. I would prefer to use a single block of memory and order the elements in row major or column major.Cheers!
kk
-
euklid schrieb:
char** myob[2]; printf("size of aaray: %d\n",sizeof(myob)); myob[0][0]="a"; myob[0][1]="b";First of all: This code invokes undefined behaviour. myob is an array of pointers which you did not initialize properly. The array contains garbage. But you actually read the pointer values and dereference them => UB
euklid schrieb:
The size of mbob is it is showing 16.
That's not an unusual result. Apparently, on your system you need 8 bytes to store a pointer value. Since myob is a 2-element array of pointers, you need 16 bytes to store this array.
euklid schrieb:
I do not understand why sizeof(char)=1 but sizeof(char*)=8.
Why not? What's so funny about that? char* is the type of a pointer to char. A pointer variable is supposed to store the memory address of something. On a 64bit system, a pointer is usually 64bit wide which explains the value of sizeof(char*) you observed.