当前位置: 首页 > news >正文

C语言家教记录(八)

C语言家教记录(八)

  • 导语
  • 指针的高级应用
    • 动态分配、使用、释放
    • 链表
    • 指向指针的指针
    • 指向函数的指针
  • 流和文件
    • 文件操作
    • 变量和格式化io
  • 总结和复习

导语

本次授课的内容如下:指针的高级应用,流和文件

辅助教材为 《C语言程序设计现代方法(第2版)》

指针的高级应用

动态分配、使用、释放

malloc,calloc(一般不用),realloc(一般不用)
null 空指针,也可以用0

null的使用

if(p==NULL)
if(!p)
if(p!=NULL)
if(p)

malloc的使用

p=malloc(n+1);//不常用
p=(char*)malloc(n+1);//常用,malloc的分配单位是字节int* a;
a=malloc(n*sizeof(int));
for(int i=0;i<n;i++)a[i]=0;

calloc的使用

a=calloc(n,sizeof(int));//自动初始化
struct point { int x, y; } *p;
p = calloc(1, sizeof(struct point));//x,y都为0

realloc的使用,规则见书

realloc(p,n*sizeof(int));

释放存储空间,例子见书

p=malloc();
q=malloc();
free(p);
p=q;char*p=malloc(...);
free(p);
strcpy(p,"abc");//报错,悬空指针问题

示例程序

char *concat(const char *s1, const char *s2)
{char *result ;result = malloc(strlen(s1) + strlen(s2) + 1);if (result == NULL) {printf("Error: malloc failed in concat\n");exit (EXIT_FAILURE);}strcpy(result, s1);strcat(result, s2);return result;
}
p=concat("abs","def");

链表

概念和例子见书

struct node
{int value;struct node *next;
};//链表的节点定义
struct node* first =NULL;//节点的创建,图见书
struct node* p;
p=malloc(sizeof(struct node));
(*p).value=10;
//等价于
p->value=10;

示例程序

解释插入链表的实现,解释图

struct node *add_to_list(struct node *list, int n)
{struct node *new_node;new_node = malloc(sizeof(struct node));if (new_node == NULL) {printf("Error: malloc failed in add_to list\n");exit(EXIT_FAILURE);}new_node->value = n;new_node->next = list;return new_node;
}

搜索链表,用书上例子解释

struct node *search_list(struct node *list, int n)
{struct node *p;for (p = list; p != NULL; p = p->next)if (p->value == n)return p;return NULL;
}struct node *search_list(struct node *list, int n)
{while (list != NULL && list->value != n)list = list->next;return list;
}

删除节点,根据书上例子

struct node *delete_from_list(struct node *list, int n)
{struct node *cur, *prev;for (cur = list, prev = NULL;cur != NULL && cur->value != n;prev = cur, cur = cur->next);if (cur == NULL)return list; /* n was not found */if (prev == NULL)list = list->next; /* n is in the first node */elseprev->next = cur->next; /* n is in some other node */free (cur);return list;
}

指向指针的指针

如果不进行返回,添加结点可能会失效,见书上例子

void_add_to_list(struct node **list, int n)
{struct node *new_node;new_node = malloc(sizeof(struct node));if (new_node == NULL) {printf("Error: malloc failed in add_to_list\n");exit(EXIT_FAILURE);}new_node->value = n;new_node->next = *list;*list = new_node;
}

指向函数的指针

void (*pf)(int);
pf=f;//赋值
(*pf)(i);//调用
pf(i);//调用
//pf 可以指向任何带有int 型形式参数并且返回void 型值的函数double integrate(double (*f)(double), double a, double b);
double integrate(double f(double), double a, double b);//等价result=integrate(sin,0.0,PI/2);integrate内可以调用
y=(*f)(x);

qsort函数

void qsort(void *base, size_t nmemb, size_t size,int (*compar) (const void *, const void *));//函数原型qsort(inventory, num_parts, sizeof(struct part), compare_parts);//调用格式int compare_parts(const void *p, const void *q)
{const struct part *p1 = p;const struct part *q1 = q;if (p1->number < q1->number)return -1;else if (p1->number == q1->number)return 0;elsereturn 1;
}
//等价于
int compare_parts(const void *p, const void *q)
{return strcmp(((struct part *) p)->name,((struct part *) q)->name);
}

流和文件

stdin,stdout,stderr简单介绍

简单介绍二进制文件

简单介绍输入输出重定向和命令行

文件操作

fopen,fclose,freopen等
tmpfile,fflush,remove,rename跳过

FILE *fopen(const char * restrict filename, const char * restrictmode);//函数原型fp=fopen("a.txt","r");//路径和模式,运行之后输入从键盘改成文件
//解释参数r w a r+ w+ a+,参数可以组合int fclose(FILE *stream);//关闭文件FILE *freopen(const char * restrict filename, const char * restrict mode, FILE * restrict stream);//重定向if (freopen("foo","w", stdout) == NULL) {/* error; foo can't be opened */
}//最好先关闭再重定向//解释从命令行获取文件名
int main(int argc, char *argv[])
{...
}

示例程序

#include <stdio.h>
#include <stdlib.h>
#define FILE_NAME "example.dat"
int main(void)
{FILE *fp;fp = fopen(FILE_NAME, "r");if (fp == NULL) {printf("Can't open %s\n", FILE_NAME);exit(EXIT_FAILURE);}...fclose(fp);return 0;
}#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{FILE *fp;if (argc != 2) {printf("usage: canopen filename\n");exit (EXIT_FAILURE);}if ((fp = fopen(argv[1], "r")) == NULL) {printf("%s can't be opened\n", argv[1]);exit (EXIT_FAILURE);}printf("%s can be opened\n", argv[1]);fclose(fp);return 0;
}

变量和格式化io

简介fprintf和printf,见书

简介fscanf和scanf,见书

简介fputc,putc,putchar,fgets,gets

sprintf,snprintf,针对字符数组的输出

sscanf,针对字符数组的输入

文件定位和块输入输出跳过,简单勾一下

示例程序

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{FILE *source_fp, *dest_fp;int ch;if (argc != 3) {fprintf(stderr, "usage: fcopy source dest\n");exit(EXIT_FAILURE);}if ((source_fp = fopen(argv[1], "rb")) == NULL) {fprintf (stderr, "Can't open %s\n", argv[1]);exit(EXIT_FAILURE);}if ((dest_fp = fopen(argv[2], "wb")) == NULL) {fprintf (stderr, "Can't open %s\n", argv[2]);fclose(source_fp);exit(EXIT_FAILURE);}while ((ch = getc(source_fp)) != EOF)putc (ch, dest_fp);fclose(source_fp);fclose(dest_fp);return 0;
}

总结和复习

本次授课讲述第17章和第22章内容,关键点:指针的高级应用和输入输出

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 豆包插件分享
  • C++系列-类模板案例
  • 最大噪音值甚至受法规限制,如何基于LBM算法有效控制风扇气动噪音
  • 数据结构之链表
  • AI辅助编码在主流IDE中的智能代码补全说明
  • 一 初识爬虫
  • 分享7款宝藏APP,用途多样,值得一试
  • centos7.9系统安装cloudpods并使用ceph存储(二)
  • kubernetes培训
  • Qt之QSS的介绍以及加载QSS
  • 阿里云私有镜像仓库配置及使用
  • 双重映射+逆向并查集+恢复
  • Spring Cloud Eureka与Kubernetes的集成:服务发现的混合方案
  • 开放式耳机哪个品牌实用?南卡、漫步者、小米 三款爆火单品横评
  • 自动化运维:Ansible、Puppet、Chef工具对比与实战
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • 230. Kth Smallest Element in a BST
  • Angular2开发踩坑系列-生产环境编译
  • exif信息对照
  • HTTP 简介
  • java小心机(3)| 浅析finalize()
  • JS专题之继承
  • React as a UI Runtime(五、列表)
  • 记录:CentOS7.2配置LNMP环境记录
  • 前端js -- this指向总结。
  • 实现简单的正则表达式引擎
  • 提醒我喝水chrome插件开发指南
  • 用 vue 组件自定义 v-model, 实现一个 Tab 组件。
  • k8s使用glusterfs实现动态持久化存储
  • NLPIR智能语义技术让大数据挖掘更简单
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • #include到底该写在哪
  • #LLM入门|Prompt#1.7_文本拓展_Expanding
  • (arch)linux 转换文件编码格式
  • (Matalb分类预测)GA-BP遗传算法优化BP神经网络的多维分类预测
  • (Oracle)SQL优化基础(三):看懂执行计划顺序
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (利用IDEA+Maven)定制属于自己的jar包
  • (区间dp) (经典例题) 石子合并
  • (十八)三元表达式和列表解析
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • (一)RocketMQ初步认识
  • (原創) 如何動態建立二維陣列(多維陣列)? (.NET) (C#)
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • (转)scrum常见工具列表
  • (转载)OpenStack Hacker养成指南
  • .Net Core 中间件与过滤器
  • .Net Winform开发笔记(一)
  • .net的socket示例
  • .py文件应该怎样打开?
  • /etc/sudoers (root权限管理)
  • /proc/vmstat 详解
  • @Autowired和@Resource装配
  • @test注解_Spring 自定义注解你了解过吗?