我不明白何时使用箭头以及何时使用点

时间:2019-01-30 22:02:19

标签: c

我不知道何时使用箭头以及何时使用点。

例如,

void scrivi_file(FILE *output, anagrafe *vect, anagrafe **max, int dim_vect){
    int i;
    *max = malloc(1 * sizeof(anagrafe));
    max[0] = &vect[0];
    for(i=1; i<dim_vect; i++)
        if(vect[i].media > max[0]->media)
            max[0] = &vect[i];
    fprintf(output, "%s %s %f", max[0]->cognome, max[0]->nome, (*max)[0].media);
}

为什么最后一个最大值是点,前两个是箭头?我不明白星号和&也令人困惑。

2 个答案:

答案 0 :(得分:0)

点运算符用于访问结构的成员。但是,max是指向结构的指针。箭头运算符等效于取消引用指针,然后使用点运算符。这些语句是相同的:

max->nome
(*max).media

和号用于检索变量的地址。在声明变量时使用星号表示已声明的变量是一个指针,在表达式中使用它来取消引用指针:

int x = 5;
int y;
int * pointer_to_int;
pointer_to_int = &x;
y = *x; // y is now equal to 5

答案 1 :(得分:0)

箭头和点运算符均用于访问<pdf-viewer [src]="pdfSrc" [render-text]="false" [original-size]="true" [fit-to-page]="true" style="display: block;"></pdf-viewer> 的成员。但是,如果struct变量是指针,则使用箭头运算符,否则使用点运算符。

struct