|
-
June 8th, 2009, 02:49 PM
#1
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
-
June 8th, 2009, 03:01 PM
#2
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.
-
June 8th, 2009, 03:37 PM
#3
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
-
June 8th, 2009, 03:39 PM
#4
Re: question about a struct pointer parameter
No wrong again!
RIGHT CODE
sorry...
-
June 9th, 2009, 12:34 AM
#5
Re: question about a struct pointer parameter
 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
-
June 9th, 2009, 11:21 AM
#6
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?
Old Unix programmers never die, they just mv to /dev/null
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|