Solved

small question about pointer array

Posted on 2006-11-19
3
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 125 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

Independent Software Vendors: 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

617 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