473,804 Members | 3,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5187

"Jason" <ja****@aol.com > wrote in message
news:6d******** *************** ***@posting.goo gle.com...
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.membe r1 == %d\n"
"x.member2 == %f\n\n", x.member1, x.member2);

change(&x);

printf("x.membe r1 == %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).sn akes

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).sn akes

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(animalPt r->snakes);
foo(animalPt r->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.sn akes);

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.al ligator, r_ptr.snake);
return 0;
}
/*************** *************EN D************** ***************/

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.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
23655
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. help! thanks Taha
8
4084
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 Transitional//EN"> <html> <head> <style type="text/css"><!-- sub {vertical-align: text-bottom; font-size: smaller}
7
5807
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
2814
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
3795
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 that specifically lists the ID's of the adjacent controls. The second is to somehow put in some navigation information into the ID's themselves and then loop through them on key press to try to calculate which cell to go to next. Which is the...
1
10157
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 Key. The code below shows what I mean. How can I cure this? (excuse me for the line breaks) */
0
2927
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 I intercept the Arrow Keys when holding down the Shift Key? */
4
1618
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 goes back to 100, the arrow changes to "down". I'm not a statistical guru but I am sure there is a definition for this. I am getting my number variable from a sql server via a query.
4
6287
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 keyboard to move through the cells. I found some code and adapted it, and it works well now. Arrow keys move the cursor exactly to the correct cell. There are two things I can't do which I was hoping you guys would give me some help on. I am a...
0
9705
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2983
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.