1. ## Arrays with pointers

Hi guys

Lets look at an example:

Code:
`char *str[]={"hi", "all"};`
This code creates an array of pointers to char, and makes the pointers point to the character arrays "hi" and "all", respectively. Now, my question is regarding the following code:

Code:
```int intarr[2][2]={{1,2} , {3,4}};
int *intptr[]=intarr;```
As you may know by now, the above code is completely wrong. My question is: Why is it wrong? Because in the first example with strings, the array of pointers to char point to character-arrays. In the second example, the pointers to integer point to integer-arrays.

Why is it that in the integer-case, I must specify that the pointers point to arrays of length 2 (i.e. int (*intptr)[2] is correct), while I do not have to do that in the string-case?

Best,
Niles.

2. I believe Salem answered this just recently. IIRC, he said the name of the array is a pointer to the first element of the array - you know that.

The address of the name of the array, is the value you need for a pointer to the whole array.

It's WAAAYYYY late, so I'm not sure if that's right, but try it.

This works, but isn't quite what I thought would work:

Code:
```#include <stdio.h>
#include <stdlib.h>

int main() {
int i;

int intarray[2][2]={{1,2}, {3,4}};
int *pint=*intarray;

printf("\n\nN1=%d, N2=%d, N3=%d", *(pint+0), *(pint+1), *(pint+2) );

i = getchar();
return 0;
}```

3. hi Niels_M,

Its because the name of the 2*2 matrix intarr is a pointer to an array of two pointers each of which point to a row of the matrix.....

So that is why you have to inform the compiler like (*intptr)[2])....

Hope this helps
Regards
C_Enthusiast

4. But intarr = &intarr[0] = &intarr[0][0]?

5. This is a better example, from Elysia's suggestion in another thread:

Code:
```#include <stdio.h>

int main() {
int i, j, len, n;

int Num[2][2]={{1,2}, {3,4}};
int (*pint)[2]=Num;

printf("\n\nN1=%d, N2=%d, N3=%d, N4=%d", pint[0][0], pint[0][1], \
pint[1][0], pint[1][1] );

i = getchar(); ++i;
return 0;
}```
I like this one. Makes sense to me.

6. As you may know by now, the above code is completely wrong. My question is: Why is it wrong? Because in the first example with strings, the array of pointers to char point to character-arrays. In the second example, the pointers to integer point to integer-arrays.
No, in the first example, You have an array of pointer to char.
The type of array element is pointer to char.

7. Originally Posted by Bayint Naung
No, in the first example, You have an array of pointer to char.
The type of array element is pointer to char.
That is also what my original post states.

8. Take string literals as anonymous char array which you can't modify and have static scope.

Code:
```static char _0[ ]  = "hi";
static char _1[ ]  = "all";

char *str[]={ _0 , _1 };```
>>That is also what my original post states.
You states the array of pointers to char point to character-arrays.
The type of str is array! It's not pointer! It's array initialization.

9. Originally Posted by Bayint Naung
Take string literals as anonymous char array which you can't modify and have static scope.

Code:
```static char _0[ ]  = "hi";
static char _1[ ]  = "all";

char *str[]={ _0 , _1 };```
I will. But I think we are all losing focus here with respect to the OP. I'll just quote the question:

Why is it that in the integer-case, I must specify that the pointers point to arrays of length 2 (i.e. int (*intptr)[2] is correct), while I do not have to do that in the string-case?

char *str[]={"hi", "all"};
This code creates an array of pointers to char, and makes the pointers point to the character arrays "hi" and "all", respectively. Now, my question is regarding the following code:
Code:
```int intarr[2][2]={{1,2} , {3,4}};
int *intptr[]=intarr;                  // created an array of pointers to int :
that's fine but does the pointers point to int array ?
// int *intptr[ ] = { intarr[0] , intarr[1] };  // to be equalivent to string analogy```
Are you doing the same thing here?

Also if you have
Code:
``` char char_arr[2][100] = {    { 'h','i',\0'}  , { 'a', 'l', 'l','\0'}  };
char *char_ptr[] = char_arr;           // will it work?
or char (*char_ptr)[2] = char_arr;   // or this ?```

11. Yes, I believe that (obviously I am wrong). Because the name "intarr" is the address of the first element in "intarr", which is (1,2). This has the same address as the element 1.

12. The difference is the type. If you do pointer arithmetic.
Try the values of :
intarr + 1
&intarr[0] + 1
&intarr[0][0] + 1

13. Originally Posted by Bayint Naung
The difference is the type. If you do pointer arithmetic.
Try the values of :
intarr + 1
&intarr[0] + 1
&intarr[0][0] + 1
Hmm,

intarr + 1: address of array 2
&intarr[0] + 1: address of row 2
&intarr[0][0] + 2: address of first element in row 2

Am I correct?

14. Ok, I feel like this thread got de-railed totally. I really appreciate all the input so far, but it does not seem to be answers to my OP.

I still don't feel like I have gotten a clear answer, so if anybody would help, I would appreciate it very much.

15. Why is it that in the integer-case, I must specify that the pointers point to arrays of length 2 (i.e. int (*intptr)[2] is correct), while I do not have to do that in the string-case?
Because your two examples are not the same.