嵌入式学习--C语言Day09
扩展
strlen计算字符串实际元素个数,不包括'\0'
size_t strlen(const char *s);
参数:地址
返回值:int
int myStrlen(char *p)
{
}
#include<stdio.h>
int fun(char *p)
{
int sum = 0;
for ( ; *p!='\0'; p++)
sum++;
return sum;
}
int main(int argc, char const *argv[])
{
char a[30] = {0};
scanf("%[^\n]",a);
getchar();
printf("%d\n",fun(a));
return 0;
}
地址接收形式
#include<stdio.h>
void fun(char p[99]) // char *p ; char p[] 接收字符串首地址
{
printf("%ld\n",sizeof(p)); //8
printf("%s\n",p);
}
int main(int argc, char const *argv[])
{
char a[33]="hello";
fun(a);
}
函数传参
形参和实参的区别
形参:形式上的参数,只有在调用时,才去开辟空间
实参:调用函数时,传递的实际的值,真实存在
值传递
单向传递
实际传递的是变量的值,子函数接收的值在子函数中任意修改,不会影响主函数
#include<stdio.h>
void fun(int m,int n)
{
m=33;
n+=1;
}
int main(int argc, char const *argv[])
{
int a=88,b=99;
fun(a,b);//fun(88,99)
printf("%d %d\n",a,b);//88 99
return 0;
}
间接传递
双向传递
实际传递的是变量的地址
#include<stdio.h>
void fun(int *m,int *n)
{
*m=33;
*n+=1;
}
int main(int argc, char const *argv[])
{
int a=88,b=99;
fun(&a,&b);
printf("%d %d\n",a,b);//33 100
return 0;
}
数组传递
本质是地址传递
#include<stdio.h>
void fun(char *p)
{
*(p+2)='a';
}
int main(int argc, char const *argv[])
{
char c[33]="hello";
fun(c);
printf("%s\n",c);
return 0;
}
栈区:系统自动开辟,并自动回收
函数体内部定义的变量,只在调用这个函数时存活,
#include<stdio.h>
char * fun()
{
// char a[33]="hello"; //栈区开辟33字节存储hello,返回值:栈区空间首地址
char *a="hello";//常量区存储hello, 把常量区首地址返回出去
return a;
}
int main(int argc, char const *argv[])
{
char *p=fun();
printf("%s\n",p);
return 0;
}
堆区空间
需要手动开辟 malloc
使用完手动回收 free
malloc
#include <stdlib.h>
void *malloc(size_t size);
功能:开辟堆区空间
参数:开辟空间大小 字节
返回值:堆区空间的首地址(失败:NULL)
int *p = (int *)malloc(sizeof(int)*5);//20
//开辟20字节的堆区空间,首地址赋值给p
if(p==NULL)
perror("error\n");//报错
free
#include <stdlib.h>
void free(void *ptr);
功能:回收堆区空间
参数:堆区空间首地址
返回值:无
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char const *argv[])
{
int *p = (int *)malloc(sizeof(int)*5);//20
//开辟20字节的堆区空间,首地址赋值给p
if(p==NULL)
perror("error\n");//报错
free(p);//回收空间
p = NULL;//指针置空,防止野指针
}
思考:如下代码块 输出结果
void fun(char *p)
{
p = (char*)malloc(32);
strcpy(p,"hello");
}
int main(int argc, char const *argv[])
{
char *m =NULL;
fun(m);
printf("%s\n",m);
return 0;
}
错误原因,怎么解决
地址传递
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void fun(char **p)
{
*p = (char*)malloc(32);
strcpy(*p,"hello");
}
int main(int argc, char const *argv[])
{
char *m =NULL;
fun(&m);
printf("%s\n",m);
return 0;
}
返回值传递
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *func()
{
char *p = (char*)malloc(32);
strcpy(p,"hello");
return p;
}
int main(int argc, char const *argv[])
{
char *m =NULL;
m = func();
printf("%s\n",m);
free(m);
m = NULL;
return 0;
}
string函数族
strcpy strcat strcmp strlen
strcpy
功能:字符串复制(把后面的复制给前面,带'\0')
参数:目标字符串首地址,源字符串首地址
返回值:目标字符串首地址
#include <string.h>
char *strcpy(char *dest, const char *src);
#include<stdio.h>
#include<string.h>
int main(int argc, char const *argv[])
{
char a1[10] = "hellower";
char a2[10] = "world";
strcpy(a1,a2);
printf("%s\n",a1);//world
printf("%s\n",a2);//wrold
for (int i = 0; i < 8; i++)
{
printf("%d ",a1[i]);//119 111 114 108 100 0 101 114
}
return 0;
}
char *strncpy(char *dest, const char *src, size_t n);
n 复制前n个字符
strcat
功能:字符串拼接
参数:目标字符串首地址,源字符串首地址
返回值:目标字符串首地址
#include <string.h>
char *strcat(char *dest, const char *src);
#include<stdio.h>
#include<string.h>
int main(int argc, char const *argv[])
{
char a1[10] = "hellower";
char a2[10] = "world";
strcat(a1,a2);
printf("%s\n",a1);//hellowerworld
return 0;
}
strcmp
功能:字符串比较
参数:要比较的两个字符串首地址
返回值:s1<s2 <0
s1=s2 =0
s1>s2 >0
#include <string.h>
int strcmp(const char *s1, const char *s2);
#include<stdio.h>
#include<string.h>
int main(int argc, char const *argv[])
{
char b1[33] = "azbzz";
char b2[33] = "bzzzz";
int sum = strcmp(b1,b2);
printf("%d\n",sum);//-1
return 0;
}