#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int sub(int a, int b){
return a - b;
}
int main(void)
{
int ret = 0;
int (*pfunc)(int, int) = add;
ret = pfunc(100, 200);
printf("add函数的返回值是%d\n", ret);
pfunc = sub;
ret = pfunc(100, 200);
printf("add函数返回值是%d\n", ret);
return 0;
}
#include <stdio.h>
typedef int (*pfunc_t)(int, int);
int add(int a, int b) {
return a + b;
}
int sub(int a, int b){
return a - b;
}
int main(void)
{
int ret = 0;
pfunc_t pfunc = add;
ret = pfunc(100, 200);
printf("add函数的返回值是%d\n", ret);
pfunc = sub;
ret = pfunc(100, 200);
printf("add函数返回值是%d\n", ret);
return 0;
}
```c
typedef int (*pfunc_t)(int, int);
int add(int a, int b) {
return a + b;
}
int sub(int a, int b){
return a - b;
}
int cal(int a, int b, pfunc_t pfunc)
{
if(pfunc == NULL)
return a * b;
else
return pfunc(a, b);
}
int main(void)
{
printf("%d\n",cal(50, 100, NULL));
printf("%d\n",cal(50, 100, add));
printf("%d\n",cal(50, 100, sub));
return 0;
}
123
123
```c
//函数指针玩法
#include <stdio.h>
#include <stdlib.h> //声明qsort函数
int intcmp(const void *p1, const void *p2)
{
return *(const int *)p1 - *(const int *)p2;
}
int main(void)
{
int a[] = {13, 34, 9, 44, 54, 22, 15, 9 };
int len = sizeof(a)/sizeof(a[0]);
//qsort已经把快速排序算法流程算好了,只需要编写一个回调函数intcmp
//比较两个数的大小,然后将比较的结果给qsort,将来qsort自动调用intcmp,
//根据返回值结果自动进行排序
//大大减轻开发的工作量
qsort(a, len, sizeof(a[0]), intcmp);
for(int i = 0;i < len; i++)
printf("%d\n", a[i]);
printf("\n");
return 0;
}
1111
#include <stdio.h>
int main(void){
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
for(int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
{
for(int j = 0; j< sizeof(a[0])/sizeof(a[0][1]); j++)
printf("%d ", a[i][j]);
printf("\n");
}
int (*p)[4] = a;
for(int i = 0; i< 3; i++){
for(int j = 0; j < 4; j++)
printf("%d ", p[i][j]);
printf("\n");
}
int *p1 =(int *)a;
for(int i = 0; i < 12; i++)
printf("%d ", p1[i]);
printf("\n");
int b[] = {0,1,2,3,4,5,6,7,8,9,10,11};
int (*p2)[4] =(int (*)[4])b;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 4; j++)
printf("%d ", a[i][j]);
printf("\n");
}
#include <stdio.h>
int main(void) {
int n = 10;
int *p = &n;
int **pp = &p;
printf("n = %d, &n = %p\n, n, &n");
printf("p = %p, &p = %p, *p = %d\n", p, &p, *p);
printf("pp = %p, &pp = %p, *pp = %p, **pp = %d\n", pp, &pp, *pp, **pp);
*p = 300;
printf("n = %d\n", *p);
**pp = 400;
printf("n = %d\n", **pp);
return 0;
}
1111
#include <stdio.h>
void swap(char *p1, char *p2){
char *p3 = p1;
p1 = p2;
p2 = p3;
}
void swap1(char **p1, char **p2){
char *p3 = *p1;
*p1 = *p2;
*p2 = p3;
}
int main(void)
{
char *pa = "hello", *pb = "world";
swap(pa, pb);
printf("pa = %s, pb = %s\n",pa, pb);
swap1(&pa, &pb);
printf("pa = %s, pb = %s\n",pa, pb);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void){
int *p = NULL;
p = (int *)malloc(8);
if(p == NULL)
{
printf("内存分配失败了。\n");
return -1;
}
printf("分配成功,首地址是%p\n", p);
*(p+0) = 520;
*(p+1) = 521;
printf("%d, %d\n", p[0], p[1]);
free(p);
p= NULL;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *p = NULL;
p = (int *)calloc(4, sizeof(int));
if(p == NULL){
printf("内存分配失败了.\n");
return -1;
}
printf("分配成功,首地址是%p\n", p);
for(int i = 0; i < 4; i++)
p[i] = i + 1;
for(int i = 0; i < 4; i++)
printf("%d", *(p+i));
printf("\n");
free(p);
p = NULL;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define NUMB 4
int main(void)
{
int *p = NULL;
if(p == NULL){
printf("内存分配失败了.\n");
return -1;
}
printf("分配成功,首地址是%p\n", p);
*(p + 0) = 520;
*(p + 1) = 521;
printf("%d, %d\n", p[0], p[1]);
p = realloc(p, 16);
首地址给p
for(int i = 0; i < NUMB; i++)
p[i] = i + 1;
for(int i = 0; i < NUMB; i++)
printf("%d ", *(p+i));
printf("\n");
free(p);
p = NULL;
return 0;
}
**
IO流与标准库
**
#include <stdio.h>
int main(void)
{
FILE *fp = NULL;
fp = fopen("a.txt", "r");
if(fp == NULL){
printf("打开失败.\n");
return -1;
}
printf("打开成功.\n");
fclose(fp);
return 0;
}
#include <stdio.h>
int main(void)
{
FILE *fp = NULL;
fp = fopen("a.txt", "r");
if(fp == NULL){
printf("打开失败.\n");
return -1;
}
printf("打开成功.\n");
int i = 0;
char a[20] = {0};
double d = 0;
fscanf(fp, "%d%s%lf", &i, a, &d);
fclose(fp);
fp = NULL;
printf("i = %d, a = %s, d = %lg\n", i, a, d);
FILE *fpw = fopen("b.txt", "w");
if(fpw == NULL)
{
printf("打开失败。\n");
return -1;
}
fprintf(fpw, "copy:%d %s %lf", i, a, d);
fclose(fpw);
fpw = NULL;
fscanf(stdin, "%d%s%lf", &i, a, &d);
printf("i = %d, a = %s, d = %lg\n", i, a, d);
fprintf(stdout, "i = %d, a = %s, d = %lg\n", i, a, d);
fprintf(stderr, "i = %d, a = %s, d = %lg\n", i, a, d);
return 0;
}
在这里插入图片描述
#include <stdio.h>
int main(void)
{
FILE *fpr = fopen("a.txt", "r");
FILE *fpw = fopen("b.txt", "w+");
char str[4] = {0};
while(fgets(str, 4, fpr) != NULL)
{
fputs(str, fpw);
}
while(1)
{
ch = fgetc(fpr);
if(ch == EOF)
break;
fputc(ch, fpw);
}
fclose(fpr);
fclose(fpw);
fpr = NULL;
fpw = NULL;
return 0;
}
#include <stdio.h>
int main(void){
FILE *fp = fopen("a.txt", "w+");
int a[] = {1,2,3,4,5,6,7,8};
int len = sizeof(a)/sizeof(a[0]);
int size = 0;
size = fwrite(a, sizeof(int), len, fp);
printf("实际写入了%d个数据\n", size);
rewind(fp);
int b[8] = {0};
size = fread(b, sizeof(int), 10, fp);
printf("实际读取了%d个数据\n", size);
for(int i = 0;i < size; i++)
printf("%d", b[i]);
printf("\n");
fclose(fp);
fp = NULL;
return 0;
}
#include <stdio.h>
int main(void){
FILE *fp = fopen("a.txt", "w+");
int a[] = {1,2,3,4,5,6,7,8};
int len = sizeof(a)/sizeof(a[0]);
int size = 0;
size = fwrite(a, sizeof(int), len, fp);
printf("实际写入了%d个数据\n", size);
rewind(fp);
int b[8] = {0};
size = fread(b, sizeof(int), 10, fp);
printf("实际读取了%d个数据\n", size);
for(int i = 0;i < size; i++)
printf("%d", b[i]);
printf("\n");
int c[2] = {0};
fseek(fp, 8, SEEK_SET);
fread(c, sizeof(int), 2, fp);
printf("%d %d\n",c[0], c[1]);
fseek(fp, 8, SEEK_CUR);
fread(c, sizeof(int), 2, fp);
printf("%d %d\n",c[0], c[1]);
fseek(fp, -12, SEEK_END);
fread(c, sizeof(int), 2, fp);
printf("%d %d\n",c[0], c[1]);
fclose(fp);
fp = NULL;
return 0;
}
#include <stdio.h>
int main(void)
{
FILE *src = fopen("a.txt", "r");
FILE *dst = fopen("b.txt", "w");
int bytes;
unsigned char buf[1024];
while(bytes = fread(buf, 1, sizeof(buf), src)){
if(fwrite(buf, 1, bytes, dst) != bytes)
{
printf("写入失败。\n");
return -1;
}
}
if(ferror(src)){
printf("读取失败。\n");
}
if(feof(src))
{
printf("到头啦\n");
return 0;
}
fclose(src);
fclose(dst);
}
|