arrow operator

J

Jason

Hi everyone:

I was trying to pass a pointer to a structure to a function, where the
pointer is pointing to one of the members in the structure, is this
possible???

For Example:

struct reptile *animalPtr

foo(animalPtr->snakes);
foo(animalPtr->aligator);
/* here now animalPtr->snakes == 100 and
animalPtr->aligaotr == 100 */

Within foo, I was hoping to be able to change the value of the object
in the structure.

For Example:

foo(???){ /* not sure what the parameters will be */

if(blah < blah2)
??? = 100 /* where ??? is the input to this function */

}

I was wondering would this work? If so, what would my ??? in my
example be? How would I be able to change the value of ??? ? If this
is not possible, what how should I go about doing this? Thanks in
advance for all replies.

-Jason
 
M

Mike Wahler

Jason said:
Hi everyone:

I was trying to pass a pointer to a structure to a function, where the
pointer is pointing to one of the members in the structure, is this
possible???

Only if that member is also a structure.
For Example:

struct reptile *animalPtr

foo(animalPtr->snakes);
foo(animalPtr->aligator);
/* here now animalPtr->snakes == 100 and
animalPtr->aligaotr == 100 */

Within foo, I was hoping to be able to change the value of the object
in the structure.

For Example:

foo(???){ /* not sure what the parameters will be */

if(blah < blah2)
??? = 100 /* where ??? is the input to this function */

}

I was wondering would this work? If so, what would my ??? in my
example be? How would I be able to change the value of ??? ? If this
is not possible, what how should I go about doing this? Thanks in
advance for all replies.

#include <stdio.h>

struct S
{
int member1;
double member2;
};

void change(struct S *a)
{
a->member1 = 42;
a->member2 = 3.14;
}

int main()
{
struct S x = {0, 0.0};

printf("x.member1 == %d\n"
"x.member2 == %f\n\n", x.member1, x.member2);

change(&x);

printf("x.member1 == %d\n"
"x.member2 == %f\n\n", x.member1, x.member2);

return 0;
}


-Mike
 
S

Sheldon Simms

Hi everyone:
For Example:

struct reptile *animalPtr

It would be easier to help you if you showed at least some
sample declaration for struct reptile. Since you haven't I'll
provide one:

struct reptile
{
unsigned int snakes;
unsigned int aligator;
};

From here on out, I'll assume that this is your structure.
I was trying to pass a pointer to a structure to a function, where the
pointer is pointing to one of the members in the structure, is this
possible???
Within foo, I was hoping to be able to change the value of the object
in the structure.

The idea isn't bad, but you are confusing yourself about what
pointer is pointing where. Let's look at expressions:

animalPtr

this is a pointer to struct reptile

animalPtr->snakes

this is *not* a pointer. this the "snakes" member of struct reptile,
which is an unsigned int. The "arrow" operator works equivalently
to the following expression

(*animalPtr).snakes

let's break this up into parts:

*animalPtr

this is a struct reptile.

(*animalPtr)

this is still a struct reptile, the parentheses are
necessary when combining * and . to do the same work as ->

(*animalPtr).snakes

is the "snakes" member of struct reptile -- an unsigned int

Now to get to what you want to do:
foo(animalPtr->snakes);
foo(animalPtr->aligator);

Do you see now that you are not passing pointers to the
members of struct reptile; you are passing the members
themselves. This is easy to correct:

foo(&animalPtr->snakes);
^
foo(&animalPtr->aligator)
^
For Example:

foo(???){ /* not sure what the parameters will be */

Just use a parameter of the type you want to pass. In this case
that's

unsigned int *

void foo (unsigned int * xyz);

Notice that this function doesn't really have anything to do
with struct reptile, because the members of struct reptile are
just plain old unsigned ints like any others.
if(blah < blah2)
??? = 100 /* where ??? is the input to this function */

*xyz = 100;

As you would expeect.

Just to be complete, I will point out that you can also pass pointers
to a structure, if you wish. But then the function has access to the
entire structure, not just one member:

void bar (struct reptile * animalPtr)
{
animalPtr->snake = 50;
animalPtr->aligator = 150;
}

-Sheldon

p.s. It's spelled "alligator"
 
D

Dan Pop

In said:
I was trying to pass a pointer to a structure to a function, where the
pointer is pointing to one of the members in the structure, is this
possible???

For Example:

struct reptile *animalPtr

foo(animalPtr->snakes);
foo(animalPtr->aligator);

You're not passing any pointer to foo, you're passing the value of a
struct reptile member.
/* here now animalPtr->snakes == 100 and
animalPtr->aligaotr == 100 */

Within foo, I was hoping to be able to change the value of the object
in the structure.

For this purpose, foo really needs a pointer to the structure.
For Example:

foo(???){ /* not sure what the parameters will be */

if(blah < blah2)
??? = 100 /* where ??? is the input to this function */

}

I was wondering would this work? If so, what would my ??? in my
example be? How would I be able to change the value of ??? ? If this
is not possible, what how should I go about doing this? Thanks in
advance for all replies.

To make this to work, you must make up your mind about what member of the
struct reptile you want to set in the function. The ??? in the function
definition CANNOT be the same thing as the ??? in the assignment, if you
want to change anything in the struct whose address is passed to foo.

void foo(struct reptile *p)
{
if(blah < blah2) p -> snakes = 100;
}

This assumes that snakes is a member of structure reptile. And the
proper way of calling this function is:

struct reptile {
int snakes;
int alligator;
...
} animal = { ... };

foo(&animal);

You can't use pointers to structures without having the structures to
which they point in the first place.

OTOH, if what you want foo() to do is to change the value of an arbitrary
member in the struct, but whose type is known at compile time, you don't
need pointers to structs at all, a pointer to int is all that is needed
in our example:

void foo2(int *p)
{
if(blah < blah2) *p = 100;
}

and you have to pass it the address of the member whose value you want
to change:

foo2(&animal.snakes);

It is not clear from your example what exactly you want. Keep in mind
that -> dereferences a pointer to struct and also selects a struct
member, so the following two assignments are having exactly the same
effect:

struct reptile *p = &animal;
animal.snakes = 100;
p -> snakes = 100;

Dan
 
J

Jason

Thanks to all that replied. Sorry for the poor example. Hopefully,
this will be a better example and can you critique me if I'm doing
something wrong? I'm working from what I understood in everyone's
reply.

file 1:

struct reptile{
int alligator;
int snake;
}
___________________________

file 2:
#include <stdio.h>

extern struct reptile *r_ptr;
....

int change(struct s ptr*){
int temp = 8;

ptr->alligator = temp;
ptr->snake = temp;

return temp;
}

....
void main(){
int maintain = 0;

r_ptr->alligator = maintain;

maintain = change(&r_ptr);

/* here r_ptr->alligator == 8 */
}

Of course, it's more than this, but I'd just like to know the concept.
Thank you all for your help.
 
A

Al Bowers

Jason said:
Thanks to all that replied. Sorry for the poor example. Hopefully,
this will be a better example and can you critique me if I'm doing
something wrong? I'm working from what I understood in everyone's
reply.

file 1:

struct reptile{
int alligator;
int snake;
}
___________________________

file 2:
#include <stdio.h>

extern struct reptile *r_ptr;
...

int change(struct s ptr*){

That would be
int change(struct reptile *ptr) {
int temp = 8;

ptr->alligator = temp;
ptr->snake = temp;

return temp;
}

...
void main(){

int main(void) {
int maintain = 0;

r_ptr->alligator = maintain;

maintain = change(&r_ptr);

/* here r_ptr->alligator == 8 */
}

Of course, it's more than this, but I'd just like to know the concept.
Thank you all for your help.

You have not identified r_ptr. Hopefully, it is a struct reptile
pointer that points to storage.

I believe what you really want is to write a header file that declares
the struct and defines the function change. In the source file you
would include this header.

Example are the two files needed. 1 header and 1 source.

/***reptile.h
*** header file */
struct reptile
{
int alligator;
int snake;
};

void change(struct reptile *ptr,int alligator, int snake)
{
ptr->alligator = alligator;
ptr->snake = snake;
return;
}
/*************************End reptile.h**********************/

/*******test.c:
*******source file*/
#include <stdio.h>
#include "reptile.h"

int main(void)
{
struct reptile r_ptr = {0,0}; /* Initialize to zero */

change(&r_ptr,8,10);
/* here r_ptr->alligator == 8 */
printf("In r_ptr there are %d alligators and %d snakes\n"
"What a duo\n",r_ptr.alligator, r_ptr.snake);
return 0;
}
/****************************END*****************************/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top