C语言指针函数链表复习

简介: 指向整型数据的指针类型表示为:int *,读作“指向int的指针”或简称“int指针” p=&a;//表示把a的地址赋给指针变量p print(“%d”,*p);//即指针变量p所指向的变量的值,即变量a的值。

指向整型数据的指针类型表示为:int *,读作“指向int的指针”或简称“int指针”

p=&a;//表示把a的地址赋给指针变量p

print(“%d”,*p);//即指针变量p所指向的变量的值,即变量a的值。

运用代码:

#include <stdio.h>
#define N 3
struct Student
{
int num;
char name[20];
float score[3];
float aver;
};

int main(int argc, const char * argv[])
 {
void input(struct Student stu[]);
struct Student max(struct Student stu[]);
void print(struct Student stu);
struct Student stu[N],*p=stu;
input(p);
print(max(p));
return 0;
}
void input(struct Student stu[])
{
int i;
printf("请输入各个学生的信息:学号、姓名、三门课成绩\n");
for (i=0; i<N; i++) {
    scanf("%d%s%f%f%f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
    stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
}
}
struct Student max(struct Student stu[]) {
int i,m=0;
for(i=0;i<N;i++)
{
    if(stu[i].aver>stu[m].aver)m=i;
}
return stu[m];
}
void print(struct Student stud)
{
printf("\n成绩最高的学生是:\n");
printf("学号:%d\n姓名:%s\n三门课成绩:%5.1f,%5.1f,%5.1f\n平均成绩:%6.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver);
}

链表:成绩系统,输入0为结束

#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct Student)
struct Student
{
   long num;
    float score;
    struct Student* next;
};
int n;
struct Student* creatn()
{
struct Student *head;
struct Student *p1,*p2;
n=0;
p1=p2=(struct Student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while (p1->num!=0) {
    n=n+1;
    if (n==1)
        head=p1;
        if (n==1)
            head=p1;

        else

            p2->next=p1;

    p2=p1;
    p1=(struct Student *)malloc(LEN);
    scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
printf("%d",n);
return (head);
}

    void print(struct Student head)
{
    struct Student *p;
    printf("\nNow,These %d records are :\n",n);

    p=head;
    if(head!=NULL)
    do
    {
        printf("%ld %5.1f\n",p->num,p->score);
        p=p->next;
    }while (p!=NULL);

}
int main(int argc, const char * argv[]) {
struct Student *head;
head=creatn();
printf(head);
return 0;
}
相关文章
|
15天前
|
安全 C语言
【C语言】如何规避野指针
【C语言】如何规避野指针
20 0
|
16天前
|
C语言
C语言:数组和指针笔试题解析(包括一些容易混淆的指针题目)
C语言:数组和指针笔试题解析(包括一些容易混淆的指针题目)
|
1月前
|
存储 程序员 编译器
爱上C语言:指针很难?来来来,看看这篇(基础篇)
爱上C语言:指针很难?来来来,看看这篇(基础篇)
|
5天前
|
C语言
c语言指针总结
c语言指针总结
11 1
|
10天前
|
存储 程序员 C语言
【C 言专栏】C 语言指针的深度解析
【4月更文挑战第30天】C 语言中的指针是程序设计的关键,它如同一把钥匙,提供直接内存操作的途径。指针是存储其他变量地址的变量,通过声明如`int *ptr`来使用。它们在动态内存分配、函数参数传递及数组操作中发挥重要作用。然而,误用指针可能导致错误,如空指针引用和内存泄漏。理解指针的运算、与数组和函数的关系,以及在结构体中的应用,是成为熟练 C 语言程序员的必经之路。虽然挑战重重,但掌握指针将增强编程效率和灵活性。不断实践和学习,我们将驾驭指针,探索更广阔的编程世界。
|
11天前
|
算法 搜索推荐 程序员
C语言中的函数指针和回调函数
C语言中的函数指针和回调函数
9 2
|
13天前
|
C语言
链表的插入、删除和查询—C语言
链表的插入、删除和查询—C语言
|
15天前
|
存储 编译器 C语言
【C语言】初步解决指针疑惑
【C语言】初步解决指针疑惑
7 0
|
16天前
|
存储 C语言
指针深入解析(C语言基础)带你走进指针,了解指针
指针深入解析(C语言基础)带你走进指针,了解指针
|
16天前
|
C语言 C++
C语言:指针运算笔试题解析(包括令人费解的指针题目)
C语言:指针运算笔试题解析(包括令人费解的指针题目)