Home > Back-end >  C language structure pointer
C language structure pointer

Time:09-30


#include
Struct student
{
Int no;
Char name [20].
int year;
Struct student * next;
};

Struct student stu1={1, "1", 23};
Struct student stu2={2, "a second", 25};
Struct student stu3={3, "zhang", 21};

Int main ()
{
Struct student * p, head;
Head. The next=& amp; Stu1;
Stu1. Next=& amp; Stu2;
Stu2. Next=& amp; Stu3;
Stu3. Next=NULL;

For (p=head. The next; P!=NULL; P=p - & gt; Next)
{
Printf (" % d % d % s \ \ t t \ t \ n ", p - & gt; No, p - & gt; The name, p - & gt; Year);
}

Getchar ();
return 0;
}

This is a structure pointer code, code I don't know why p - & gt; Next this statement can make move to the next node p, p is a pointer to structure, next is a pointer to a variable name column, that is why the pointer to a pointer to another can make pointer

CodePudding user response:

Put the pointer is what? Address, referred to in the p element, which is stored in the next pointer address assigned to p, is equivalent to p moves, for example, for example, if you stand on the car of the train, moved to the next compartment beneath your feet, is equivalent to you move back, but the metaphor a little ghost livestock

CodePudding user response:

Because you have been defined before the good points to the,

CodePudding user response:

This is a linked list,
Each structure holds the address of a structure,
List need a head, head is simplified as here and chain table structure is the same,
You find stu123 were initialized, but the head is not initialized,
. In fact, the program USES only the head next as the head, the other members of the structure has not been used,
General in the linked list to set up a chain of header alone, this simplifies the
This is the first place, a beginner of

Struct student * p
P is changing when traversing the list, so you want to define a separate pointer p
P=head. Next
P is actually the address of the first construction
That is to say, in this list, traverse is starting from the head
This is the place where plagued the building Lord,

In the traversal of the past, for example, the for loop
There is a control variable, control loop,
And list traversal, generally use this index in the list of a whole, there is an index also do not call list, called an array,
List is done by its own traversal,
Every structure, that is, save the address of a structure under,
In turn, iterate over all structure,

CodePudding user response:

Participated in the games,
I said, pointing to (stand) in the top left corner of your
: look at this row go really neat (pointer)
Is coming: the 3 class 4 old classmates, they... Class (pointer)
: 3 old classmates was below to enter the stadium, they... Grade (pointer)

Obviously, I was pointed at the upper left corner of your
Is the location of the point to is the same,

But the type is different,

When I need to point to the next row, (row pointer + +)
When I need to point to the next class, then, (+ + class pointer)
.

CodePudding user response:

references of the dream life (4th floor response:
participated in the games,
I said, pointing to (stand) in the top left corner of your
: look at this row go really neat (pointer)
Is coming: the 3 class 4 old classmates, they... Class (pointer)
: 3 old classmates was below to enter the stadium, they... Grade (pointer)

Obviously, I was pointed at the upper left corner of your
Is the location of the point to is the same,

But the type is different,

When I need to point to the next row, (row pointer + +)
When I need to point to the next class, then, (+ + class pointer)
.

Finished to see, a bit irrelevant answer, count me in

CodePudding user response:

p-> Next is the pointer variable, deposit is the address of the next node; P=p - & gt; next; Is to the next node address assigned to p, they point to the next node p, so with no p to visit the next node, the name, the year is no problem,

Year to age may some ~ code more readable
  • Related