question about a struct pointer parameter
Hello everybody!
Recently while i was experimenting with some code written in c++ i noticed something that confused me a bit. In that code i had to deal with 2 structs :
Code:
struct date
{
string day;
int month;
int dday;
int year; };
and the other struct
Code:
struct note
{
date d;
string notice; };
I noticed that when i was passing in a function i built, an instance of note struct as a pointer parameter my program kept crashing continuously, although compiler wasn't complaining. To make myself clear :
Code:
void myFunction(note* n, ...other parameters...)
{
cout << n->d.day << endl;
cout << n->d.month << endl;
etc...
}
I'm sure that my program crashed because of that piece of code
i'm sure because i tested it in debug mode.
When i wrote my function differently i had no problem running my program :
Code:
void myFunction(note& n, ..other parameters...)
{
cout << n.d.day << endl;
cout << n.d.month << endl;
etc...
}
Has anyone got an idea why the pointer parameter and the arrow
caused that problem? Looking forward to reading your ideas and thoughts...Thank you in advance for your time...
vanalex
Re: question about a struct pointer parameter
I doubt that could be the cause.
Most likely that std::string is getting corrupted somehow, though. Make sure you aren't trying to use memset, memcpy, or anything like that.
Re: question about a struct pointer parameter
Thank you very much for your reply Lindley, but i found what caused the whole problem. The . operator precedes the -> operator in expressions so i had to write my code like this:
wrong way without parenthesis:
vanalex
Re: question about a struct pointer parameter
No wrong again!
RIGHT CODE
sorry...:)
Re: question about a struct pointer parameter
Quote:
Originally Posted by
vanalex
No wrong again!
RIGHT CODE
sorry...:)
I don't think that is the reason. -> and . operators have the same preference and they have left to right associativity.
http://cppreference.com/wiki/operator_precedence
Re: question about a struct pointer parameter
Also, interpreting n->d.day as n->(d.day) does not make sense. What exactly is that supposed to mean to the compiler?