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();
}

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;
}

thank you. it is correct. This website helps me to learn c+++

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.