How do strings work in C

1 week ago 34

There are multiple ways to create a string in C:

char* string1 = "hi"; char string2[] = "world"; printf("%s %s", string1, string2)

I have a lot of problems with this:

According to my understanding of [[Pointers]], string1 is a pointer and we're passing it to [[printf]] which expects actual values not references.

if we accept the fact that printf expects a pointer, than how does it handle string2 (not a pointer) just fine

I understand that char* is designed to point to the first character of a string which means it effectively points to the entire string, but what if I actually wanted to point to a single character

this doesn't work, because we are assigning a value to a pointer:

int* a; a = 8

so why does this work:

char* str; str = "hi"
submitted by /u/spcbfr to r/C_Programming
[link] [comments]
Read Entire Article