Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #12318

Re: passing struct pointer to function.

From 88888 Dihedral <dihedral88888@googlemail.com>
Newsgroups comp.lang.c
Subject Re: passing struct pointer to function.
Date 2011-10-30 23:17 -0700
Organization http://groups.google.com
Message-ID <29606791.839.1320041868083.JavaMail.geo-discussion-forums@pref15> (permalink)
References <4eaa6e47$0$293$14726298@news.sunsite.dk> <0b162be3-152f-43a7-a7f7-6939a9deb66c@j20g2000vby.googlegroups.com> <slrnjana1b.1bk.grahn+nntp@frailea.sa.invalid>

Show all headers | View raw


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 <El_Du...@yahoo.com> 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 is a pointer to a pointer of type struct  a
x[i][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   .

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

passing struct pointer to function. Durango <El_Durango@yahoo.com> - 2011-10-28 08:56 +0000
  Re: passing struct pointer to function. Mark Bluemel <mark_bluemel@pobox.com> - 2011-10-28 10:25 +0100
  Re: passing struct pointer to function. Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-10-28 02:43 -0700
    Re: passing struct pointer to function. Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-10-29 07:15 +0000
      Re: passing struct pointer to function. 88888 Dihedral <dihedral88888@googlemail.com> - 2011-10-30 23:17 -0700
  Re: passing struct pointer to function. Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-28 07:56 -0400
  Re: passing struct pointer to function. falk@rahul.net (Edward A. Falk) - 2011-10-28 22:03 +0000
    Re: passing struct pointer to function. Durango <El_Durango@yahoo.com> - 2011-10-29 01:32 +0000

csiph-web