简体   繁体   English

如何使用带有结构体数组的指针?

[英]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? //这是我的问题,如何使用ptr1访问结构数组的下一个元素? 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? 我必须声明ptr1为指针数组吗? and if I don't know the size of the array of struct how can I declare an array of pointers, 如果我不知道struct数组的大小,该如何声明一个指针数组,

another question here, what should be the size of the pointer? 另一个问题,指针的大小应该是多少? sizeof(ptr1)? 的sizeof(ptr1的)? I've tried this line of code 我已经尝试过这行代码

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

and the answer was 4 ?!!HOW COME? 答案是4?!!怎么来?

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 如果要使用ptr1访问结构中的下一个(即第二个)元素,则只需对值使用数组符号

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 sizeof(ptr1)表达式返回4因为这是x86平台上指针的典型大小

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. 检入计算机的整数大小应为4,这就是显示指针大小4的原因。

By using the array index notation [ ] , you effectively dereference the pointer. 通过使用数组索引符号[ ] ,可以有效地取消引用指针。 For example, ptr1[1] can be written *((ptr1)+(1)) . 例如,可以将ptr1[1]写入*((ptr1)+(1)) In other words, ptr1[1] is of type struct p , not struct p * . 换句话说, ptr1[1]的类型为struct p ,而不是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. 例如,将代码更改为ptr1[1].x If you want to use the -> notation, you can instead write (ptr1 + 1)->x . 如果要使用->表示法,可以改写(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.) 如果需要,可以使用(*(ptr1 + 1)).x完成同一件事,更明确地引用指针,但这可能会阻止某些编译器优化代码,并且可读性较低(许多CPU允许用于索引访问,这样ptr1[1]可能只需要1条指令,而*(ptr1 + 1)可能需要3条指令: ptr的装入操作,做+1的加法操作和解引用操作。)

In response to your other question, sizeof ptr1 is 4 because that is the size of the pointer on your machine. 在回答您的另一个问题时, sizeof ptr1为4,因为那是您计算机上指针的大小。 Mine for example prints 8 because it is 64-bit and has 8 bits per byte. 例如,我的系统会打印8,因为它是64位的,每个字节有8位。 I'm guessing you have a 32-bit OS running, so it prints 4 because it is 32-bit with 8 bits per byte. 我猜您正在运行32位OS,因此它打印4,因为它是32位,每字节8位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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