CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009
    Posts
    40

    Accessing inside structure via a struct pointer to a struct pointer

    "
    #include <stdio.h>

    struct datastructure
    {
    char character;
    };


    void function(struct datastructure** ptr);


    int main()
    {
    struct datastructure trial;
    struct datastructure *structPtr;
    structPtr=&trial;
    structPtr->character='a';
    function(&structPtr);
    }


    void function(struct datastructure** ptr)
    {
    *ptr->character='a';
    printf("Ptr: &#37;c",*ptr->character);
    }
    "

    These codes give these errors:
    error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'
    error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'


    these errors are related to
    "
    *ptr->character='a';
    printf("Ptr: %c",*ptr->character);
    "

    I want to access "character" data inside the structure "trial" by a pointer to pointer "ptr" inside function "function",but I couldn't find a way to do this.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Accessing inside structure via a struct pointer to a struct pointer

    You should have written:
    Code:
    (*ptr)->character = 'a';
    Otherwise, the compiler interprets it as *(ptr->character), but of course a pointer does not have a character member.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Oct 2009
    Posts
    40

    Re: Accessing inside structure via a struct pointer to a struct pointer

    Thank you very much,really I was working on it for hours to make it work!

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Accessing inside structure via a struct pointer to a struct pointer

    You're welcome

    That said, why do you need to pass a pointer to a pointer? You are not modifying the pointer to the struct object, so just passing a pointer to the struct object will do.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured