Solved

small question about pointer array

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

  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 …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now