passing struct pointer to function.

D

Durango

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)
}
 
M

Mark Bluemel

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.
 
M

Malcolm McLean

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 */
}
 
E

Eric Sosman

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);
 
E

Edward A. Falk

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.
 
D

Durango

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 :)
 
J

Jorgen Grahn

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
 
8

88888 Dihedral

Jorgen Grahnæ–¼ 2011å¹´10月29日星期六UTC+8下åˆ3時15分56秒寫é“:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top