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
myCharArray1andmyCharArray2are 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
myCharMultiArrayrequires some memory in an address not associated with wheremyCharArray1ormyCharArray2are stored, and the syntax ofchar myCharMultiArray[2][10] = {myCharArray1, myCharArray2};is actually trying to move themyCharArray1andmyCharArray2data 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).
char myCharMultiArray[2][10] = {myCharArray1, myCharArray2};Does it show a warning about "making integer value from pointer of different size" or similar?chararray.p &myCharArray1in gdb I get(char (*)[4]) 0x20041ff4. So this address is in hex and the numbers I'm seeing in themyCharMultiArray(i.e.\364\360) are octal? decimal? Just trying to figure out how I would check that for my sanity.0xF4, 0xF0which are the least significant bytes of&myCharArray1, &myCharArray2