-> The former is a pointer, and the former is a structure variable
The a->b
first meaning is (*a).b
, so they are different, but indeed ->
can implemented with and*
without a single operator. .
Well, I'm saying that modern standardized C semantics ->
can be implemented *
with .
a combination of and .
Early C had semantics that differed from modern C for a while.
Students who have a little knowledge of assembly may know that from the perspective of machine code and assembly, there are no variables, no such things as struct, only registers and a large array called memory.
Struct variables use . to access the members of the structure as follows:
#include<stdio.h>
#include <malloc.h>
struct stu{
int age;
stu* next;
};
int main(){
stu s1;
s1.age =18;
return 0;
}
A pointer to a structure uses -> to access the members of the structure it points to, such as:
#include<stdio.h>
#include <malloc.h>
struct stu{
int age;
stu* next;
};
int main(){
stu *phead = (stu*)malloc(sizeof(stu));//测试代码
phead->age=18;
phead->next = NULL;
stu* p = phead;
return 0;
}
To put it simply:
1. A->a means that A is a pointer to a structure
2. Aa means that A is a structure
3. A->a is equivalent to (*A).a
4. For AB, A is an object or structure;
5. For A->B, A is a pointer, -> is member extraction, A->B is to extract member B in A, and A can only be a pointer to a class, structure, or union;
6. (*a).b is equivalent to a->b. "." is generally read as "of"; "->" is generally read as "of the structure pointed to". That is to say, in the structure, operator -> is the combination of operator * and operator.
7. "->" is a member operator pointing to a structure. "." is a breakpoint symbol, not an operator.
8. "->" points to the first address of the structure or object. "." points to a structure or an object.
9. The purpose of "->" is to use a pointer to access the members of the structure or object. The use of "." is to use a pointer to access a structure or object.