简体   繁体   中英

How to use a pointer with an array of struct?

I'm trying to find out how can I use a pointer to access an array of struct

#include<stdio.h>

struct p
{
int x;
char y;
};


int main()
{
struct p p1[]={1,92,3,94,5,96};
struct p *ptr1=p1;

printf("size of p1 = %i\n",sizeof(p1));

//here is my question, how can I use ptr1 to access the next element of the array
//of the struct?
printf("%i %c\n",ptr1->x,ptr1->y);      

int x=(sizeof(p1)/3);
if(x == sizeof(int)+sizeof(char))
    printf("%d\n",ptr1->x);
else
    printf("falsen\n");
return 0;
}

//here is my question, how can I use ptr1 to access the next element of the array of the struct? I tried to write this line of code but an error appeared

printf("%i %c\n",ptr1[0]->x,ptr1[1]->y);

the error was

invalid type of argument '->'(have'struct p')

do I have to declare ptr1 as an array of pointers? and if I don't know the size of the array of struct how can I declare an array of pointers,

another question here, what should be the size of the pointer? sizeof(ptr1)? I've tried this line of code

printf("size of ptr1 = %i",sizeof(ptr1));

and the answer was 4 ?!!HOW COME?

thanks

If you want to use ptr1 to access the next, ie the second, element in the struct just use array notation on the value

int secondX = ptr1[1].x
char secondY = ptr1[1].y

The sizeof(ptr1) expression returns 4 because that is the typical size of pointers on x86 platforms

If you want to access the second element of the array of struct type then just increment increment pointer Like:

  ptr1++;

now pointer will point to the second element of array of struct type.

and your second answer is:

pointer holds the address of the variable and address of the variable is considered as integer value. So based on the machine pointer size is also as integer. Check in your machine the integer size should be 4 that's why it is showing you size of the pointer 4.

By using the array index notation [ ] , you effectively dereference the pointer. For example, ptr1[1] can be written *((ptr1)+(1)) . In other words, ptr1[1] is of type struct p , not struct p * .

Because it is not a pointer, you must use the . operator to access the elements of the structure. Changing your code to ptr1[1].x works for example. If you want to use the -> notation, you can instead write (ptr1 + 1)->x . The -> operator dereferences the pointer.

If you wanted, you could use (*(ptr1 + 1)).x to accomplish the same thing, dereferencing the pointer more explicitly, but this may prevent some compilers from optimising your code and also is less readable (a number of CPUs allow for indexed access such that ptr1[1] may only require 1 instruction whereas *(ptr1 + 1) might require 3 instructions: a load operation for ptr , an addition operation to do +1, and a dereference operation.)

In response to your other question, sizeof ptr1 is 4 because that is the size of the pointer on your machine. Mine for example prints 8 because it is 64-bit and has 8 bits per byte. I'm guessing you have a 32-bit OS running, so it prints 4 because it is 32-bit with 8 bits per byte.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM