structs like pointers?

This is a discussion on structs like pointers? within the C Programming forums, part of the General Programming Boards category; is it possible to use structs like pointers? i mean with a array and pointers you could do char temp[] ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    structs like pointers?

    is it possible to use structs like pointers? i mean with a array and pointers you could do

    char temp[] = "testing";
    char *spot= temp;

    and then spot was temp. can you do that with structs, is it always one block of memory.

    how could i take something like this, use something like spot from above and get one of the vars?

    struct test {
    int a;
    int b;
    };

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    Code:
    typedef struct
    {
       int a,b;
    } test;
    ...
    test t[] = {{1,2},{3,4},{4,5}};
    test *p = t;
    printf("%d,%d",p[1].a,p[1].b);
    Output: "3,4"

    gg

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    i dont get it, you passing 3 sets of 2 when theres only 2 ints. and whats this p[1] stuff, wasnt one a value for the inits?

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    He's actually making an array of structs. Thus p[1] references the first element in the array. For referencing a single struct, here's an example.

    Code:
    typedef struct
    {
       int a,b;
    } test;
    ...
    test t = { 1, 2 };
    test *p = &t;
    printf( "t.a=%d, p->a=%d", t.a, p->a );
    This would output 't.a=1, p->a=1'.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    >Thus p[1] references the first element in the array

    p[1] = the second element of the array.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    169
    Also if you have a pointer to an array of structures the pointer can be incremented to the next structure pointer with P++.

    Code:
    #include <stdio.h>
    
    typedef struct
    {
       int a,b;
    } test;
    
    test t[] = {{1,2},{3,4},{4,5}};
    
    int main()
    {
       test *p = t;
    
       for(int i = 0; i < 3; i++)
       {
          printf("p++ = %d,%d\n",p->a, p->b); 
          p++;
       }
    
    //is the same as:
    
       p = t;
       for(i = 0; i < 3; i++)
       { 
          printf("P[i] = %d,%d\n",p[i].a, p[i].b); 
       }
       return 0;
    }
    Last edited by Scarlet7; 03-14-2003 at 02:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault with structs and char pointers
    By Keybone in forum C++ Programming
    Replies: 20
    Last Post: 01-17-2004, 12:36 PM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. pointers and structs
    By alexir in forum C Programming
    Replies: 0
    Last Post: 10-20-2001, 01:28 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

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