我已经读过,如果我们有一个指向结构类型对象的指针,我们就不能通过使用星号(*)访问对象字段,但我们需要使用它 - >(如箭头符号).
现在虽然我读到它但我不明白为什么我们不能使用星号,我会尝试提供一个代码示例和一个图片,说明我如何在内存中可视化结构,以帮助您了解阻止我理解的内容这个话题.
好的,所以这是我们的代码:
#include <stdio.h>
typedef struct MyStruct
{
char c1;
char c2;
} MyStruct;
int main()
{
MyStruct s1; // Let's assume s1's address is 0x34A
s1.c1 = 'A';
s1.c2 = 'B';
printf("s1 c1 is %c and c2 is %c.\n", s1.c1, s1.c2);
MyStruct *p = &s1; // Declaring our pointer to point to s1 address, so p points to 0x34A
printf("s1 c1 is %c and c2 is %c.\n", p->c1, p->c2); // I know this is the valid …Run Code Online (Sandbox Code Playgroud)