引言
C语言的一些基本例题 全部例题来自于C语言王道训练营 链接如下
B站链接. 书籍:《跟“龙哥”学C语言编程》
1.引用与解引用
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i = 10;
int j = 5;
int* p;
p = &i;
printf("i=%d\n", i);
printf("p=%d\n", *p);
printf("%p\n", &*p);
printf("%d\n", *&i);
system("pause");
return 0;
}
运行结果
i=10
p=10
0032F728
10
请按任意键继续. . .
2.指针的传递
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
void change(int j)
{
j = 5;
}
void zzchange(int* p)
{
*p = 5;
}
int main()
{
int i = 10;
printf("before change i=%d\n", i);
change(i);
printf("after change i=%d\n", i);
zzchange(&i);
printf("after zzchange i=%d\n", i);
system("pause");
return 0;
}
运行结果
before change i=10
after change i=10
after zzchange i=5
请按任意键继续. . .
3.指针的偏移
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
#define N 5
int main()
{
int a[N] = { 1,2,3,4,5 };
int* p;
int i;
p = a;
for (i = 0; i < N; i++)
{
printf("%3d", *(p + i));
}
printf("\n----------------------------\n");
p = &a[4];
for (i = 0; i < N; i++)
{
printf("%-3d", *(p - i));
}
printf("\n");
system("pause");
return 0;
}
运行结果
1 2 3 4 5
----------------------------
5 4 3 2 1
请按任意键继续. . .
4.指针与自增、自减
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[3] = { 2,7,10 };
int* p;
int j;
p = a;
j = *p++;
printf("a[0]=%d,j=%d,*p=%d\n", a[0], j, *p);
j = p[0]++;
printf("a[0]=%d,j=%d,*p=%d\n", a[0], j, *p);
system("pause");
return 0;
}
运行结果
a[0]=2,j=2,*p=7
a[0]=2,j=7,*p=8
请按任意键继续. . .
5.指针与一维数组
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
void change(char *d)
{
*d = 'H';
d[1] = 'E';
*(d + 2) = 'L';
}
int main()
{
char c[10] = "hello";
char* p = "world";
change(c);
printf("c=%s\n", c);
printf("p=%s\n", p);
p = "hello";
printf("p=%s\n", p);
system("pause");
return 0;
}
运行结果
c=HELlo
p=world
p=hello
请按任意键继续. . .
6.动态内存申请
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int i;
char* p;
scanf("%d", &i);
p = (char *)malloc(i);
strcpy(p, "malloc success");
puts(p);
free(p);
printf("free success\n");
system("pause");
return 0;
}
运行结果
20
malloc success
free success
请按任意键继续. . .
7.野指针
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int* p1, * p2, * p3;
p1 = (int*)malloc(4);
*p1 = 1;
p2 = (int*)malloc(4);
*p2 = 2;
free(p1);
p3 = (int*)malloc(4);
*p3 = 3;
printf("*p3=%d\n", *p3);
*p1 = 100;
printf("*p1赋值100后,*p3=%d\n", *p3);
system("pause");
return 0;
}
运行结果
*p3=3
*p1赋值100后,*p3=100
请按任意键继续. . .
8.栈空间与堆空间的差异
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
char* print_stack()
{
char c[] = "I am print_stack";
puts(c);
return c;
}
char* print_malloc()
{
char* p;
p = (char*)malloc(20);
strcpy(p, "I am print_malloc");
puts(p);
return p;
}
int main()
{
char* p;
p = print_stack();
puts(p);
p = print_malloc();
puts(p);
system("pause");
return 0;
}
运行结果
I am print_stack
H`?
I am print_malloc
I am print_malloc
请按任意键继续. . .
9.深入理解const
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
int main()
{
char str[] = "hello world";
char str1[] = "how do you do";
const char* ptr = str;
char* const ptr1 = str1;
str[0] = 'H';
printf("++++str的内容++++\n");
puts(str);
printf("++++str1的内容++++\n");
puts(str1);
printf("++++ptr1指向str1++++\n");
puts(ptr1);
ptr1[0] = 'n';
printf("++++ptr1修改后++++\n");
puts(ptr1);
printf("++++ptr指向str++++\n");
puts(ptr);
ptr = str1;
printf("++++ptr更改指向str1++++\n");
puts(ptr);
system("pause");
}
运行结果
++++str的内容++++
Hello world
++++str1的内容++++
how do you do
++++ptr1指向str1++++
how do you do
++++ptr1修改后++++
now do you do
++++ptr指向str++++
Hello world
++++ptr更改指向str1++++
now do you do
请按任意键继续. . .
10.memmove的实现
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 5
int main()
{
int a[N] = { 1,2,3,4,5 };
int b[N] = { 1,2,3,4,5 };
int i;
memmove(b + 2, b + 1, 8);
for (i = 0; i < N; i++)
{
printf("%3d", b[i]);
}
printf("\n");
system("pause");
return 0;
}
运行结果
1 2 2 3 5
请按任意键继续. . .
11.数组指针与二维数组
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
void print(int (*p)[4], int row)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < sizeof(p[0]) / sizeof(int); j++)
{
printf("%-3d",*(*(p+i)+j));
}
printf("\n");
}
}
int main()
{
int a[3][4] = { 1,3,5,7,9,11,13,15,17,19,21,23 };
int b[4] = { 1,2,3,4 };
int i = 10;
int(*p)[4];
p = a;
print(a, 3);
system("pause");
return 0;
}
运行结果
1 3 5 7
9 11 13 15
17 19 21 23
请按任意键继续. . .
12.二级指针的传递
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
void change(int** p, int* pj)
{
*pj= 15;
*p = pj;
}
int main()
{
int i = 10;
int j = 5;
int* pi, * pj;
pi = &i;
pj = &j;
printf("i=%d,*pi=%d,*pj=%d\n", i, *pi, *pj);
change(&pi, pj);
printf("after change i=%d,*pi=%d,*pj=%d\n", i, *pi, *pj);
system("pause");
return 0;
}
运行结果
i=10,*pi=10,*pj=5
after change i=10,*pi=15,*pj=15
请按任意键继续. . .
13.二级指针的偏移
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
void print(char* p[])
{
int i;
for (i = 0; i < 5; i++)
{
puts(p[i]);
}
}
int main()
{
char* p[5];
char b[5][10] = { "lele","lili","lilei","hanmeimei","zhousi" };
int i, j, tmp;
char* t;
char** p2;
int a[5] = { 3,7,9,2,4 };
for (i = 0; i < 5; i++)
{
p[i] = b[i];
}
p2 = p;
for (i = 4; i > 0; i--)
{
for (j = 0; j < i; j++)
{
if (strcmp(p2[j], p2[j + 1]) == 1)
{
t = p2[j];
p2[j] = p2[j + 1];
p2[j + 1] = t;
}
}
}
print(p2);
puts("----------------");
for (i = 0; i < 5; i++)
{
puts(b[i]);
}
system("pause");
return 0;
}
运行结果
hanmeimei
lele
lilei
lili
zhousi
----------------
lele
lili
lilei
hanmeimei
zhousi
请按任意键继续. . .
14.函数指针
下面展示一些 可运行的代码 。
#include<stdio.h>
#include<stdlib.h>
void b()
{
printf("I am func b\n");
}
void a(void (*p)())
{
p();
}
int main()
{
void (*p)();
p = b;
a(p);
system("pause");
return 0;
}
运行结果
I am func b
请按任意键继续. . .
总结
第一次发表一篇完整的博客,对于一些遗漏,读者不要太过深究~ 比较适合新手打基础大佬请绕路~ 希望这些可以帮助你更好的理解C语言 马上就考研了 我居然还在纠结一些基础 真是闲的闲的闲的闲*10000! 过几天在更新后几章的内容 欢迎大家评论、收藏、点赞 !!!
|