简体   繁体   English

如何使用允许我访问结构域的结构测试-> C运算符?

[英]How can I test the -> C operator using a struct that allows me to access a struct's field?

I'm trying to test the -> operator, but I can't seem to do that, because when I execute the following program and feed the input with a stream, the program stops working. 我正在尝试测试->运算符,但似乎无法做到这一点,因为当我执行以下程序并向输入流提供输入时,该程序将停止工作。

Note1: I get a warning before compiling, it says that: 注意1:我在编译前收到警告,它说:

format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=] 格式'%s'期望的参数类型为'char *',但是参数2的类型为'int'[-Wformat =]

Note2: if I omit the line printf("%s\\n", *(&home1)->name ) , it works just fine, and actually prints whatever I wrote. 注意2:如果我省略了printf("%s\\n", *(&home1)->name ) ,它就可以正常工作,并且实际上可以打印我写的任何内容。

#include <stdio.h>
#include <string.h>

typedef struct home_type {
    char name[30] ;
}home;

int main (void) {
    home home1 ;
    char name[30] ;
    scanf ("%[^\n]%*c", name);
    strcpy( home1.name, name) ;
    printf("%s\n", home1.name ) ;
    printf("%s\n", *(&home1)->name ) ;
    return 0 ;
}

Remove * and it works. 删除* Your code *(&home1)->name is analogous to *(home1.name) eg instead of passing the pointer to the first character, it passes the value of the first char in name ; 您的代码*(&home1)->name*(home1.name)类似,例如,它没有将指针传递给第一个字符,而是传递了name第一个char的值; due to default argument promotions, that value is converted into an int . 由于默认的参数提升,该值将转换为int

Thus: 从而:

printf("%s\n", (&home1)->name );

should work; 应该管用; however you don't need -> here; 但是,您不需要->在这里; -> is now just a shortcut for using a pointer-to-structs more conveniently; ->现在只是更方便地使用指向结构的指针的快捷方式; ie (*ptr).name into a more palatable ptr->name ; (*ptr).name成为更可口的ptr->name as home is already a struct and not just a pointer to struct, you should use the . 由于home已经是一个struct ,而不仅仅是指向该结构的指针,因此您应该使用. instead. 代替。

You just have to drop the * (ie not dereference): 您只需要删除* (即不取消引用):

printf("%s\n", (&home1)->name );

The member name is an array which gets converted into a pointer ( char* ) when passing to printf() . 成员name是一个数组,当传递给printf()时,该数组将转换为指针( char* printf() However, when you dereference it, it's just a single char that you pass. 但是,当取消引用它时,它只是传递的一个char Obviously, it doesn't match with what printf() expects for the format %s . 显然,它与printf()%s格式的期望不匹配。

See: What is array decaying? 请参阅: 什么是阵列衰减?

Operators -> and . 运算符->. are interchangeable: 可以互换:

  • obj.field is the same as (&obj)->field obj.field(&obj)->field
  • ptr->field is the same as (*ptr).field ptr->field(*ptr).field ptr->field相同

Note that you have added an asterisk to the result of (&home1)->name , which produces a char . 请注意,您已经在(&home1)->name的结果中添加了一个星号,这会产生一个char Since printf is a variable-argument function, char is promoted to int during the call, explaining the warning. 由于printf是变量参数函数,因此在调用过程中char被提升为int ,从而解释了警告。

When you pass an int for a parameter expecting a pointer, undefined behavior happens; 当为需要指针的参数传递int值时,会发生未定义的行为; in your case, the program crashes. 在您的情况下,程序崩溃。 Removing the dereference operator * will fix this problem. 删除取消引用运算符*将解决此问题。

(&home1)->name is the member array. (&home1)->name是成员数组。 *(&home1)->name is a dereference on the member array which because of array decay is equivalent to (&home1)->name[0] . *(&home1)->name是成员数组上的取消引用,由于数组衰减,该引用等效于(&home1)->name[0] This has type char . 此类型为char Passing a char through the ... of a variadic function such as printf promotes it to int ( ... causes default argument promotions to apply). 传递一个char通过...一个可变参数函数如printf促进它int...使默认参数促销活动适用)。

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

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