Originally Posted by
Scatman
the name multi without brackets is a pointer to the first element (array of 4 elements). However i cannot increment it to point to the next element, because multi is pointer constant.
No, the name multi without brackets is an array of 3 arrays of 4 ints. In most contexts, it is converted to a pointer to an array of 4 ints, but that is not what it is, and that is why you cannot increment it as you cannot assign to an array even if you could assign to its elements (i.e., the array itself is not an lvalue).
Originally Posted by
Scatman
to do that i have to define a pointer variable, a pointer that can point to an array of 4 elements:
Code:
int (*ptr)[4];
ptr = multi.
now i cant use some pointer arithmetik (ptr++).
But you can do pointer arithmetic:
Code:
#include <stdio.h>
int main(void)
{
int multi[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};
int (*ptr)[4] = multi;
printf("%d\n", (*ptr)[0]);
ptr++;
printf("%d\n", (*ptr)[0]);
return 0;
}
You will see 0 and then 4 printed because the increment of ptr caused it to point from the first array element of multi to the second.
Originally Posted by
Scatman
and i dont know how to define a pointer variable (which can be incremented and decremented) and assign message to it so that i can do the following:
message is an array of 3 pointers to char. Therefore, in most contexts, it will be converted to a pointer to a pointer to char. Hence, you could write:
Code:
char **ptr = message;
for (int i = 0; i < 3; i++)
printf("%s ", *ptr++);
By the way, because the pointers to char will point to the first elements of string literals, you should declare them pointers to const char:
Code:
const char *message[3] = {"One", "two", "three"};
const char **ptr = message;
This will help to warn you in case you make the mistake of trying to modify a string literal.