First block
#include <stdio.h>
const int MAX = 3;
int main() {
int var[] = { 10, 100, 200 };
int i, *ptr[MAX];
for (i = 0; i < MAX; i++) {
ptr[i] = &var[i];
}
for (i = 0; i < MAX; i++) {
printf("Value of var[%d] = %d\n", i, *ptr[i]);
}
return 0;
}
Easy to understand since ptr is an array of int pointers. Therefore, when you need to access the i-th element , you need to dereference its value as *ptr[i].
Now the second block is exactly the same, but now it points to a pointer array char:
#include <stdio.h>
const int MAX = 4;
int main() {
char *names[] = {
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali",
};
int i = 0;
for (i = 0; i < MAX; i++) {
printf("Value of names[%d] = %s\n", i, names[i]);
}
return 0;
}
This time, when we need to access its element, why not add it first *?
I tried to generate the correct instruction for printing this value, it seems if you dereferenced, it will be one char. Why?
printf("%c", *names[1])
I know that in C there are no lines, and this is an array char. And I know pointers, but still not under the syntax.