0

hello it is easy program but there is a mistake in void application
while printing the birthday, pt pointer gives a mistake why?

#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;

struct Date
{
       
       int day;
       int month;
       int year;
};

void printdate(Date* pt)
{
     cout << "\nbirthday= "<<*pt->day<<"."<<*pt->month<<"."<<*pt->year;
     }
     

int main(int argc, char *argv[])
{
    Date t;
    t.day=11;
    t.month=11;
    t.year=2009;
    printdate (&t);
    getch();
}
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by burcin erek
0

On line 16, you can remove the dereference operator and your code will work as you expect it to. Remember that the dereference operator in effect treats your pointer as the actual object. Conversely, if you wanted to use the dereference operator, change the '->' to a period (.). For example:

Change:

void printdate(Date* pt)
{
cout << "\nbirthday= "<<*pt->day<<"."<<*pt->month<<"."<<*pt->year;
}

To (to use the pointer as a pointer):

void printdate(Date* pt)
{
cout << "\nbirthday= "<<pt->day<<"."<<pt->month<<"."<<pt->year;
}

Or (to use the pointer as the object):

void printdate(Date* pt)
{
cout << "\nbirthday= "<<(*pt).day<<"."<<(*pt).month<<"."<<(*pt).year;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.