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