int aryint[4] = {1,2,3,4};
char *ptr = "1234";
char ch ='a';
char *chptr = &ch;
//chptr = 3509449623 (mem addr)
printf("chptr = %u, chptr);
//aryint = 3509449648 (mem addr)
printf("aryint = %u ", aryint);
//ptr = 1234
printf("ptr = %s, ptr);
Why would printing out a char point “ptr” return an entire array of char (the whole string), and printing out a char point “chptr” return the mem addr of “chptr”?
If this is because “ptr” is pointing to an array, then why printing out the array “aryint” only return the mem addr of aryint (not the entire array)? So printing out entire array only applies to string constant? Or is it because of the %s?
But when I tried replacing the %s with %u on the printf() of ptr, I also get a constant (4197008) which is equivalent to the entire array/string (1234), and not the mem address:
//ptr u = 4197008 (a constant not mem addr), ptr s = 1234,
printf("ptr u = %u, ptr s = %s”, ptr, ptr)
However, when I attempt to print out char array “str”, I get the mem address when I applied the %u on “str” (not the entire array as in ptr). But I got the entire array printed again when %s is applied on “str”, and not the mem address as in chptr and aryint previously.
char str[5] = "1234";
//str u = 3509449696 (mem addr), str s = 1234
printf("str u = %u, str s =%s”, str, str );
This seems a bit strange to me. Is there any other weird phenomena that I should be aware of when coding in C? I am a newbie.
Any help will be greatly appreciated!