We help IT Professionals succeed at work.

plz help

ambitious
ambitious asked
on
hi all
////////////////////////////////////////
// File Name: pr09001.cpp
////////////////////////////////////////
#include <iostream>

// Date structure.
struct Date
{
    int month, day, year;
};

////////////////////////////////////////
// Display a date in a standard format.
////////////////////////////////////////
void display(Date& dt)
{
    static char *mon[] =
    {
        "January","February","March","April","May","June",
        "July","August","September","October","November",
        "December"
    };

    std::cout << mon[dt.month-1] << ' '
              << dt.day << ", " << dt.year;
}

////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
    Date birthday = {4, 6, 1961};
    std::cout << "Alan's date of birth is ";
    display(birthday);

    return 0;
}
in the above code i wanna ask that (1)why we have used an array of pointers ?(2)why we have passed the address of the structure.it wouldnt have been possible without that?(3)which things decide that we are required to pass addresses
thank you
Ambitious:=)
Comment
Watch Question

Commented:
Are these homework questions?  If so, I wouldn't be happy giving answers to all three.  But - you need to think about what is happening in terms of memory allocation, and passing the address of something large is a lot "cheaper" than passing it by value, where the entire thing would be copied.

Play around with the code, and try doing it other ways - you will learn a lot <hopes he wasn't too patronizing>

Commented:
>>why we have used an array of pointers

You wrote this program, didn't you?  

No, wait, I'll bet this is a homework problem and you don't have a clue and this is due tomorrow...

It's against EE policies for you to just post your homework and ask for someone to do it for you.  This is going to get you removed from this site if you persist...
CERTIFIED EXPERT

Commented:
(1)why we have used an array of pointers ?

think how strings work, would it have been possible to store all those strings in a single array!. no, therefore we have an array of pointers, each pointer pointing to a char array (string).

(2)why we have passed the address of the structure.it wouldnt have been possible without that?

try it an see what happerns, first work out the difference between

function(Obj o)
function(Obj &o);
function(Obj *o);

(3)which things decide that we are required to pass addresses

if the caller must see changes made to the variable
if the variable is massive and would take up loadz of stack space, therefore passing thge address only requires 4 bytes

Paul

Commented:
ambitious, is this homework.  It is strictly forbidden by this site for homework to be performed here.  I am going to lock this and others you may have out until I get a response from you.

Thank you,
ComTech
Community Support Moderator

Author

Commented:
hi
this is not a homework problem but it was given as an example in the book which i am following.he gave this example to show the differences between procedural programming and object oriented programming.Plz,this is no homework but i wasnt able to understand those three points which i asked .i have just started doing c++ so plz help
thanx
Ambitious:=)

Commented:
ambitious all I asked for was an explination, you gave me one I am satisfied with.  I will unlock the question, and you may procede.

Thank you,
ComTech
Community Support Moderator

I will reject my own proposed answer to re-open.

Author

Commented:
hi comtech
thank you very much.sir the thing is that i often put the whole code  and ask questions regarding it becuase im doing c++ of my own without anybodys help.so i hope u'll understand me in future also.thanx for unlocking the question.
Ambitious:=)
Good Luck for your C++ Programming Future.

- Ajay Chadha

Author

Commented:
hi
thank you very much ajay
1) The array of pointers was used for 2 reasons that I can think of... first, it is SIMPLER AND PRETTIER than static char[][9]... secondly, it is MORE EFFICIENT.  If you used a 2d array like char[][9] you would be allocating 9 bytes (enough to hold "December") for every month, even the short ones like "May".

2) Why we have passed the address of the structure?  I have no idea...  there was no reason at all to pass the address in the example you posted, which leads to question...

3) When would we really want or need to pass the address?  If the function you are passing the structure to ever needs to change any of the data it must receive the address either as a pointer (Date *dt) or by reference (Data &dt).  By receiving the address, the function can actually go the that specific location in memory where your structure is stored and alter the data there.  If you DON'T pass it by address, the function just makes a copy of the structure.  Anything done to the structure afterwards wouldn't affect the original structure that you passed.

Also, as someone else pointed out, if you had a REALLY BIG STRUCTURE or if you had very limited memory and had to conserve it, you could pass the structure by reference or use a pointer to avoid having the compiler make a local copy of the structure for the function.

Author

Commented:
Hi smitty
ur comment cleared my questions but one thing which i wanna ask is that when we have declared the array of pointers like in above code what actally is present in that array.Is it the addresses of the months???if so then where are those months stored?? OR  is it storing these months directly in that array??
thanx to you
and thak you all for your responses
Ambitious:=)
CERTIFIED EXPERT

Commented:
the array is an array of pointers therefore you have one array that is contiguous in memeory, each item being a pointer to string. the strings being the month name. each item in the array is an address that points to the begining of a string.

Paul
CERTIFIED EXPERT

Commented:
therefore the actual month names are no contained in the array, they are all contained in there own individual arrray of chars, the first char of each being pointed to by an entry in the array of pointers

Author

Commented:
Hi paul
Thank you very much for clerifying this point im really thankful to you but plz i will have to give the points to smitty cuz she explained the solutions of my probs in detail.i hope u'll help me like this only in future
thanx
Ambtious

Author

Commented:
thanx smitty for ur help and hope for the same in future
ambitous:=)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.