C/C++|物联网开发入门+项目实战|空间读写|非字符空间|返回值内部实现|嵌入式C语言高级|C语言函数的使用(2)-学习笔记(12)


参考: 麦子学院-嵌入式C语言高级-C语言函数的使用

空间的读写

void fun(char *p);
const char *p 只读空间,只为了看
char *p;该空间可能修改,几乎都要变

strcpy();
定义:char *strcpy(char *dest,const char *src);

sprintf();

作用

1、修改 int * short * long *
2、空间传递
2.1 子函数看看空间里的情况 const *
2.2 子函数反向修改上层空间里的内容 char * void * 都代表反向修改
void fun(const int *p) //第一反应仅是上层对下层的查看功能

字符空间:空间首地址,结束标志的不同
结束标志:内存里面存放了0x00(1B),字符空间
非字符空间0x00,不能认为是结束。
void fun(char *p)
{
int i = 0;
p[i]++;
while(p[i]){
p[i]操作 p[i] = x; a = p[i] + -
i++;
}
}

实现strlen

int strlen(const char *p){
/错误处理,判断输入参数是否合法?/
if(p==NULL){
//return…
}
/内存处理,从头到尾逐一处理,遍历/
while(p[i]){
//;
i++;
}
}

实现strcpy

void strcpy(char *dest,const char *src);

“”===>初始化const char*
char buf[10]—>初始化 char *

非字符空间

unsigned char *p;
结束标志:数量(个数)B字节
int *p unsigned char *p
short *p struct abc *p
void * :数据空间的标志
大小

void fun(unsigned char *p,int len)
{
    int i;
    for(i=0;i<len;i++)
        p[i];
        a = p[i] //++++++++++
}
int main()
{
    struct bensor_data buf;

    fun(&buf,sizeof(buf)*1);

}

memcpy函数
void *memcpy (void *dest, const void *src,size_t n);
recv函数:
ssize_t recv(int sockfd,void *buf, size_t len, int flags);
send函数
ssize_t send(int sockfd, const * void xuf, size_t len, int flaqs);

void*

int fun(void *buf,int len)
{
unsigned char *tmp = (unsigned char *)buf;
tmp[i] i++ len
}

返回值

提供启下功能的一种表现形式
基本语法
返回值或者输入地址的传递达到返回信息的目的
返回类型 函数名称(输入列表)
{
return
}
调用者
a = fun(); //生命周期很短
被调者:
int fun()
{
return num;
}

拷贝
示例:

#include <stdio.h>

int fun(void)
{
    return 0x123;
}


int main()
{
    int ret;

    ret = fun(); //拷贝给ret

    printf("the ret is %x\n",ret);
    return 0;
};
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// the ret is 123
//如果函数定义改为char ,则只返回8位
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// the ret is 123

返回基木数据类型
基本数据:int char
指针类型: 空间,不能是数组
int fun(void)
{
return
}

struct abc fun(void); //工程中不建议使用,冗余度太高,应该用连续空间

int fun1(void); int a; a = fun1(); //定义一个一模一样的类型接收就可以

void fun2(int *p); int a = 0; fun2(&a);a //通过输入参数承上启下,传递一个int类型的地址给输入参数,才能够上层对下层进行修改

int fun(int *); //承上启下的功能可以实现2个

int *fun1(void);
int main()
{
int *p;
p = fun1();
}

void fun2(int **p) //变向指向一个地址
int main()
{
int *p;
fun2(&p);
p;
}

返回连续空间类型

return a[10];
指针作为空间返回的唯一数据类型
int *fun();
地址:指向的合法性,作为函数的设计者,必须保证函数返回的地址所指向的空间是合法的,不能是局部变量。

示例:

#include <stdio.h>

char  *fun(void)
{
   char buf[] = "hello world!"; //buf为fun所管理的局部变量

   return buf; //return后buf就回收了
}


int main()
{
    char *p;

    p = fun();
    printf("the p is %s\n",p);
    return 0;
};
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// 2.c: In function 'fun':
// 2.c:7:4: warning: function returns address of local variable [enabled by default]
// the p is `#?
//如果改成常量:
char  *fun(void)
{
   //char buf[] = "hello world!"; //buf为fun所管理的局部变量

   return "hello world"; //常量区是固定的,不会回收
}
//可以正常打印
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// the p is hello world

使用者:
int *fun();
int *p = fun(); //只接收程序地址就可以,定义一个一样的接收即可。

函数内部实现

基本数据类型 fun(void)
{
基本数据类型 ret;
xxxx;
ret = xxxx;
return ret;
}

int fun()
{
int ret = =;
count ++;
ret = xxx;
return ret;
}

地址类型相对复杂:
char *fun()
{
char *s = NULL;
char buf[];
return buf;

}
1.静态区,函数生命周期内
char *fun(void)
{
static char buf[] = “hello world!”; //static以后认为是在静态区,数据段中

return buf;
}
2.只读区,工程上意义不大,可忽略,
char *fun(void)
{
//char buf[] = “hello world!”; //buf为fun所管理的局部变量

return “hello world”; //常量区是固定的,不会回收
}
3.堆区:malloc函数和free函数

示例:

#include <stdio.h>

char  *fun(void)
{
   //static char buf[] = "hello world!"; //static以后认为是在静态区,数据段中
   char *s = (char *)malloc(100); //申请
   strcpy(s,"hello world"); //初始化
   return s;
}


int main()
{
    char *p;

    p = fun();
    printf("the p is %s\n",p);

    free(p); //释放
    return 0;
};
// E:\temp>cd "e:\temp\" && gcc 2.c -o 2 && "e:\temp\"2
// 2.c: In function 'fun':
// 2.c:6:22: warning: incompatible implicit declaration of built-in function 'malloc' [enabled by default]
// 2.c:7:4: warning: incompatible implicit declaration of built-in function 'strcpy' [enabled by default]
// 2.c: In function 'main':
// 2.c:19:5: warning: incompatible implicit declaration of built-in function 'free' [enabled by default]
// the p is hello world
//man malloc 打开手册,会告知你该引用哪个头文件
// 增加:(警告消失)
// #include <string.h>
// #include <stdlib.h>

man fopen
FILE *fopen (constchar *path,const char *mode) ;
返回值指向堆区或者静态区,返回的FILE不是标准类型,具体看说明,但如果是堆区,必须使用后free。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Medlar_CN/article/details/130291944