Unexpected dereferencing of char array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Unexpected dereferencing of char array?

When I try to dereference a char array in printf() standard function using the "%c" format specifier for char it delivers the first character. Has it anything to do with the fact that arrays of C types are kind of pointers or the like? How can I dereference a non-pointer variable? If it dereferences, so can I consider that a pointer also? This is the code: #include <stdio.h> int main (void) { char str [50] = "Hello, World!"; printf ("%c", *str ); } Output: H Conversely, if you code a char pointer, you can assign it a string and use "%s" format specifier to the char pointer to print the "char array". This is an example code: #include <sdtio.h> int main (void) { char *str = "Hello, World!"; printf ("%s", str ); } Output: Hello, World!

14th Jan 2022, 10:19 AM
Christina Ricci
Christina Ricci - avatar
2 Answers
+ 1
Thanks ROOKIE for the tip!
14th Jan 2022, 10:52 AM
Christina Ricci
Christina Ricci - avatar