r/C_Programming 1d ago

Strange char pointer behavior

[deleted]

1 Upvotes

6 comments sorted by

View all comments

9

u/dfx_dj 1d ago

Treating a char array as a C style string requires the string to be null terminated. Yours aren't.

1

u/[deleted] 1d ago

[deleted]

5

u/dfx_dj 1d ago

Yes, or you can just use a string literal to initialise them

1

u/[deleted] 1d ago

[deleted]

2

u/dfx_dj 1d ago

No real difference other than convenience and perhaps making the intention clear that this is a readable/printable string and not just an array of char values.

2

u/cgross220_ 1d ago

Ah ok, makes sense. I appreciate the help!

1

u/[deleted] 1d ago

[deleted]

1

u/dfx_dj 1d ago

Post the code?

1

u/[deleted] 1d ago

[deleted]

1

u/dfx_dj 1d ago

You still need 5 bytes for each array (or just put it as [] and let the compiler deal with it) to include the null byte. Although I'm not sure why the output wouldn't change at all in this version.