I am new to C and was trying to play around with different ways to initialize arrays of chars according to various ways explained here and found one difference I cannot explain based on what I could learn from that previous thread or other resources I've been learning from. Stopping at a breakpoint just below the lines shown below in gdb:
char myCharArray1[] = "foo";
char myCharArray2[] = "bar";
char myCharMultiArray[2][10] = {myCharArray1, myCharArray2};
char myCharMultiArrayLiteral[2][10] = {"foo", "bar"};
In gdb I notice the following:
ptype myCharMultiArray
type = char [2][10]
ptype myCharMultiArrayLiteral
type = char [2][10]
ptype myCharMultiArray[0]
type = char [10]
ptype myCharMultiArrayLiteral[0]
type = char [10]
info locals
myCharArray1 = "foo"
myCharArray2 = "bar"
myCharMultiArray = {"\364\360\000", "\000\000\000"}
myCharMultiArrayLiteral = {"foo", "bar"}
Why do the contents of myCharMultiArray
and myCharMultiArrayLiteral
differ? Where do the numbers in myCharMultiArray
\364\360
even come from?
If I were to try to explain why this is happening from what I've read so far, is it may have something to do with the following ideas:
- I'm inadvertently trying to modify a string literal
myCharArray1
andmyCharArray2
are not actually typechar [4]
(despite what gdb tells me) and they are just pointers to the first character in the string literals (i.e. the address of where the 'f' and 'b' are stored respectively.- The creation of a new char array
myCharMultiArray
requires some memory in an address not associated with wheremyCharArray1
ormyCharArray2
are stored, and the syntax ofchar myCharMultiArray[2][10] = {myCharArray1, myCharArray2};
is actually trying to move themyCharArray1
andmyCharArray2
data as opposed to copying it. Which is not possible for some reason I don't yet quite grasp.
Adding a link to a relevant topics (but still can't find a duplicate).