I am very much confused with pointers. If we create a pointer variable of type 'char', that is some thing like char *ch; and assign a character array to it. Then we can access each letter in the string by increasing the pointer (++ch).
char *ch;
char a[5]="hello";
*ch=a;
while(*ch) {
printf("%c",*ch);
++ch;
}
This works perfectly fine. But I see many examples like,
struct example {
char *ch;
};
int main() {
struct example ex={"hello"};
printf("%s",ex.ch);
}
I am pretty confused with this example like how directly assigning a string to a character pointer(instead of assigning the string to a variable and then assigning it to character pointer) can make it accessible. In the previous case, *ch points to the starting memory of the array "a". But in the second case to which memory does the pointer ch point to? Can anyone give me a clear explanation?