Structs and pointers
-
- DCEmu Freak
- Posts: 71
- https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
- Joined: Thu Feb 20, 2003 11:46 am
- Has thanked: 1 time
- Been thanked: 0
- Contact:
Structs and pointers
I have a pointer to a struct like this:
struct list_type *vmuq;
I can manipulate this struct without any problem, but when I pass it to a function like this:
salva_questoes_vmu(vmuq);
looks like it don't pass anything at all, I can't access any value of it from inside the function:
void salva_questoes_vmu(struct list_type *tmpvmuq) {
...
bla bla bla
...
printf("%s\n", tmpvmuq->right);
the printf above just prints nothing...
Any idea?
struct list_type *vmuq;
I can manipulate this struct without any problem, but when I pass it to a function like this:
salva_questoes_vmu(vmuq);
looks like it don't pass anything at all, I can't access any value of it from inside the function:
void salva_questoes_vmu(struct list_type *tmpvmuq) {
...
bla bla bla
...
printf("%s\n", tmpvmuq->right);
the printf above just prints nothing...
Any idea?
-
- DCEmu Freak
- Posts: 71
- Joined: Thu Feb 20, 2003 11:46 am
- Has thanked: 1 time
- Been thanked: 0
- Contact:
-
- Insane DCEmu
- Posts: 138
- Joined: Sun Apr 06, 2003 5:29 am
- Has thanked: 0
- Been thanked: 0
- Contact:
are you allocating memory for the structure pointer?
Code: Select all
list_type *vmuq = (list_type *)malloc(sizeof(list_type));
strcpy(vmuq->right,"blah blah");
salva_questoes_vmu(vmuq);
void salva_questoes_vmu(list_type *tmpvmuq) {
printf("%s\n", tmpvmuq->right);
}
-
- DCEmu Freak
- Posts: 71
- Joined: Thu Feb 20, 2003 11:46 am
- Has thanked: 1 time
- Been thanked: 0
- Contact:
Yes, I'm.
On the code above, the first printf works, the second one don't
Code: Select all
strcpy(vmuq.right,"blah blah");
printf("%s\n", vmuq.right);
salva_questoes_vmu(vmuq);
void salva_questoes_vmu(list_type *tmpvmuq) {
printf("%s\n", tmpvmuq->right);
}
-
- DC Developer
- Posts: 529
- Joined: Wed Oct 17, 2001 7:44 pm
- Location: Texas
- Has thanked: 0
- Been thanked: 0
- Contact:
somehow you have to pass the address of where the struct begins.
or just
It been awhile so I could be wrong on the syntax.
Troy
Code: Select all
struct list_type *vmuq, z;
z=&vmuq;
salva_questoes_vmu(z);
Code: Select all
salva_questoes_vmu(&vmuq);
Troy
-
- Insane DCEmu
- Posts: 138
- Joined: Sun Apr 06, 2003 5:29 am
- Has thanked: 0
- Been thanked: 0
- Contact:
Well from the code you presented, it doesn't look like you declared vmuq as a pointer, because you are accessing vmuq wit the "." in the first and 2nd lines rather than "->"PrOfUnD Darkness wrote:Yes, I'm.
On the code above, the first printf works, the second one don'tCode: Select all
strcpy(vmuq.right,"blah blah"); printf("%s\n", vmuq.right); salva_questoes_vmu(vmuq); void salva_questoes_vmu(list_type *tmpvmuq) { printf("%s\n", tmpvmuq->right); }
Then you're passing the variable to a function that required the address of the variable. So you might want to replace your third line with salva_questoes_vmu(&vmuq).
if vmuq has been declared as a pointer, then you can call it like so:
salva_questoes_vmu(vmuq)