reference of struct

This is a discussion on reference of struct within the C Programming forums, part of the General Programming Boards category; Hello How the set testinst[0] char and integer? What am I doing wrong? Code: struct test { char c; int ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    reference of struct

    Hello

    How the set testinst[0] char and integer?
    What am I doing wrong?

    Code:
    struct test {
      char c;
      int n
    };
    
    void somefunction(struct test *inst) {
      inst[0].c = 'a';
      inst[0].n = 5;
    }
    
    int main() {
      struct test testinst[20];
      somefunction(&testinst);
      return 0;
    }

    Many thanks for help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If somefunction expects an array as an argument, you should probably pass it an array. Note that testinst is an array, and &testinst is not. (This is why your compiler says "warning: passing arg 1 of 'somefunction' from incompatible pointer type".)

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by tabstop View Post
    If somefunction expects an array as an argument, you should probably pass it an array. Note that testinst is an array, and &testinst is not. (This is why your compiler says "warning: passing arg 1 of 'somefunction' from incompatible pointer type".)
    Code:
    void somefunction(struct test *inst[]) {
      inst[0]->c = 'a';
      inst[0]->n = 5;
    }
    Wont work. What is wrong with the code above?

    Thanks.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're also not passing the function an array of pointers.

    You want
    Code:
    void somefunction(struct test *inst);
    so that your function expects an array, and then you want to just pass the array to the function already:
    Code:
    somefunction(testinst);

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    It's still

    inst[0].c = 'a';
    inst[0].n = 5;

    This is not a pointer-to-pointer and index operator [] dereferences the pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Elysia View Post
    It's still

    inst[0].c = 'a';
    inst[0].n = 5;

    This is not a pointer-to-pointer and index operator [] dereferences the pointer.
    This wont compile..

    I get: error C2231: '.c' : left operand points to 'struct', use '->'

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by l2u View Post
    Hello

    How the set testinst[0] char and integer?
    What am I doing wrong?

    Code:
    struct test {
      char c;
      int n
    };
    
    void somefunction(struct test *inst) {
      inst[0].c = 'a';
      inst[0].n = 5;
    }
    
    int main() {
      struct test testinst[20];
      somefunction(&testinst);
      return 0;
    }

    Many thanks for help!
    > somefunction(&testinst);
    This should be:
    somefunction(testinst);

  8. #8
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by swoopy View Post
    > somefunction(&testinst);
    This should be:
    somefunction(testinst);
    Wouldnt this just pass the value?

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Wouldnt this just pass the value?
    What makes you think that?

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by l2u View Post
    Wouldnt this just pass the value?
    No, because you're passing an array, and arrays are always converted to a pointer to their first element in C/C++.
    If you put a & infront of it as you did, you're now passing a pointer to a pointer.

  11. #11
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by cpjust View Post
    No, because you're passing an array, and arrays are always converted to a pointer to their first element in C/C++.
    If you put a & infront of it as you did, you're now passing a pointer to a pointer.
    This will still crash the program:

    Code:
    void somefunction(struct test *inst[]) {
      inst[0].c = 'a';
      inst[0].n = 5;
    }
    
    int main() {
      struct test testinst[20];
      somefunction(testinst);
      return 0;
    }

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,593
    Code:
    void somefunction(struct test* inst) {
      inst[0].c = 'a';
      inst[0].n = 5;
    }
    
    int main() {
      struct test testinst[20];
      somefunction(testinst);
      return 0;
    }
    Try listening...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21