Solved

small question about pointer array

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

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

Suggested Solutions

Title # Comments Views Activity
ASP.net build a IF/Then Walkthrough Guide 1 321
C++ question 3 84
Dialogbox API leak? 18 119
I could not set window to top 4 63
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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

752 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