#1
  1. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13

    Question passing structs to a function


    I am having a problem accessing a struct in a function in ANSI C. I get four indentical errors that say, "request for member `abcarray' in something not a structure or union".

    Can you briefly look at this code and see if I am dereferencing my pointers properly?



    // struct define
    struct abc
    {
    int abcarray[4];
    };



    // function define
    void functionname(struct abc* xyz)
    {
    *xyz.abcarray[0] = 255; // this causes the errror
    *xyz.abcarray[1] = 255; // this causes the errror
    *xyz.abcarray[2] = 255; // this causes the errror
    *xyz.abcarray[3] = 255; // this causes the errror
    return;
    }
    Last edited by Doucette; April 23rd, 2003 at 01:39 PM.
    Matthew Doucette / Xona.com
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,222
    Rep Power
    2222
    Sometimes I think that a foreign language should be required for programmers.

    Think about what you are telling the computer to do:
    *xyz.abcarray[0] = 255; // this causes the errror

    You are telling the computer to dereference the entire expression, xyz.abcarray[0] -- specifically the integer value already contained in abcarray[0] -- and to store a value of 255 at that location being pointed to. It cannot recognize xyz as a struct because xyz was declared as a pointer to a struct, but not as a struct itself.

    In order to tell it that you want to dereference xyz and access the element abcarray[0] within it, you could change that line to:
    (*xyz).abcarray[0] = 255;
    Now, that way it knows that you are talking about dereferencing xyz, not the entire left expression.

    I mention that construction above because it can be handy at times in C++ (especially in MFC), but a far better and much more conventional way of expressing it would be:
    xyz->abcarray[0] = 255;
    Last edited by dwise1_aol; April 23rd, 2003 at 02:23 PM.
  4. #3
  5. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13
    Thanks. Would you believe I already thought of that but used { } instead of ( )? :rolleyes: You can tell that I am not use to programming in C...

    Just tested it, it works. Thanks a million.

    Originally posted by dwise1_aol
    Think about what you are telling the computer to do:
    *xyz.abcarray[0] = 255; // this causes the errror

    You are telling the computer to dereference the entire expression, xyz.abcarray[0] -- specifically the integer value already contained in abcarray[0] -- and to store a value of 255 at that location being pointed to.
    Ouch. That could be deadly to my server. I ran this program about 10 times too with that bad code. [edit: nevermind, I just remembered that it did not compile with the bad code, so when I was running it it was an early copy of the program.]
    Last edited by Doucette; April 24th, 2003 at 06:18 AM.
    Matthew Doucette / Xona.com
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,222
    Rep Power
    2222
    Some languages, like Ada, are written to protect the programmer from himself (actually, Ada was somewhat paranoid about that, as I recall). Then there's C, written by programmers to do what they wanted to do, that figures that you know what you're doing and if you wrote it that way, that's what you wanted to have happen.

    There was a classic C book, which I unfortunately did not get, called something like "Shooting Yourself in the Foot with Your Own Rope". It spawned a serious of computer jokes which described how various languages would accomplish the same effect; eg, BASIC: you shoot your foot repeatedly with a water pistol until it gets waterlogged. One page with such a collection is at http://burks.brighton.ac.uk/burks/language/shoot.htm .

    It's all part of learning and using C. And the main reason I can spot many mistakes is because I've committed them myself.

IMN logo majestic logo threadwatch logo seochat tools logo