passing struct pointer to function.

Discussion in 'C Programming' started by Durango, Oct 28, 2011.

  1. Durango

    Durango Guest

    Hello, I am trying to write a function that will take a struct pointer and
    some other values defined in the struct and within the function allocate
    memory for the struct and pass the values to it. How can this be done in
    C?

    Here is some example code that does not work:

    struct a {
    int a;
    long b;
    char buf[32];
    }

    void createNode(struct a* x, int y, long z, char *mesg);
    int main()
    {
    struct a* x;
    int a=1;
    long b=2.0;
    char *msg = "This is a test";

    createNode(&x, a, b, msg);
    }

    void createNode(struct a* x, int y, long z, char *mesg)
    {
    x=malloc(sizeof(struct a)); x->a = y;
    x->b = z;
    strcpy(x->buf, mesg)
    }
     
    Durango, Oct 28, 2011
    #1
    1. Advertising

  2. Durango

    Mark Bluemel Guest

    On 10/28/2011 09:56 AM, Durango wrote:
    > Hello, I am trying to write a function that will take a struct pointer and
    > some other values defined in the struct and within the function allocate
    > memory for the struct and pass the values to it. How can this be done in
    > C?
    >
    > Here is some example code that does not work:


    Doesn't work? It can't even compile... If you want us to look at sample
    code, at least
    a) get it to compile (or tell us about the problems you're having
    compiling it)
    b) cut and paste the real code rather than typing some approximation.

    >
    > struct a {
    > int a;
    > long b;
    > char buf[32];
    > }


    Missing ';' here.

    >
    > void createNode(struct a* x, int y, long z, char *mesg);
    > int main()
    > {
    > struct a* x;
    > int a=1;
    > long b=2.0;
    > char *msg = "This is a test";
    >
    > createNode(&x, a, b, msg);


    createNode is declared as taking "struct a*", you're passing "struct a**".

    > }
    >
    > void createNode(struct a* x, int y, long z, char *mesg)
    > {
    > x=malloc(sizeof(struct a)); x->a = y;


    You are setting the parameter passed by value, this won't help.

    Also you haven't included a declaration of malloc (or strcpy), so the
    compiler will assume malloc returns int.

    > x->b = z;
    > strcpy(x->buf, mesg)


    Missing ';' again.
    > }
     
    Mark Bluemel, Oct 28, 2011
    #2
    1. Advertising

  3. On Oct 28, 10:56 am, Durango <> wrote:
    > Hello, I am trying to write a function that will take a struct pointer and
    > some other values defined in the struct and within the function allocate
    > memory for the struct and pass the values to it.  How can this be done in
    > C?
    >


    You pass in a pointer to a pointer

    int main()
    {
    struct a *x;
    int a=1;
    long b=2; /* longs must be integers, maybe you mean double */
    char *msg = "This is a test";

    /* address of x creates a pointer to a pointer, as x is a pointer */
    createNode(&x, a, b, msg);
    }

    void createNode(struct a** x, int y, long z, char *msg)
    {
    *x = malloc(sizeof(struct a));
    if(!*x)
    /* out of memory error */
    (*x)->a = y;

    /* etc */
    }

    --
    Read Basic Algorithms, now also available as an Apple ibook
    http://www.malcolmmclean.site11.com/www
     
    Malcolm McLean, Oct 28, 2011
    #3
  4. Durango

    Eric Sosman Guest

    On 10/28/2011 4:56 AM, Durango wrote:
    > Hello, I am trying to write a function that will take a struct pointer and
    > some other values defined in the struct and within the function allocate
    > memory for the struct and pass the values to it. How can this be done in
    > C?


    As you've asked it, the answer is "In no way." If there is no
    memory allocated to the struct when you call the function, you cannot
    pass a pointer to that as-yet-unallocated memory as a function argument.
    There are, however, some alternative approaches.

    One is to pass not a pointer to the nonexistent struct, but a
    pointer to a pointer variable. The function can allocate memory and
    can use its argument pointer to make the variable point to the
    allocated memory:

    void createNode(struct a* *p, int y, long z, char *mesg) {
    struct a* x = malloc(sizeof *x);
    *p = x; /* store pointer in caller's variable */
    if (x != NULL) {
    x->whatever = whatnot;
    ...
    }
    }

    /* In the caller: */
    struct a* ptr;
    createNode(&ptr, 42, 999L, "Hello, world");
    if (ptr != NULL) {
    printf("%d %ld %s\n", ptr->a, ptr->b, ptr->buf);

    Another and possibly smoother method is to pass only the "data"
    arguments and let the function return the struct pointer as its value:

    struct a* createNode(int y, long z, char *mesg) {
    struct a* x = malloc(sizeof *x);
    if (x != NULL) {
    x->whatever = whatnot;
    ...
    }
    return x;
    }

    /* In the caller: */
    struct a* ptr = createNode(42, 999L, "Hello, world");
    if (ptr != NULL) {
    printf("%d %ld %s\n", ptr->a, ptr->b, ptr->buf);

    --
    Eric Sosman
    d
     
    Eric Sosman, Oct 28, 2011
    #4
  5. In article <4eaa6e47$0$293$>,
    Durango <> wrote:
    >Hello, I am trying to write a function that will take a struct pointer and
    >some other values defined in the struct and within the function allocate
    >memory for the struct and pass the values to it. How can this be done in
    >C?


    You define a function that allocates the space for the struct, fills
    in the struct with the passed-in values, and then either return the
    pointer to that allocated struct or the function can accept a pointer
    to a pointer to the struct and fill that in.

    Method 1:

    struct a *x;

    x = createNode(y, z, mesg);

    ...

    struct a *
    creatNode(int a, long b, const char *mesg)
    {
    struct a *x = malloc(sizeof(*x));
    x->a = a;
    x->b = b;
    strncpy(x->buf, mesg, sizeof(x->buf));
    return x;
    }

    Method 2:

    struct a *x;

    createNode(&x, y, z, mesg);

    ...

    void
    creatNode(struct a **xp, int a, long b, const char *mesg)
    {
    struct a *x = malloc(sizeof(*x));
    x->a = a;
    x->b = b;
    strncpy(x->buf, mesg, sizeof(x->buf));
    *xp = x;
    }


    Note: error checks for malloc() returns NULL and mesg too long for buf
    omitted for clarity.


    --
    -Ed Falk,
    http://thespamdiaries.blogspot.com/
     
    Edward A. Falk, Oct 28, 2011
    #5
  6. Durango

    Durango Guest

    On Fri, 28 Oct 2011 22:03:03 +0000, Edward A. Falk wrote:

    > In article <4eaa6e47$0$293$>,
    > Durango <> wrote:
    >>Hello, I am trying to write a function that will take a struct pointer
    >>and some other values defined in the struct and within the function
    >>allocate memory for the struct and pass the values to it. How can this
    >>be done in C?

    >
    > You define a function that allocates the space for the struct, fills in
    > the struct with the passed-in values, and then either return the pointer
    > to that allocated struct or the function can accept a pointer to a
    > pointer to the struct and fill that in.
    >


    <snip>

    Thank you all for your thoughtful feedbacks. I apologize for the poor
    code, I am just here trying to learn :)
     
    Durango, Oct 29, 2011
    #6
  7. Durango

    Jorgen Grahn Guest

    On Fri, 2011-10-28, Malcolm McLean wrote:
    > On Oct 28, 10:56 am, Durango <> wrote:
    >> Hello, I am trying to write a function that will take a struct pointer and
    >> some other values defined in the struct and within the function allocate
    >> memory for the struct and pass the values to it.  How can this be done in
    >> C?
    >>

    >
    > You pass in a pointer to a pointer
    >
    > int main()
    > {
    > struct a *x;
    > int a=1;
    > long b=2; /* longs must be integers, maybe you mean double */
    > char *msg = "This is a test";
    >
    > /* address of x creates a pointer to a pointer, as x is a pointer */
    > createNode(&x, a, b, msg);
    > }
    >
    > void createNode(struct a** x, int y, long z, char *msg)
    > {
    > *x = malloc(sizeof(struct a));
    > if(!*x)
    > /* out of memory error */
    > (*x)->a = y;
    >
    > /* etc */
    > }


    Passing a pointer to something to fill in is tedious and makes the
    calling code harder to read: use it only when there's no other option.

    It's easier to simply return the pointer, just like malloc() does:

    struct a* createNode(int y, long z, char *msg)
    {
    struct a* const x = malloc(sizeof(*x));
    if(!x)
    { /* out of memory error */ }
    x->a = y;
    /* etc */
    return x;
    }

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Oct 29, 2011
    #7
  8. Jorgen Grahnæ–¼ 2011å¹´10月29日星期六UTC+8下åˆ3時15分56秒寫é“:
    > On Fri, 2011-10-28, Malcolm McLean wrote:
    > > On Oct 28, 10:56 am, Durango <> wrote:
    > >> Hello, I am trying to write a function that will take a struct pointerand
    > >> some other values defined in the struct and within the function allocate
    > >> memory for the struct and pass the values to it.  How can this bedone in
    > >> C?
    > >>

    > >
    > > You pass in a pointer to a pointer
    > >
    > > int main()
    > > {
    > > struct a *x;
    > > int a=1;
    > > long b=2; /* longs must be integers, maybe you mean double */
    > > char *msg = "This is a test";
    > >
    > > /* address of x creates a pointer to a pointer, as x is a pointer */
    > > createNode(&x, a, b, msg);
    > > }
    > >
    > > void createNode(struct a** x, int y, long z, char *msg)
    > > {


    x is a pointer to a pointer of type struct a
    x[j] is struct a, if allocated well!
    > > *x = malloc(sizeof(struct a));

    not efficient to just allocate 1 , also no free of x[0]
    > > if(!*x)
    > > /* out of memory error */
    > > (*x)->a = y;
    > >
    > > /* etc */
    > > }

    >
    > Passing a pointer to something to fill in is tedious and makes the
    > calling code harder to read: use it only when there's no other option.
    >
    > It's easier to simply return the pointer, just like malloc() does:
    >
    > struct a* createNode(int y, long z, char *msg)
    > {
    > struct a* const x = malloc(sizeof(*x));
    > if(!x)
    > { /* out of memory error */ }
    > x->a = y;
    > /* etc */
    > return x;
    > }
    >
    > /Jorgen
    >
    > --
    > // Jorgen Grahn <grahn@ Oo o. . .
    > \X/ snipabacken.se> O o .
     
    88888 Dihedral, Oct 31, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ole
    Replies:
    4
    Views:
    623
    Michael Wojcik
    Oct 26, 2004
  2. beetle
    Replies:
    2
    Views:
    967
    beetle
    Jan 25, 2005
  3. Zero
    Replies:
    16
    Views:
    682
    Barry Schwarz
    Nov 19, 2005
  4. Vijai Kalyan
    Replies:
    4
    Views:
    746
    Vijai Kalyan
    Nov 8, 2005
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    510
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page