Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

small question about pointer array

Posted on 2006-11-19
3
Medium Priority
?
276 Views
Last Modified: 2010-04-01
     char* pszMonths[] ={"invalid",
                          "January",
                          "February",
                          "March",
                          "April",
                          "May",
                          "June",
                          "July",
                          "August",
                          "September",
                          "October",
                          "November",
                          "December",};


In this statement there are 12 pointers that points at 12 months. Every pointer has 4 bytes in memory. The thing i don't understand is:

Where are these strings stored in memory?

pszMonths[1] is the pointer that points at January. Let's say 0x111 is the address of "January" But how command do tell "store 'January' at 0x111 " ? Is it stored simultaneously with the pointer array? (sorry for my english)
0
Comment
Question by:data_grrr
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 500 total points
ID: 17975828
The array you declared is an array of pointers to chars. Pointer to some object is actually its address (address of its first byte).

So basicly this is array of addresses of begginings of your strings, so elements of your array may look like this in memory:

pszMonths[0]   ---> 0x0400 and this is address of the first character of your first string.
pszMonths[1]   ---> 0x0700 and this is address of the first character of your second string.
...
pszMonths[12] ---> 0x1004 and this is address of the first character of your 12th string.

And memory looks like this:

0x0400 -> i
0x0401 -> n
0x0402 -> v
...
0x0700 -> J
...

The choice of addresses where compiler puts your strings is compiler's job so you don't need to worry about that.  Of course compiler tends to put your strings one after another without empty spaces between them in memory.

So it is very probbably that memory will look like this

i n v a l i d \0  J a n u a r y \0 F e b r u a r y...

Regards, U.V.
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 17975835
\0 is an character with ASCII code 0 that marks the end of some char array.
If you need more help, just say...
0
 
LVL 1

Author Comment

by:data_grrr
ID: 17975842
thanks, now i understand :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question