Click to See Complete Forum and Search --> : Pointer/Structure question (C)
dunno
10-08-2000, 03:54 PM
If I have a structure with a pointer as one of it's members, how do I change the address that pointer points to?
place walmart;
typedef struct {
place * location;
} person;
void moveperson(person * theperson) {
/* code to change theperson's location to walmart */
}
int main() {
person jeff;
place somewhere;
jeff.location = &somewhere;
moveperson(&jeff);
}
How would I make jeff.location = &walmart ?
Keith M
10-08-2000, 09:21 PM
Unless I'm missing your point, my guess is that you aren't aware of using -> instead of . when dealing with a pointer to a structure. Your function should consist of:
person->location = &walmart
dunno
10-08-2000, 09:34 PM
I believe though that you use the dereference -> when the struct object is a pointer, in this case it is, but what about when the member of the struct is also a pointer? what if I want to change the address that member points to?
Strike
10-08-2000, 09:52 PM
This code is incomplete. Where's your definition for the place datatype?
But, the code for your function would be theperson->location = &walmart, I believe. I don't know, this code isn't really intuitive with the naming, so it's not clicking for me, sorry.
----edit----
I'm a moron, and I told you it wasn't clicking http://www.linuxnewbie.org/ubb/smile.gif
[This message has been edited by Strike (edited 08 October 2000).]
Stuka
10-08-2000, 11:28 PM
I could be wrong here, but it would seem to me that the proper code would read: theperson->location = &walmart
This dereferences the person pointer, refers to its location member (which is itself a pointer), and gives it a valid reference to a place object.
Keith M
10-08-2000, 11:33 PM
I'm assuming that place is some sort of structure or other type defined elsewhere, and that walmart has been initialized with some value. That being the case, the fact that location is inside person does not change the way it is used. You have written the code jeff.location = &somewhere; which is completely valid, and inside the function theperson->location = &walmart is valid also. Basically that is saying that location is now equal to the address of the variable walmart.
dunno
10-09-2000, 03:38 PM
So, if that is the case then:
(this doesn't really reference the above code all that much)
When the struct isn't a pointer
jeff.location == &walmart
jeff->location == walmart
When the struct is a pointer
jeff->location == &walmart
jeff.location == walmart
???
jemfinch
10-09-2000, 04:22 PM
When you're working with a pointer to a struct, you have to use the -> operator to access any members of the referenced struct. The . operator makes no sense in that context, since a pointer has no fields.
Jeremy
dunno
10-09-2000, 06:48 PM
When a struct has a member which is a pointer to an object. How do I refer to the value of the object that is being pointed to by the member of the struct, when the struct itself is a pointer and when the struct is not a pointer?
Normal pointers:
variable == address of something;
*variable == value of what is pointed at;
Struct members as pointers:
struct.member == address of something;
??? == value of what is pointed at;
Pointers to structs with members as pointers:
struct->member == address of something;
??? == value of what is pointed at;
Keith M
10-09-2000, 10:04 PM
So you want to alter the value of a pointer in a struct? OK, now that's different. You do the following:
s is a structure
p is a pointer and is a member of s
*s.p
or
*s->p if s is a pointer to a structure.
Does that help?
Of course, p needs to be pointed to something valid.
dunno
10-10-2000, 09:39 PM
Are you sure it is not:
s.(*p)
or
s->(*p)
That seems to make more sense to me
Keith M
10-11-2000, 03:58 PM
You may need to use some parenthesis, but I'm pretty sure the * goes first. You might have to do *(s->p). s->p resolves to the address of p, then you dereference it with the *, so that's why the * goes first.
Like other people have said in that other thread, get the K&R book. Its an invaluable reference for C syntax.
StarWeaver
10-12-2000, 05:56 AM
Uh, i think you guys are getting a little confused. He is using a staticly-done um... function local non pointer related variable for the struct itself. See
person jeff;
That has nothing to do with pointers to structs thus '->' is out of the question.
He awnsered his own question. You set the pointer variable 'location' contaned in the local structure variable 'jeff' by using the local structure element operater '.' and assining '=' TO it the address of '&' the location you want to set it to 'walmart'.
Or simply:
jeff.location = &walmart;
Gah http://www.linuxnewbie.org/ubb/smile.gif
now if you had something like this
person jeff;
person * p_pointer;
p_pointer = &jeff;
p_pointer->location = &walmart;
[or would it be '*p_pointer->locatoin = &walmart;' ... i've never actually done THIS]
the -> doodad would make sense.
Why am i taking an intro to C class again? <BONK>
-Ronin Angel StarWeaver
[This message has been edited by StarWeaver (edited 12 October 2000).]