宏函数:例如:#define ADD ((x)+(y));
1:要保证运算的完整性
2:在一定程度上会比普通函数效率高,省去出栈入栈 时间上的开销。
3:以空间换时间
#include<stdio.h>
#include<stdlib.h>
#define ADD(x,y) ((x) + (y));
int main(){
int a = 40, b = 20,c;
c = ADD(a,b);
printf("%d", c);
}
调用惯例:
1:C语言默认使用cdecl。
2:包含:出栈方(主调函数管理被调函数),参数的传入顺序(从右向左),函数名称的修饰。
变量传递分析:
1:栈:主函数的变量子函数可以使用,自函数的变量主函数不可以使用。注意多级子函数。在子函数中的改变与主函数无关。
2:堆:其在子函数的变量主函数也可以使用只要不释放。在子函数中的改变影响主函数。
栈的生成方向:栈底是高地址,栈顶是低地址。
代码如下:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a = 10;
int b = 9;
int c = 8;
int d = 7;
int e = 6;
int f = 5;
printf("%d\n",&a);
printf("%d\n",&b);
printf("%d\n",&c);
printf("%d\n",&d);
printf("%d\n",&e);
printf("%d\n",&f);
}
内存存储方式:高字节对应高地址,低字节对应低地址。
空指针和野指针;
1:空指针int *p = null; int *p = 0x0002; //胡乱的赋地址不行
2:野指针:
int *p; //这是第一种情况
//这是第二种情况
#include<stdio.h>
void pointer(int *p){
free(p);
p = NULL; //这不是更高级的指针,所以并不能使指针为空。
}
int main(){
int *q = (int *)malloc(sizeof(int)*5);
q[0] = 0;
q[1] = 1;
q[2] = 2;
q[3] = 3;
q[4] = 4;
pointer(q);
}
//这是第三种情况 在子函数中定义的变量在该函数结束后变量被清空。
#include<stdio.h>
int *pointer1(){
int *p;
p[0] = 0;
p[1] = 1;
p[2] = 2;
p[3] = 3;
p[4] = 4;
return p;
}
int main(){
int *q = pointer1();
for(int i=0;i<5;i++)
printf("%d",q[i]);
}
指针的步长:
char指针每加1跳动一个字节。
int指针每加1跳动四个字节。
float指针每加1跳动四个字节。
double指针每加1跳动8个字节。
指针的间接赋值:
#include<stdio.h>
int main(){
int a;
int *p;
p = &a;
*p = 5;
printf("%d",*p);
}
拷贝字符:
//拷贝一
#include<stdio.h>
#include<string.h>
#include<math.h>
void Copy(char str1[], char *str2, int len)
{
for (int i = 0; i < len; i++)
{
str2[i] = str1[i];
}
str2[len] = '\0';
}
int main() {
char str1[1024] = "nihaohaha";
char str2[1024];
int len = strlen(str1);
Copy(str1, str2,len);
printf("%s", str2);
}
//拷贝二
#include<stdio.h>
#include<string.h>
#include<math.h>
void Copy(char *str1, char *str2, int len)
{
for (int i = 0; i <= len; i++)
{
*str2 = *str1;
str2++;
str1++;
}
}
int main() {
char *str1 = "nihaohaha";
char str2[1024];
int len = strlen(str1);
Copy(str1, str2,len);
printf("%s", str2);
}
//拷贝三
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main() {
char *str1 = "nihaohaha";;
int len = strlen(str1);
char *str2 = malloc(sizeof(char) * (len + 1)); //void返回的是万能指针void*
memcpy(str2, str1, (len + 1));
printf("%s", str2);
}
指针的反转:
#include<stdio.h>
#include<string.h>
#include<math.h>
void Copy(char* str1, char* str2, int len)
{
str1 = str1 + len - 1;
for (int i = 0; i <= len; i++)
{
*str2 = *str1;
str2++;
str1--;
}
}
int main() {
char* str1 = "nihaohaha";
char str2[1024];
int len = strlen(str1);
Copy(str1, str2, len);
printf("%s", str2);
}
sprintf()字符串拼接/格式化字符串:
#include<stdio.h>
#include<string.h>
#include<math.h>
int main() {
char str[1024];
int year = 100;
sprintf(str, "哇塞,我今年已经%d岁了", year);
printf("%s", str);
}
malloc(sizeof()*size) , calloc(size,sizeof()) , realloc(primary,sizeof()*size)
sscanf()的使用:
#include<stdio.h>
#include<string.h>
#include<math.h>
int main() {
char exp1[1024] = "ascvde@你猜我去掉了啥?哈哈。";
char exp2[1024] = "buhaohou1234567";
char str1[1024], str2[1024], str3[1024];
sscanf(exp1, "%[a-z]%*[@]%s", str1,str2);
sscanf(exp2, "%*8s%s",str3);
printf("%s%s\n", str1,str2);
printf("%s",str3);
}
查找子串:
#include<stdio.h>
#include<string.h>
#include<math.h>
int Compare(char* exp, char* find) {
char *e1 = exp;
char *f1 = find;
int len1=0, len2=0, len3=0, lene;
len1 = strlen(find);
lene = strlen(exp);
while (*e1 != '\0') {
char *e2 = e1;
char *f2 = f1;
if (*f2 == *e2) {
while (*e2 != '\0')
{
len2++;
f2++;
e2++;
if (*f2 != *e2) {
break;
}
}
}
if (len2 == len1)
break;
else {
e1++;
e2 = e1;
f2 = f1;
len3++;
len2 = 0;
}
}
if (len3 < lene)
return len3+1;
else
return -1;
}
int main() {
char *exp = "asdfgh";
char *find = "fgh";
int len;
len = Compare(exp, find);
if (len == -1)
printf("没有连续相同的选项");
else
printf("有相同的连续选项,初始位置为%d", len);
}
使用const定义形参说明这个值不能修改。不能在子函数中修改。
二级指针–文件读写:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int getFileLine(FILE* file) {
int line = 0;
char butf[1024];
while (fgets(butf, 1024, file) != NULL)
{
line++;
}
fseek(file,0,SEEK_SET);
return line;
}
void getFileElement(FILE *file, char **filetext, int len)
{
char butf[1024];
int index = 0;
while (fgets(butf, 1024, file) != NULL)
{
int num = strlen(butf) + 1;
char *current_file = (char *)malloc(sizeof(char)*num);
strcpy(current_file, butf);
filetext[index++] = current_file;
memset(butf,0,1024);
}
}
void showFileElement(char **filetext, int len) {
for (int i = 0; i < len; i++)
{
printf("%s", filetext[i]);
}
}
void clearPile(char **filetext, int len)
{
for (int i = 0; i < len; i++)
{
free(filetext[i]);
filetext[i] = NULL;
}
free(filetext);
filetext = NULL;
}
int main() {
FILE* file = fopen("text.txt", "r");
int len = getFileLine(file);
char** filetext = (char **)malloc(sizeof(char*) * len);
getFileElement(file, filetext, len);
showFileElement(filetext, len);
clearPile(filetext, len);
}