为什么scanf中不需要地址运算符?

为什么scanf中不需要地址运算符?,c,pointers,scanf,C,Pointers,Scanf,为什么螺柱->名称.firstName不需要地址运算符? 但是&stud->studentid中需要address运算符 struct student { struct { char lastName[10]; char firstName[10]; } names; int studentid; }; int main() { struct student record; GetStudentName(&

为什么螺柱->名称.firstName不需要地址运算符? 但是&stud->studentid中需要address运算符

struct student {
    struct
    {
        char lastName[10];
        char firstName[10];
    } names;
    int studentid; 
};


int main()
{  
    struct student record;
    GetStudentName(&record);
    return 0;
}

void GetStudentName(struct student *stud)
{
    printf("Enter first name: ");
    scanf("%s", stud->names.firstName); //address operator not needed
    printf("Enter student id: ");
    scanf("%d", &stud->studentid);  //address operator needed
}

这不仅是不必要的,而且是不正确的。因为arrays1会自动转换为指针

以下

scanf("%s", stud->names.firstName);
相当于

scanf("%s", &stud->names.firstName[0]);
所以在这里使用运算符的地址是多余的,因为这两个表达式是等价的

像对待
%d“
格式说明符一样使用它
这是错误的

将是错误的,并且实际上会发生未定义的行为

注意:始终验证从
scanf()
返回的值



1也被称为数组名

可能重复的地址为什么取地址是错误的?因为
firstName
是一个数组,所以我希望它能工作。如果
firstName
是一个指针,它就不起作用了。@RolandIllig它相当于获取指针的地址。重要的区别在于指针算法会有所不同。
scanf("%s", &stud->names.firstName);