上课笔记-指针(从百草园到三味书屋)

目录

求最大字符串

指针函数

函数指针

结构体指针

结构体中的指针

链表
 


求最大字符串

拓展:strcmp函数详解https://blog.csdn.net/MQ0522/article/details/111226036?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163952814216780264015667%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=163952814216780264015667&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-3-111226036.pc_search_em_sort&utm_term=strcmp&spm=1018.2226.3001.4187https://blog.csdn.net/MQ0522/article/details/111226036?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163952814216780264015667%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=163952814216780264015667&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-3-111226036.pc_search_em_sort&utm_term=strcmp&spm=1018.2226.3001.4187

写法1

#include<stdio.h>
#include<string.h>
void fun(char s[][100],int n) //void fun(char (*s)[100],int n)
{
    int x=0,i;
    for(i=1;i<n;i++)
            if(strcmp(s[i],s[x])>0) x=i;
    printf("%s\n",s[x]);  //printf("%s\n",*(s+x));
}

int main()
{
    char str[5][100];
    int i;
    for(i=0;i<5;i++)
            gets(str[i]);
    fun(str,5);
    return 0;
}

写法2

#include<stdio.h>
#include<string.h>
void fun(char *s,int n)
{
    int x=0,i;
    for(i=1;i<n;i++)
            if(strcmp(s+i*100,s+x*100)>0) x=i;
    printf("%s\n",s+x*100);
}

int main()
{
    char str[5][100];
    int i;
    for(i=0;i<5;i++)
            gets(str[i]);
    fun(str[0],5);
    return 0;
}

如果每个字符串的长度不固定呢?

#include<stdio.h>
#include<string.h>
void fun(char *ps[],int n) //数组的第一个长度可以省略掉
{
        int i,x=0;
        for(i=0;i<5;i++)
                if(strcmp(ps[i],ps[x])>0) x=i;
        puts(ps[x]);
}

int main()
{
    char *str[]={"wwwwwwwwwwww","q","0","aaaa","5555556"};
    fun(str,5);
    return 0;
}

指针函数

拓展:puts()函数详解https://blog.csdn.net/zhanshen112/article/details/84075164?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163953025616780357228397%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=163953025616780357228397&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-84075164.pc_search_em_sort&utm_term=puts&spm=1018.2226.3001.4187https://blog.csdn.net/zhanshen112/article/details/84075164?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163953025616780357228397%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=163953025616780357228397&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-84075164.pc_search_em_sort&utm_term=puts&spm=1018.2226.3001.4187

返回值为指针的函数

#include<stdio.h>
#include<string.h>
char *fun(char *ps[],int n) //数组的第一个长度可以省略掉
{
        int i,x=0;
        for(i=0;i<5;i++)
                if(strcmp(ps[i],ps[x])>0) x=i;
        return ps[x];
}

int main()
{
    char *str[]={"wwwwwwwwwwww","q","0","aaaa","5555556"};
    char *ps;
    ps=fun(str,5);
    puts(ps);
    return 0;
}

同样将第一个代码段改成

#include<stdio.h>
#include<string.h>
char *fun(char s[][100],int n) 
{
    int x=0,i;
    for(i=1;i<n;i++)
            if(strcmp(s[i],s[x])>0) x=i;
    return *(s+x);
}

int main()
{
    char str[5][100];
    int i;
    for(i=0;i<5;i++)
            gets(str[i]);
    puts(fun(str,5));
    return 0;
}

函数指针

可以这样用

#include<stdio.h>
int Add(int a,int b)
{
    return a+b;
}

int main()
{
    int (*pf)(int,int);
    int x;
    pf=Add;
    x=pf(3,5);
    printf("%d",x);
    return 0;
}
#include<stdio.h>
int Max(int a,int b)
{
    return a>b?a:b;
}

int Add(int a,int b)
{
    return a+b;
}

int main()
{
    int (*pf[2])(int,int);
    int c,d;
    pf[0]=Max;
    c=pf[0](3,5);
    pf[1]=Add;
    d=pf[1](3,5);
    printf("c=%d\nd=%d",c,d);
    return 0;
}

结构体指针

写法1:

#include<stdio.h>

struct Date
{
    int yearl,month,day;
};

struct STU
{
    int no;
    char name[9];
    double score[3];
    struct Date birth;
};

int main()
{
    struct STU a={101,"qqq",77,88,99,2002,12,7};
    struct STU *p=&a;
    printf("学号 %d\n",(*p).no);
    printf("姓名 %s",(*p).name);  //*p必须要加括号,因为成员运算符"."的等级最高
    return 0;
}

用"->"写

#include<stdio.h>

struct Date
{
    int year,month,day;
};

struct STU
{
    int no;
    char name[9];
    double score[3];
    struct Date birth;
};

int main()
{
    struct STU a={101,"qqq",77,88,99,2002,12,7};
    struct STU *p=&a;
    printf("学号 %d\n",p->no);
    printf("姓名 %s\n",(*p).name);  //*p必须要加括号,因为成员运算符"."的等级最高
    printf("成绩 %lf\n",p->score[0]);
    printf("生日 %d-%d-%d\n",p->birth.year,p->birth.month,p->birth.day);
    return 0;
}

结构体中的指针

#include<stdio.h>
struct STU
{
    int *p;
};

int main()
{
    int s[10]={1,2,3,4,5,6,7,8,9,10};
    struct STU a={101,"qqq",77,88,99,2002,12,7};
    int i;   a.p=s;
    // 输出数组s的每一个成员
    for(i=0;i<10;i++)
            printf("%4d",(a.p)[i]);
          //printf("%4d",a.p[i]); "[]"和"."的运算等级一样高,所以也可以不加()
          //printf("%4d",(a.p+i));
    return 0;
}

可以用来写一个类似线性表的东西

#include<stdio.h>

struct Date
{
    int year,month,day;
};

struct STU
{
    int no;
    char name[9];
    double score[3];
    struct Date birth;
    struct STU *next;
};

int main()
{
    struct STU a={101,"qqq",77,88,99,2002,12,7};
    struct STU b={102,"www",77,88,99,2002,12,7};
    struct STU *p=&a;
    a.next=&b;
    printf("b 学号 %d\n",(a.next)->no);
    //printf("b  %d\n",p->next->no);
    //printf("b 学号 %d\n",b.no);
    return 0;
}
#include<stdio.h>
typedef char ch;
typedef struct Student //把数据换个名字
{
    int no;
    struct Student *next;
}STU;

int main()
{
    STU a={110},b={111},*p;
    p=&a;
    a.next=&b;
    b.next=NULL;
    printf("%d\n",p->no);
    printf("%d\n",p->next->no);
    return 0;
}

为什么不直接访问呢?因为在程序设计的过程中,有些数据是没有名字的,它只有地址,此时只能用这种方法来访问;

以动态分配为例:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int *p,i;
    p=(int*)malloc(100*sizeof(int));  //返回的地址没有任何类型。C语言中要说明地址是什么类型的,所以我们要强制转换
    for(i=0;i<10;i++)
        scanf("%d",p+i); //scanf("%d",&p[i]);

    for(i=0;i<10;i++)
    printf("%d ",*(p+i));  //printf("%d",*p[i]);
    free(p);  //输入首地址以回收
    return 0;
}

链表

单向链表:

  头插入法:最先进入链表的在最后(还有尾插入法)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;

typedef struct student   //类型定义,其实就是给他换个名字
{
    int no;
    char name[10];
    struct student *next;
}STU;

int main()
{
    int n,i;
    STU *head,*p;
    head = NULL;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        p = (STU*)malloc(sizeof(STU)); //表示指向结构体类型的指针
        scanf("%d%s",&(p->no),p->name);
        p -> next = head;
        head = p;
    }
    p=head; //在这个地方其实可以删掉
    while(p!=NULL)
    {
        printf("%10d %10s\n",p->no,p->name);
        free(p);
        p=p->next;
    }
    return 0;
}

双向链表:一个结构体存放两个地址

双向链表->树

宏:没有任何含义,就是原原本本地去替换
例如 #define Pi 3.14
易错点:
#define sqr(a) a*a
sqr(3+5) -> 3+5*3+5
#define sqr(a) (a)*(a)
sqr(3+5) -> (3+5)*(3+5)
常用:
#define max(a,b) a>b?a:b

文件:分为文本文件和二进制文件
存入13这个数字
文本:1:0011 0001  3:0011 0011
二进制:00001101

记住两个函数
freopen:文件重定向,文件重新打开,输入从文件里读,输出写到文件中去
        freopen(文件名 文件 打开方式);
                ① 文件名 stdin,stdout 标准输入文件(默认为键盘)与标准输出文件(默认为显示器)
                ②

示范: 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Student
{
    int no;
    char name[10];
    struct Student *next;
}STU;

int main()
{
    freopen("c:\users\Desktop\编程学习\文件的测试数据(输入).txt","r",stdin);
    freopen("c:\users\Desktop\编程学习\文件的测试结果(输出).txt","w",stdout);
    int n,i;
    STU *head,*p;
    scanf("%d",&n);
    head=NULL;
    for(i=0;i<n;i++)
    {
        p=(STU*)malloc(sizeof(STU));
        scanf("%d%s",&(p->no),p->name);
        p->next=head;
        head=p;
    }
    p=head;
    while(p!=NULL)
    {
        printf("10%d 10%s\n",p->no,p->name);
        p=p->next;
    }
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值