Thread: Pointer to struct members and to the whole structure.

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    2

    Pointer to struct members and to the whole structure.

    Hi everybody,

    I'm trying to solve a very complex issue (complex at least for me!).

    I have to pass a pointer to a struct's member as a function parameter. I can't pass a pointer to the whole structure, but only to a structure's member (I'm trying to use an API already done).
    Is it possible to access the whole structure and to write values in other struct members location from inside the function?

    Till now, my attempts gave me "segmentation fault" results.

    Best regards
    Beppe
    Last edited by satsriakal; 12-20-2017 at 08:54 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    For example:

    Code:
    typedef struct INDATA
    {
        int fld1;
        char *buf;
    } INDATA;
    
    void anotherFunc(int *x)
    {
        *x = 10;
        ........ // code
    }
    
    void aFunc(INDATA *idPtr)
    {
        idPtr->fld1 = 10;
        .....
    
    }
    
    
    int main()
    {
        INDATA id;
    
        aFunc(&id);
    
        anotherFunc(&(id.fld1));
        ....
        return(0);
    }

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    2
    Actually I have this problem: I'm sure I'm writing in the right position; I verified the address is correct, but when I write, I get "segmentation fault".

    I got the right address, adding offset to struct member address.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    You need to post your actual test case.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    It is easy to make your own offsetof(),
    for example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    // gcc tst.c -o tst
    
    typedef struct INDATA
    {
        int fld1;
        int fld2, fld3, fld4;
        char buf1[8];
        char buf2[8];
    } INDATA;
    
    
    
    int main()
    {
        INDATA id;
        void *vptr1, *vptr2;
        unsigned long offset;
    
        vptr1 = &id;
        vptr2 = &(id.buf2);
    
        offset = vptr2 - vptr1;
        printf("offset field buf2 = %ld\n", offset);
    
        strcpy((char *) vptr2, "Hallo");
        printf("id.buf2 = %s\n", id.buf2);
    
        return(0);
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    >offset = vptr2 - vptr1;
    You need to cast the pointers to char* in order to do arithmetic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    >offset = vptr2 - vptr1;
    You need to cast the pointers to char* in order to do arithmetic.
    Why ?

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by ddutch View Post
    Why ?
    Because subtracting two pointers gives you the distance in elements, not bytes. If they're arrays of ints, it'll be the distance in ints, and so on. You can't use void pointers because the compiler doesn't know what size their elements are.
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    Is it not so that void-pointers are (single) byte pointers

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by ddutch View Post
    Is it not so that void-pointers are (single) byte pointers
    No, never. void pointers literally have no type. You can't do anything with them except casting/assigning them to other types.
    Devoted my life to programming...

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    offsetof() is already defined in stddef.h and has been since C89. Use that instead of rolling your own.

  13. #13
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    Yes indeed, one can not do pointer arithmetic with void pointer and it is not allowed according to the Ansi C standard.
    But .... in my example above I only use void-pointers in order to obtain 2 memory addresses and find out the difference
    between them. True, statements like vptr++ would have been illegal....

    Required for my example were pointers which align on 1 byte boundaries and void pointer do so. But indeed, it
    migth have been better for portability (my GNU compiler did not complain though) to have used unsigned char pointers.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Gcc allows it unless you use the options to restrict it to standard behaviour.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ddutch
    But .... in my example above I only use void-pointers in order to obtain 2 memory addresses and find out the difference
    between them. True, statements like vptr++ would have been illegal....
    This compiles for me without warning on gcc 5.4 with the same lack of warning options in the compile options as you used:
    Code:
    int main(void)
    {
        char str[] = "hello";
        void *vptr = str;
        vptr++;
        return 0;
    }
    so if you find it acceptable to subtract pointers to void, then you must also find it acceptable to increment pointers to void, contrary to your claim that "statements like vptr++ would have been illegal". If you agree that the increment is illegal, then you are following the C standard, so likewise you should agree that subtracting pointers to void is illegal, and hence you should pass -pedantic as an option to gcc to warn you about such illegal things.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing structure members
    By mc088 in forum C Programming
    Replies: 1
    Last Post: 01-26-2017, 10:42 PM
  2. How to find number of structure members in a given structure?
    By bhaskarReddy in forum C Programming
    Replies: 4
    Last Post: 01-16-2012, 05:37 AM
  3. Accessing Structure Members
    By anndruu12 in forum C Programming
    Replies: 5
    Last Post: 12-02-2010, 02:37 AM
  4. Pointer to struct members
    By kybert in forum C Programming
    Replies: 7
    Last Post: 07-01-2003, 08:27 AM
  5. structure members outside main
    By threahdead in forum C Programming
    Replies: 5
    Last Post: 03-09-2003, 07:34 AM

Tags for this Thread