|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
若想访问结构体指针变量的非首个内部成员的地址,除了使用&p->mumber3 这种方式,还有什么方法?
例如在链表那节的一个练习:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student) //student结构的大小
struct student *creat(); //创建链表
void print(struct student *head); //打印链表
struct student
{
int num;
float score;
struct student *next;
};
int n; //n是全局变量
void main()
{
struct student *stu;
stu = creat( );
print( stu );
printf("\n\n");
system("PAUSE");
}
struct student *creat()
{
struct student *p1,*p2,*head;
p1 = p2 = (struct student *)malloc(LEN);
printf("Please enter the num :");
scanf_s("%d", &p1->num );
printf("Please enter the score :");
scanf_s("%f", &p1->score );
head = NULL;
for (n = 0; p1->num != 0 ;n++ )
{
if (0 == n)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("Please enter the num :");
scanf_s("%d", &p1->num );
printf("Please enter the score :");
scanf_s("%f", &p1->score );
}
p2->next = NULL;
return head;
}
void print(struct student *head)
{
struct student *p=head;
if( n==0 )
{
printf("There is only %d score\n", n);
}
else
printf("There are %d scores totally\n", n);
while (p->next != NULL)
{
printf("num is %d ; score is %f\n\n", p->num ,p->score );
p = p->next;
}
printf("num is %d ; score is %f\n\n", p->num ,p->score );
}
在中scanf_s("%f", &p1->score );我想直接用p1来操作获取p1->score的地址,怎么搞,之前试过scanf_s("%f", p1+1 )是不对的,因为p1+1是下一个节点的首地址,不是第二个成员的地址?
要那么多种方法干什么,多了还容易混淆
|
|