473,395 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

arrow operator

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
Nov 13 '05 #1
5 5167

"Jason" <ja****@aol.com> wrote in message
news:6d**************************@posting.google.c om...
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
Nov 13 '05 #2
On Wed, 29 Oct 2003 07:24:28 -0800, Jason wrote:
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"

Nov 13 '05 #3
In <6d**************************@posting.google.com > ja****@aol.com (Jason) writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #4
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.
Nov 13 '05 #5


Jason wrote:
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******************* **********/

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ataha | last post by:
Is it possible to display an up-arrow in a Label, TextBox, ListView, DataGrid, etc without using a graphic ? I keep trying the characters that should represent arrows and im getting little squares....
8
by: Harry Haller | last post by:
The right arrow in IE is displayed aligned to the bottom of the line. Is there any way I can display it aligned vertially in the middle? Example: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01...
7
by: Seash | last post by:
Hi friends , here is the sample code private void txtbox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(e.KeyChar == 13) //enter { txtbox2.Focus(); }
11
by: Rlrcstr | last post by:
How can you detect when an arrow key gets pressed? Doesn't seem to trigger a KeyPress or KeyDown event. Thanks. Jerry
2
by: PJ6 | last post by:
When I hit an arrow key, I want to be able to change the focus from cell to cell just like in Excel. I see two ways of doing this. First, emitting in script a function call in each cell on key down...
1
by: Martijn Mulder | last post by:
/* I have problems detecting the Arrow Keys on a User Control. A control derived from System.Windows.Forms.Control neglects 'bare' Arrow Keys but does react on the combination <Altor <Ctrl+ Arrow...
0
by: Martijn Mulder | last post by:
/* I override IsInputKey() to direct the Arrow Keys (Cursor Keys) to my custom System.Windows.Forms.Control. But, holding down the Shift-Key prevents the Arrow Keys from coming through. How can...
4
by: Techhead | last post by:
How can I implement a rising/falling trend arrow based on a number that may rise or fall. For example, if the number (variable) is 100 and changes to 101, the "arrow" changes to "up", if the number...
4
by: beary | last post by:
Hi Being tested using FF 3 on WAMP server. I have spent a number of hours trying to figure this out myself. I have a html form using table cells and had a request to enable the arrow keys on a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.