Accessing structures in functions in C++

Posted on 2000-03-20
Medium Priority
Last Modified: 2010-04-02
I have some functions that take in structures, amongst other things.
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!

Question by:nicolac

Accepted Solution

xLs earned 200 total points
ID: 2638436
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

// &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 :)
LVL 10

Expert Comment

ID: 2638623
Two ways...

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


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.


Expert Comment

ID: 2639673
Both person has replied correctly but RONSLOW's explanation is easy to understand.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

LVL 13

Expert Comment

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

Expert Comment

ID: 2639925
p->x is just shorthand for


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.


Author Comment

ID: 2655601
Thanks, I understand now :)

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

619 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