# structs like pointers?

• 03-13-2003
mart_man00
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;
};
• 03-13-2003
Codeplug
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
• 03-13-2003
mart_man00
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?
• 03-13-2003
XSquared
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'.
• 03-14-2003
Scarlet7
>Thus p[1] references the first element in the array

p[1] = the second element of the array.
• 03-14-2003
Scarlet7
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; }```