Reputation: 384
Alright, this is a really quick question: why does this first block of code compile, but the second one doesn't? They seem like they should be equivalent, since all that I change is the variable declaration/references.
First:
int memberStudents(struct studentType x, Snodeptr students, Snodeptr *s) {
Snodeptr p = *s;
while (p->next != NULL) {
if (strcmp(x.sid, p->student.sid) == 0)
return 1;
p = p->next;
}
return 0;
}
Second:
int memberStudents(struct studentType x, Snodeptr students, Snodeptr *s) {
while (s->next != NULL) {
if (strcmp(x.sid, s->student.sid) == 0)
return 1;
s = s->next;
}
return 0;
}
I want to change the Snodeptr s so that it points to the proper place, but when I try the second block of code I get an error saying there is a request for member 'next' in something not a struct or union.
Upvotes: 1
Views: 81
Reputation: 42165
In the first block, the while
loop operates on a Snodeptr
instance. In the second block you're operating on a Snodeptr*
.
Upvotes: 2
Reputation: 1673
I expect this (slightly modified version of your second snippet) should compile:
int memberStudents(struct studentType x, Snodeptr students, Snodeptr *s) {
while ((*s)->next != NULL) {
if (strcmp(x.sid, (*s)->student.sid) == 0)
return 1;
(*s) = (*s)->next;
}
return 0;
}
Upvotes: 1
Reputation: 3633
In the second code, you are using s->next != NULL
, s is pointer.
In the first code, you are using p->next != NULL
, p is not pointer
Upvotes: 1
Reputation: 31184
Snodeptr *s
and Snodeptr p
are different types, and therefore behave differently
try (*s)->next
Upvotes: 3