?
Solved

Accessing structures in functions in C++

Posted on 2000-03-20
6
Medium Priority
?
330 Views
Last Modified: 2010-04-02
I have some functions that take in structures, amongst other things.
eg.
int winapi rectangle(SIZES *pInfo);

...where SIZES is a structure. How can I assign values to the variables of the Structure SIZES? I'v tried creating an object of type SIZES and assigning values using the (.)
 SIZES MySizes;  //create a Sizes object
*mySizesPtr=&mySizes, //pointer       &mySizesRef=mySizes;  //reference

mySizes.version = 0x03000000;

But I'm not sure that this is assigning the values to the right place.
Pls can i have a simple example of how to do this correctly. Pls help!

0
Comment
Question by:nicolac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 2

Accepted Solution

by:
xLs earned 200 total points
ID: 2638436
example
int winapi rectangle(SIZES* pInfo)
{
   pInfo->version = 0x0300000;
 SIZES s = *pInfo; // example of copy
}

in method:
{

 SIZES mySize;
 rectangle(&mySize); // '&' since rectangle needs a pointer of SIZES.
 printf("%d", mySize.version);
}



e.g if its a pointer to an instance of SIZES you use -> to assing or extract value or '*' as prefix.

if it is not a pointer you use '.'.


what you did
SIZES MySizes; //MySizes is not a pointer

*mySizesPtr=&mySizes;
// &mySize is the pointer of mySize
// *mySize is invalid because mySize is not a pointer

think of it this way:

*mySize lets you access whatever the pointer holds (its the mySizes instance).

&mySize gives you the pointer of the instance mySize


this is quite anoying to explain but ehm. comment if you need better explaination :)
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2638623
Two ways...

int winapi rectangle(SIZES *pInfo) {
  pInfo->xxxx = yyy;
}

or

int winapi rectangle(SIZES *pInfo) {
  SIZES& sizes = *pInfo;
  sizes.xxxx = yyyy;
}

The first accesses directly from the pointer with ->

The second uses a reference, and you can then use '.'

Both of the above will change the thing pointed to by pInfo.

If you take a COPY of the thing pointed to by pInfo and change that, then the original is unchanged.



0
 

Expert Comment

by:kaushikg
ID: 2639673
Both person has replied correctly but RONSLOW's explanation is easy to understand.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Kelvin_King
ID: 2639845
what does the -> mean ?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2639925
p->x is just shorthand for

(*p).x

ie. it dereferences the pointer to a struct and then gets the member of the struct.

or simpler...

ie. it does a '.' on the object pointed to by p

pointers are a tricky C concept for many to grasp.

even trickier for some is C++ references, which is what I used in the other example.

0
 

Author Comment

by:nicolac
ID: 2655601
Thanks, I understand now :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question