Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > passing struct pointer to function.

Reply
Thread Tools

passing struct pointer to function.

 
 
Durango
Guest
Posts: n/a
 
      10-28-2011
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)
}

 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      10-28-2011
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.
> }

 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      10-28-2011
On Oct 28, 10:56*am, Durango <(E-Mail Removed)> 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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-28-2011
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Edward A. Falk
Guest
Posts: n/a
 
      10-28-2011
In article <4eaa6e47$0$293$(E-Mail Removed)>,
Durango <(E-Mail Removed)> 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, (E-Mail Removed)
http://thespamdiaries.blogspot.com/
 
Reply With Quote
 
Durango
Guest
Posts: n/a
 
      10-29-2011
On Fri, 28 Oct 2011 22:03:03 +0000, Edward A. Falk wrote:

> In article <4eaa6e47$0$293$(E-Mail Removed)>,
> Durango <(E-Mail Removed)> 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

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      10-29-2011
On Fri, 2011-10-28, Malcolm McLean wrote:
> On Oct 28, 10:56*am, Durango <(E-Mail Removed)> 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 .
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      10-31-2011
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 <(E-Mail Removed)> 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[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 .


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struct pointer vs. struct array pointer aleksa C Programming 16 02-20-2013 08:20 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
(: Pointer to struct withing pointer to struct :) Zero C Programming 16 11-19-2005 01:27 AM
passing pointer->struct->pointer->struct to function. .. ?? beetle C Programming 2 01-25-2005 06:08 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments