数组
int a[10];// 数组名a 就是一个指针常量
a = &b;//修改a的值,不行,因为a只能当作指针常量来用
//%c
输出字符
%s
输出字符串
%x 16
进制输出
/*
定义单个字符用单引号
*/
char d='a';
printf("%c \n",d);
/*定义字符串*/
char a[]="chenzhenwen";//数组方式
printf("%s \n", a);
char* sp = "chenzhenwen";//指针方式
printf("地址 %p 字符串 %c \n", sp,*sp);
char input[]="abcde";
char* p = "chenwn";
int i = sizeof(p);//4
int j = sizeof(input);//6
printf("%s %c\n", p,p[1]);//输出字符串用地址,输出字符用解引用
printf("%d %d", i, j);//指针的大小固定,数组的大小要加上'\0'
return 0;
//把字符串中的大写字母换成小写
char *toLowerCase(char s[]) {
int i = 0;
while (s[i] != '\0')
{
if (s[i] >= 'A' && s[i] <= 'Z')
s[i] = s[i] + 32;
i++;
}
return s;
递归
/*
汉诺塔
*/
#include<stdio.h>
void hanoi(int n, char x, char y, char z)
{
if (n == 1)
{
printf("%c --> %c \n", x, z);
}
else
{
hanoi(n - 1, x, z, y);//第一步把n-1个从x借助z移动到y
printf("%c --> %c\n", x, z);//第二步把最后一个从x移动到z
hanoi(n - 1, y, x, z);//第三步把n-1个从y借助于x移动到z
}
}
int main(void)
{
int input;
char x = 'x';
char y = 'y';
char z = 'z';
printf("请输入一个整数:");
scanf_s("%d", &input, 1);
hanoi(input, x, y, z);
return 0;
}
/*
快速排序
*/
#include <stdio.h>
void quickSort(int arr[], int low, int high)
{
int first = low;//0
int last = high;//max
int key = arr[first];
if (low >= high)
return;
while (first < last)
{
while (first < last && arr[last] > key)//从后往前遍历,直到数组
元素的值小于等于 参考值
{
last--;
}
arr[first] = arr[last];//从前往后未比较过的第一个位置即fisrt位置
替换为该数据
while (first < last && arr[first] < key)//再从前往后遍历,直到数
组元素的值大于等于 参考值
{
first++;typedef
}
arr[last] = arr[first];//从后往前的第一个比较过数据位置替换
}
arr[first] = key;
quickSort(arr, low, first - 1);
quickSort(arr, first + 1, high);
}
int main()
{
int i;
int a[10] = { 43, 14, 151, 85, 8,82, 80, 29, 13, 81 };
printf("排序前 \n");
for (i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
printf("\n");
quickSort(a, 0, 9);
printf("排序前 \n");
for (i = 0; i < 10; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
typedef
int*
integer
;
//
数据类型起别名 封装
**
注意有分号
#define inte int*
//
直接替换
*
integer pa
,
ps
;
//int *pa;int *pa;
*
inte pa
,
ps
;
//int *pa,ps;
注意
ps
变成了整形
//typedef
常与结构体用在一起
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct DATA
{
int year;
char* name;
}data;//data为结构体的别名
int main(void)
{
data *amount;
amount = (data *)malloc(sizeof(data));//为指针分配动态内存空间
if(amount==NULL)
exit(1);//内存分配失败
amount->name = "chen";
amount->year = 20;
printf("%d \n",amount->year);
printf("%s \n", amount->name);
return 0;
}
指针
malloc函数使用注意事项
malloc函数的返回的是无类型指针,在使用时一定要
强制转换为所需要的类型
。
重点:在使用malloc开辟空间时,使用完成一定要
释放空间
,如果不释放会造内存泄漏。
在使用malloc函数开辟的空间中,不要进行指针的移动,因为一旦移动之后可能出现申请
的空间和释放空间大小的不匹配
#include<stdio.h>
int main(void)
{
unsigned int a = 'a';
unsigned char d = 'c';
unsigned int* b = &a;
unsigned int** c = &b;
char e[] = "I am happy";
printf("%d ,%d ,%d ,%c \n", a, *b, **c,d);
printf("%s \n", e);
printf("%c \n", e[2]);
return 0;
}
/*
函数内要修改地址是用二级指针
*/
#include<stdio.h>
#include<string.h>
void strs(int** b)
{
printf("子函数内打印&b %p \n",b);
printf("子函数内打印b %p \n", *b);
*b += 7;
**b += 9;
}
int main(void)
{
int a[8] = {100,100,100,0,0,0,0,89};
unsigned int* b = &a[0];
printf("b的地址是 %p ,a的数据值 %d \n", b,a[0]);
strs(&b);
printf("%p \n", b);
printf("%d \n", *b);
return 0;
}#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
int main() {
char* sp;
sp = (char*)malloc(sizeof(char));//**强制转换为所需要的类型**
scanf_s("%s \n", sp,2);
printf("%p", sp);
free(sp);//释放内存空间
return 0;
}
#include<stdio.h>
#include<string.h>
int main() {
int arr[8] = { 3,62,6,32,6,6,16,3 };
int* pa = arr;
int* pb = &arr[3];
int a = pb - pa;
printf("%d \n", a);//3
printf("%p %p \n", pb, pa);//地址差12个字节
return 0;
}
#include<stdio.h>
#include<string.h>
int main() {
int arr[8] = { 13,62,6,32,6,6,16,3 };
int* pa = &arr[0];
int* pb = &arr[3];
int a = pb - pa;
printf("%p \n", pa);
for (char i = 0; i < 8; i++)
printf("%d ", arr[i]);
printf("\n");
for (pa = &arr[0]; pa <= &arr[7]; pa++)//清空数组的内容
*pa = 0;
for (char i = 0; i < 8; i++)
printf("%d ", arr[i]);
printf("\n");
printf(" %p \n", pa);//指针偏移0x20g
return 0;
}
单项链表
#include<stdio.h>
#include<string.h>
char* find_char(char const* str, char const* chars)
{
char* cp;
while (str != NULL && chars != NULL) //不为空指针
{
for (; *str != '\0'; str++) //循环外层
{
for (cp = chars; *cp != '\0'; cp++)//内层循环
if (*str == *cp)//如果找到
return str;
}
}
return NULL;
}
int main() {
char* sount = "abcdee";
char* strs = "dgehht";
char* pa;
pa = find_char(sount, strs);
printf("%c \n", *pa );
}
/*
添加或删除元素
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
struct data
{
int amount;
struct data* next;
};
void insertNode(struct data** head, int amount)//添加
{
struct data* current;
struct data* previous;
struct data* new;
current = *head;
previous = NULL;
while (current != NULL && current->amount < amount)//头指针不为空指
针,而且结构体成员数据小于形参数据
{
previous = current;//
current = current->next;
}
new = (struct data*)malloc(sizeof(struct data));if (new == NULL)
{
printf("内存分配失败");
exit(1);
}
new->amount = amount;
new->next = current;
if (previous == NULL)//空链表
{
*head = new;//头指针指向当前插入的结构体
}
else
{
previous->next = new;//前一个的指针指向当前插入的结构体
}
}
void printfdata(struct data* head)//打印
{
struct data* current;
current = head;
while (current != NULL)
{
printf("%d ", current->amount);
current = current->next;
}
printf("\n");
}
void subdata(struct data** head, int amount)//删除
{
struct data* current=*head;
struct data* previous=NULL;
while (current != NULL && current->amount != amount)
{
previous = current;
current = current->next;
}
if (current == NULL)
{
printf("找不到删除的对象");
return;
}
else
{
if (previous == NULL)//第一个元素就找到了
*head = current->next;
else//后面找到的
{
previous ->next= current->next;
}
free(current);
}}
int main(void)
{
struct data* head = NULL;
int input;
while (1)
{
printf("添加元素:请输入数据(输入-1表示结束)");
scanf_s("%d", &input,1);
if (input == -1)
break;
insertNode(&head, input);//插入链表
printfdata(head);//打印链表成员
}
while (1)
{
printf("删除元素:请输入数据(输入-2表示结束)");
scanf_s("%d", &input, 1);
if (input == -2)
break;
subdata(&head, input);
printfdata(head);
}
return 0;
}