Thread: pointers and struct

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    17

    pointers and struct

    Function my_create() takes an argument of 'struct socket **s'. I'm trying to pass in the 'struct socket *skt' member of 'struct sA' into function my_create().

    Can anyone help out here. I'm pretty sure I dont' have this correct.

    Code:
    struct sA {
        struct socket *skt;
    };
    
    struct sA a;
    
    my_create(struct socket **s);
    
    int main()
    {
        ...
        my_create(&a->skt);		// i don't think this is correct
        ...
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Code:
    my_create(&a.skt);
    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.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    17
    That gives me the following error when I try to compile it.

    Code:
    error: request for member 'skt' in something not a structure or union

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Then a must be a pointer, and not a struct, as you originally posted.
    Code:
    $ cat bar.c
    #include<stdio.h>
    
    struct socket { int a; };
    
    struct sA {
        struct socket *skt;
    };
    
    struct sA a;
    
    void my_create(struct socket **s);
    
    int main()
    {
        my_create(&a.skt);     // i don't think this is correct
    }
    $ gcc -c bar.c
    Post what you tried, not something paraphrased from memory.
    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.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by rrlangly View Post
    Code:
        my_create(&a->skt);		// i don't think this is correct
    Did you try
    Code:
        my_create(&(a.skt));
    Tim S.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    London
    Posts
    12
    Quote Originally Posted by stahta01 View Post
    Did you try
    Code:
        my_create(&(a.skt));
    Tim S.
    This looks like it should work to me - passing in the address of the pointer (a pointer to a pointer **).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers with Struct
    By JamesD in forum C Programming
    Replies: 12
    Last Post: 03-10-2011, 12:48 PM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. Struct as pointers
    By wirefree101 in forum C Programming
    Replies: 8
    Last Post: 12-03-2009, 02:48 PM
  4. struct pointers
    By seidel in forum C Programming
    Replies: 2
    Last Post: 08-11-2009, 10:19 AM
  5. Assigning pointers from pointers in struct.
    By Brian in forum C Programming
    Replies: 2
    Last Post: 10-18-2003, 04:30 AM