I'm trying to find out how can I use a pointer to access an array of struct
我试图找出如何使用指针访问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
//这是我的问题,如何使用ptr1访问结构数组的下一个元素?我试着编写这行代码,但出现了错误
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,
我必须将ptr1声明为指针数组吗?如果我不知道struct数组的大小我怎么能声明一个指针数组,
another question here, what should be the size of the pointer? sizeof(ptr1)? I've tried this line of code
这里的另一个问题是,指针的大小应该是多少?的sizeof(ptr1的)?我试过这行代码
printf("size of ptr1 = %i",sizeof(ptr1));
and the answer was 4 ?!!HOW COME?
答案是4?!!怎么样?
thanks
3 个解决方案
#1
2
If you want to access the second element of the array of struct type then just increment increment pointer Like:
如果要访问struct类型数组的第二个元素,则只需增加增量指针Like:
ptr1++;
now pointer will point to the second element of array of struct type.
now指针将指向struct类型数组的第二个元素。
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的大小。
#2
2
If you want to use ptr1
to access the next, i.e. 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平台上指针的典型大小
#3
1
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 *
.
通过使用数组索引表示法[],您可以有效地取消引用指针。例如,ptr1 [1]可以写成*((ptr1)+(1))。换句话说,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. If you want to use the ->
notation, you can instead write (ptr1 + 1)->x
. The ->
operator dereferences the pointer.
因为它不是指针,所以必须使用。运算符访问结构的元素。例如,将代码更改为ptr1 [1] .x可以正常工作。如果要使用 - >表示法,则可以改写(ptr1 + 1) - > x。 - >运算符取消引用指针。
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. 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.
为了回答您的其他问题,sizeof ptr1为4,因为这是您机器上指针的大小。我的例如打印8因为它是64位并且每字节有8位。我猜你有一个运行的32位操作系统,因此它打印4,因为它是32位,每字节8位。
#1
2
If you want to access the second element of the array of struct type then just increment increment pointer Like:
如果要访问struct类型数组的第二个元素,则只需增加增量指针Like:
ptr1++;
now pointer will point to the second element of array of struct type.
now指针将指向struct类型数组的第二个元素。
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的大小。
#2
2
If you want to use ptr1
to access the next, i.e. 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平台上指针的典型大小
#3
1
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 *
.
通过使用数组索引表示法[],您可以有效地取消引用指针。例如,ptr1 [1]可以写成*((ptr1)+(1))。换句话说,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. If you want to use the ->
notation, you can instead write (ptr1 + 1)->x
. The ->
operator dereferences the pointer.
因为它不是指针,所以必须使用。运算符访问结构的元素。例如,将代码更改为ptr1 [1] .x可以正常工作。如果要使用 - >表示法,则可以改写(ptr1 + 1) - > x。 - >运算符取消引用指针。
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. 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.
为了回答您的其他问题,sizeof ptr1为4,因为这是您机器上指针的大小。我的例如打印8因为它是64位并且每字节有8位。我猜你有一个运行的32位操作系统,因此它打印4,因为它是32位,每字节8位。