Thread: Structs

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    37

    Structs

    Hi , could anyone instruct me how could I look at members of struct members? I mean if I have variable length as integer like int length , so it's easy to just write length , but if that variable is a struct member , lets assume that struct called A, then in order to get the length we say A.length and not just write length .. my problem is that Im not finding it easy to write A.length instead of length, so anyone have a good approach to tell me how could I look at member inside the struct in order to be comfortable with the syntax once I write A.length and not length .

    Exactly what happened that once I write A.length I say that It's not just a simple variable like length ..so that's why Im not finding it comfortable with because I have A. which still not make sense/sound for me.


    thanks for helpers.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,727
    The Teir family has a few members. One of the members is named Brian. So, to differentiate him from a person named Brian from another family, or with no family name, we call him Brian Teir.

    Do you find this to be a particularly difficult concept, Brian Teir? If not, A.length should be just as easy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    37
    Quote Originally Posted by laserlight View Post
    The Teir family has a few members. One of the members is named Brian. So, to differentiate him from a person named Brian from another family, or with no family name, we call him Brian Teir.

    Do you find this to be a particularly difficult concept, Brian Teir? If not, A.length should be just as easy.
    Youre awesome, so if I understand you I could assume that I have variable called Brian Teir? so in the same concept I have variable called A.length which it's length but the variable called A.length and not length ..... am I right?

    thanks alot

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    928
    Quote Originally Posted by Brian_Teir View Post
    I have variable called A.length which it's length but the variable called A.length and not length ..... am I right?
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    37
    Quote Originally Posted by john.c View Post
    I mean that variable length is in concept LENGTH , so in the same manner I could assume that variable A.length is like variable length which it's conceptually LENGTH .

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,968
    Are you equally confused about arrays?

    Let's say you wanted to store the dimensions of a box using an array.
    Code:
    int mybox[3];
    
    Which looks like this laid out in memory.
    +----------+
    | mybox[0] |
    +----------+
    | mybox[1] |
    +----------+
    | mybox[2] |
    +----------+
    Now set some sizes for our box.
    Code:
    // To keep our sanity, we assign specific indices of the array to be for a
    // particular purpose, so we give each one a name.
    #define LENGTH_INDEX 0
    #define WIDTH_INDEX  1
    #define HEIGHT_INDEX 2
    
    mybox[LENGTH_INDEX] = 22;
    mybox[WIDTH_INDEX]  = 33;
    mybox[HEIGHT_INDEX] = 44;
    Now do the same thing with a struct.
    Code:
    struct box {
      int length;
      int width;
      int height;
    };
    
    struct box mybox;
    Which looks like this laid out in memory.
    +--------+
    | length |
    +--------+
    | width  |
    +--------+
    | height |
    +--------+
    See the similarity?


    Now set some sizes for our box.
    Code:
    mybox.length = 22;
    mybox.width  = 33;
    mybox.height = 44;

    So the thing we call a length, in the thing we call a box has been written as either
    mybox[LENGTH_INDEX] = 22;
    or
    mybox.length = 22;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2019
    Posts
    37
    Quote Originally Posted by Salem View Post
    Are you equally confused about arrays?

    Let's say you wanted to store the dimensions of a box using an array.
    Code:
    int mybox[3];
    
    Which looks like this laid out in memory.
    +----------+
    | mybox[0] |
    +----------+
    | mybox[1] |
    +----------+
    | mybox[2] |
    +----------+
    Now set some sizes for our box.
    Code:
    // To keep our sanity, we assign specific indices of the array to be for a
    // particular purpose, so we give each one a name.
    #define LENGTH_INDEX 0
    #define WIDTH_INDEX  1
    #define HEIGHT_INDEX 2
    
    mybox[LENGTH_INDEX] = 22;
    mybox[WIDTH_INDEX]  = 33;
    mybox[HEIGHT_INDEX] = 44;
    Now do the same thing with a struct.
    Code:
    struct box {
      int length;
      int width;
      int height;
    };
    
    struct box mybox;
    Which looks like this laid out in memory.
    +--------+
    | length |
    +--------+
    | width  |
    +--------+
    | height |
    +--------+
    See the similarity?


    Now set some sizes for our box.
    Code:
    mybox.length = 22;
    mybox.width  = 33;
    mybox.height = 44;

    So the thing we call a length, in the thing we call a box has been written as either
    mybox[LENGTH_INDEX] = 22;
    or
    mybox.length = 22;


    thanks got it, appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-08-2013, 07:55 AM
  2. Typedef Structs inside Typdef structs
    By gremory in forum C Programming
    Replies: 21
    Last Post: 12-30-2011, 07:48 PM
  3. [ noob question ] Help with structs within structs
    By Riverfoot in forum C Programming
    Replies: 3
    Last Post: 04-26-2011, 07:24 PM
  4. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  5. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM

Tags for this Thread