question about a struct pointer parameter
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: question about a struct pointer parameter

  1. #1
    Join Date
    Jun 2009
    Location
    Chrysoupoli, Greece
    Posts
    3

    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
    Code:
    n->d.day
    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

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    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.

  3. #3
    Join Date
    Jun 2009
    Location
    Chrysoupoli, Greece
    Posts
    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:

    Code:
    (n->).d.day
    wrong way without parenthesis:

    Code:
    n->d.day
    vanalex

  4. #4
    Join Date
    Jun 2009
    Location
    Chrysoupoli, Greece
    Posts
    3

    Re: question about a struct pointer parameter

    No wrong again!

    RIGHT CODE

    Code:
    (n->d).day
    sorry...

  5. #5
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: question about a struct pointer parameter

    Quote Originally Posted by vanalex View Post
    No wrong again!

    RIGHT CODE

    Code:
    (n->d).day
    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

  6. #6
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center