Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   passing struct pointer to function. (http://www.velocityreviews.com/forums/t805271-passing-struct-pointer-to-function.html)

Durango 10-28-2011 08:56 AM

passing struct pointer to function.
 
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)
}


Mark Bluemel 10-28-2011 09:25 AM

Re: passing struct pointer to function.
 
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.
> }


Malcolm McLean 10-28-2011 09:43 AM

Re: passing struct pointer to function.
 
On Oct 28, 10:56*am, Durango <El_Dura...@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 = 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

Eric Sosman 10-28-2011 11:56 AM

Re: passing struct pointer to function.
 
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
esosman@ieee-dot-org.invalid

Edward A. Falk 10-28-2011 10:03 PM

Re: passing struct pointer to function.
 
In article <4eaa6e47$0$293$14726298@news.sunsite.dk>,
Durango <El_Durango@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 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, falk@despams.r.us.com
http://thespamdiaries.blogspot.com/

Durango 10-29-2011 01:32 AM

Re: passing struct pointer to function.
 
On Fri, 28 Oct 2011 22:03:03 +0000, Edward A. Falk wrote:

> In article <4eaa6e47$0$293$14726298@news.sunsite.dk>,
> Durango <El_Durango@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 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 :)


Jorgen Grahn 10-29-2011 07:15 AM

Re: passing struct pointer to function.
 
On Fri, 2011-10-28, Malcolm McLean wrote:
> On Oct 28, 10:56*am, Durango <El_Dura...@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 = 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 .

88888 Dihedral 10-31-2011 06:17 AM

Re: passing struct pointer to function.
 
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 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 .




All times are GMT. The time now is 05:35 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.