r/cprogramming • • Feb 26 '26

What's the best way to make a string variable?

I generally use:
char* string_variable = "text here";
But if there's a better way, let me know.

12 Upvotes

28 comments sorted by

View all comments

Show parent comment

1

u/Swipsi Feb 28 '26

So the size of the char* returns the size of a char pointer, but the size of s[] would return the size of a char since thats what the first array element is pointed on by s[]?

1

u/Jonny0Than Feb 28 '26

No, sizeof when applied to an array returns the size of the entire array. You do have to be careful because some things that look like arrays are not arrays, like void foo(char str[])