简体   繁体   English

我无法使用->访问结构数组的元素

[英]I cannot access the elements of an array of structure with ->

I'm having a little problem with a function which receives as a parameter an array of structures, the problem occurs when trying to access array elements with operator -> 我在函数中收到一个小问题,该函数接收结构数组作为参数,当尝试使用运算符->访问数组元素时出现问题

#include <stdio.h>

typedef struct{
    int order;
}record;

void entry(record*reg, size_t num_regs);

int main(void){
    record reg[10];
    entry(reg, sizeof reg / sizeof reg[0]);

    return 0;
}

void entry(record*reg, size_t num_regs){
    size_t i;

    for (i = 0; i < num_regs; i++){
        reg[i]->order = i;
        printf("\n order = %d", reg[i]->order);
    }
}

throws this error if you try to compile 如果尝试编译,则会引发此错误

*error #2140: Type error in argument 1 to 'ingreso'; expected 'registro * *' but found 'registro *'.*

because it throws this error and how to fix it? 因为它会引发此错误以及如何解决?

When you use [ ] for pointer you already have a data, so use . 当您将[]用作指针时,您已经有一个数据,因此请使用。 instead of -> 代替->

reg[i].order = i;

and the same for printf argument. 与printf参数相同。

I tried your code like this and whole of the array printed exactly true: 我像这样尝试了您的代码,并且整个数组的打印都完全正确:

for (i = 0; i < num_regs; i++){
    reg->order = i;
    printf("\n order = %d", reg->order);
}

You need to understand the difference between the -> and . 您需要了解->和之间的区别. operators when accessing data inside of a struct . struct内部访问数据时的运算符。

  • sa is just for when s is a struct and a is a member of s sa仅用于sstructas的成员时
  • sp->a is really just a shorthand for (*sp).a This is used when sp is a pointer-to- struct , and we want to dereference that pointer and access the struct 's data all in one step. sp->a实际上只是(*sp).a的简写形式。当sp是指向struct的指针时,可以使用它,而我们想取消引用该指针并一步访问struct的数据。

As VolAnd said , you are using reg[i]->order but you should really be using reg[i].order . 正如VolAnd所说,您正在使用reg[i]->order但实际上应该使用reg[i].order


If it's still unclear... 如果还不清楚...

With your entry() function, you are passing in an array of 10 record structs called reg . 使用entry()函数,您将传入10个称为reg记录structs的数组。 You do this by passing a pointer to the array's base address, so the function accepts a pointer of type record* . 您可以通过传递指向数组基地址的指针来执行此操作,因此该函数接受record*类型的指针。

The elements of the array are structs , not struct pointers , so you access them with reg[i].order , not reg[i]->order . 数组的元素是structs而不是struct指针 ,因此您可以使用reg[i].order而不是reg[i]->order访问它们。

Here you are passing address of array and storing / collecting is in pointer. 在这里,您正在传递数组的地址,并且存储/收集在指针中。 And as the pointer is pointing to array, if you use -> operator to access the structure element it will treat array as pointer and throws error as "error: invalid type argument of â->â (have ârecordâ)". 并且当指针指向数组时,如果使用->运算符访问结构元素,它将把数组视为指针,并将错误抛出为“错误:â->(具有'record')的无效类型参数”。

So you have to use (.) operator instade of (->) operator. 因此,您必须使用(->)运算符的(。)运算符。

for (i = 0; i < num_regs; i++)
{
    reg[i].order = i;
    printf("\n order = %d", reg[i].order);
}

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

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