How to find memory content at location x?

H

humanmutantman

I'm reading a book that lists the example:

char month[10] = "January";

Memory for this looks something like the following:

| J | a | n | u | a | r | y | \0 | ? | ? |

Q1: How can I find out what is in the two memory locations following
the string terminator?
Q2: Does gcc zero out unused array elements?
Q3: If yes to Q2, where would I find that information?

Thanks!
 
B

Ben Pfaff

I'm reading a book that lists the example:

char month[10] = "January";

Memory for this looks something like the following:

| J | a | n | u | a | r | y | \0 | ? | ? |

Q1: How can I find out what is in the two memory locations following
the string terminator?

They're set to 0.
Q2: Does gcc zero out unused array elements?

Yes. The language standard requires it.
Q3: If yes to Q2, where would I find that information?

C99 6.7.8, para 21:

If there are fewer initializers in a brace-enclosed list
than there are elements or members of an aggregate, or fewer
characters in a string literal used to initialize an array
of known size than there are elements in the array, the
remainder of the aggregate shall be initialized implicitly
the same as objects that have static storage duration.
 
W

Walter Roberson

Ben Pfaff said:
C99 6.7.8, para 21:
If there are fewer initializers in a brace-enclosed list
than there are elements or members of an aggregate, or fewer
characters in a string literal used to initialize an array
of known size than there are elements in the array, the
remainder of the aggregate shall be initialized implicitly
the same as objects that have static storage duration.

The C89 wording does not have the phrase about characters
in a string literal, but the "intended" behaviour can be deduced
by the example saying that initialization with a string
"is identical to" initialization with the brace-enclosed list
of characters, together with the explicit phrasing about fewer
initializers.

But on the surface, the C89 wording would appear to allow for a
difference between

char month[10] = { "January" };
and
char month[10] = "January";

in that the former is certainly a "brace enclosed list" but the later
is not -- the C89 part about the braces being optional for string
literals does not preclude the possibility of different behaviours
when the braces are or are not present.
 
M

Morris Dovey

Ben Pfaff (in (e-mail address removed)) said:

| "I don't have C&V for that handy, but I've got Dan Pop."
| --E. Gibbons

Speaking of whom, where is Dan these days?

And while I'm asking OT questions, are you still at the same west
coast institution and do you still find time to make steel sing and
dance?
 
H

humanmutantman

I'm reading a book that lists the example:

char month[10] = "January";

Memory for this looks something like the following:

| J | a | n | u | a | r | y | \0 | ? | ? |

Q1: How can I find out what is in the two memory locations following
the string terminator?
Q2: Does gcc zero out unused array elements?
Q3: If yes to Q2, where would I find that information?

Thanks for the answers. Now for two more related questions. I put in a
for loop to print the cell contents:

printf("Month: %s\n", month);
for (cell=0;cell<10;cell++)
{
printf("Cell %d: %c\n",cell+1,month[cell]);
}

Q4: How can I get the hex contents of each cell?
Q5: How can I format "printf("Cell %d: %c\n",cell+1,month[cell]);" so
I can get:
....
Cell 9 ... // Note: There are two spaces before the '9'.
Cell 10 ... // Note: There is one space before the "10".

Thanks!
 
W

Walter Roberson

I put in a
for loop to print the cell contents:
printf("Month: %s\n", month);
for (cell=0;cell<10;cell++)
{
printf("Cell %d: %c\n",cell+1,month[cell]);
}
Q4: How can I get the hex contents of each cell?

Each cell only has value contents, and values are abstract. But you
can ask to have those values represented in hex:

printf("Cell %d: %c(%x)\n", cell+1,month[cell],(unsigned int)month[cell] );
Q5: How can I format "printf("Cell %d: %c\n",cell+1,month[cell]);" so
I can get:
Cell 9 ... // Note: There are two spaces before the '9'.
Cell 10 ... // Note: There is one space before the "10".

%2d instead of %d
 
H

humanmutantman

I put in a
for loop to print the cell contents:
printf("Month: %s\n", month);
for (cell=0;cell<10;cell++)
{
printf("Cell %d: %c\n",cell+1,month[cell]);
}
Q4: How can I get the hex contents of each cell?

Each cell only has value contents, and values are abstract. But you
can ask to have those values represented in hex:

printf("Cell %d: %c(%x)\n", cell+1,month[cell],(unsigned int)month[cell] );
Q5: How can I format "printf("Cell %d: %c\n",cell+1,month[cell]);" so
I can get:
Cell 9 ... // Note: There are two spaces before the '9'.
Cell 10 ... // Note: There is one space before the "10".

%2d instead of %d

That was pretty cool. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top