Alert icon
We're changing our privacy policy. This stuff matters.  Learn more  Dismiss

C Programming Tutorial # 32 - Passing Structure Pointers to Functions [HD]

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
6,845
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Apr 19, 2010

Visit http://www.CPlusPlusTutor.com
In this tutorial we learn to pass structures to functions by reference.

This tutorial is part of the following series:
http://www.youtube.com/view_play_list?p=CB9F975ECF01953C

  • likes, 0 dislikes

Link to this comment:

Share to:

Uploader Comments (Learnorama)

  • And yes, my question is still, 'why' don't we need to? I assume this different to arrays where the name of the array contained the address of the start of the element and so hence we did not need to use the REFERENCE operator, but with in this case, the DEFERENCE operator. Is this correct?

    Yes, as you can see, pointers and such that when used, really do confuse me.

  • @itsdannyftw Answer to the question "why don't we need to?" is that this is in the way -> operator is defined. When you have a pointer to structure you can access its members using -> operator. If you have the structure itself then you use . (dot) to access the members. Now you can also write something like (*pRect).length instead of rect->length and that will work too. Why? Because *pRect represents the structure itself, and hence you can access its members with a . (dot). Cool huh!

  • Ok, lets just create a function that changes the value of those structure member variables.

    void Change(struct Price_Struct *price)

    { price->Apple = 8; price->Banana = 16;

    }

    So this is fine? Hence, no deference operator had to be used?

    I'll continue further in the next comment.

  • @itsdannyftw That's correct.

  • more videos plz

  • @arghyainfo Coming up ;-)

see all

All Comments (15)

Sign In or Sign Up now to post a comment!
  • @ronswayoflife

    Cheers bro!! After 1 year, I would sure love to get back into programming!! I'm so ponderous at this stuff!

  • @itsdannyftw

    You can't do *pRect.Length because of operator precedence. The "." operator has higher precedence than " * ".

    To avoid compile errors you would have to use explicit parentheses (*pRect).Length

  • @Learnorama

    Ah, very cool! Just wondering though, why do you need the brackets around it? Why couldn't you do *pRect.Length?

  • Oh, I forgot to print it out in main. So, if we successfully did it and wanted to print out the value of the changed price (the main local variables will also change because we used a pointer as a dummy argument in the function), we would do (in C++):

    cout << "The price of Apple had been changed to: " << Price.Apple;

    cout << "The price of Banana had been changed to: " << Price.Banana;

    The values printed out in console would be as follows:

    8

    16

    This should be correct, hopefully gets cleared up

  • Hence the "->" connotation conveniently removes the rule usage of using the deference operator? Or am I misunderstanding this entire logic? I assume it's the same concept along with when assigning values to the member variables using functions as well!

    So, lets say we have a structure:

    struct Price_Struct {

    int Apple = 2;

    int Banana = 4;

    }

    In main, we declare it as: Price_Struct Price

    I will continue this in the next comment.

  • Since the "->" is a connotation to be used when you want a pointer to access a member variable of a structure, does this mean that you do not have to use the deference operator in advance? So, you do not have to (or you're not even supposed to???) do this:

    result = *rect->length * *rect->width

    Basically, my logic is that when you want to access a value (or a member variable (in this case, length & width), that is being pointed by a pointer, you would have to use the deference operator.

Loading...

Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more