Solved

small question about pointer array

Posted on 2006-11-19
3
246 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 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

778 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