[C] Pointer member in structure confusion

Hi, I have a typedef'd struct with a pointer member:

1
2
3
4
typedef struct
{
    int *i;
} TEMP;


I want to set the VALUE of this member:

1
2
TEMP temp;
*(temp.i) = 42;


I would think that temp.i would be a pointer, then I would dereference it with *, then set the value. The code compiles but the program crashes at that point. Can someone explain what I'm doing wrong?

Thanks.
i points to a garbage address in the memory. You're going to need to allocate memory for i.
Ah right, silly mistake. Thanks.
Another Q. If I wanted to use malloc, would I have to malloc each member in the struct, or can I just malloc the pointer to the struct? The latter seems to crash on me:

1
2
3
4
5
TEMP *temp;
temp = malloc(sizeof(*temp));

*(temp->i) = 42;
printf("%d", *(temp->i));


But, after experimenting I found out that it won't crash if I just malloc the member and not the struct:

1
2
3
4
5
TEMP *temp;
temp->i = malloc(sizeof(*temp->i));

*(temp->i) = 42;
printf("%d", *(temp->i));


It would be a pain if I had to malloc a struct with a lot of pointers, so can somebody tell me if I'm doing something wrong or if there's an easier way?
You must malloc each member separately.

If you want to do it for all objects of your type, and you are using C++, then you can just use a constructor to do it for you.
If you have to use C, you can still imitate C++:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
typedef struct
{
    int *i; 
    /*Other members*/
} TEMP;

void construct_TEMP(TEMP *t) { 
    t->i = malloc(sizeof(int));
    /*Init other members*/
}

void destruct_TEMP(TEMP *t) { 
    t->i = free(t->i);
    /*free other members*/
}

int main() {
    TEMP t;
    construct_TEMP(&t);
    /*use t*/
    destruct_TEMP(&t);
    return 0;
}


EDIT: fixed code, thx yoonkwun and Disch
Last edited on
Thanks, I have just two more questions. If the structure contained non-pointer members, do I have to malloc them in the same way I would malloc a pointer? Also, in your code R0mai, I noticed in line 8:

t->i = malloc(sizeof(t->i));

It would return a pointer to a memory block of 4 bytes, since that's the size of a pointer right? Isn't it supposed to be:

t->i = malloc(sizeof(*t->i));

so that it returns a memory block of how big t->i is, and not how big a pointer is?
t->i = malloc(sizeof(t->i));

This code isn't right.

t->i is a pointer, so you're allocating the size of a pointer, which may not be the size of an int.

The right way to do it:

t->i = malloc(sizeof(int) * however_many_ints_you_want_to_allocate);
If the structure contained non-pointer members, do I have to malloc them in the same way I would malloc a pointer?

No, you don't have to allocate those.
Last edited on
I would advise two things:

1) use new instead of malloc;
2) declare the struct as:

1
2
3
struct TEMP {
   int* i;
};


The way you've declared it is a bad C-ism that you should avoid in C++.
Based on the code and thread title I assumed he was coding C.
Topic archived. No new replies allowed.