C/C++ | Introduction to Internet of Things Development + Project Combat | Space Reading and Writing | Non-character Space | Internal Implementation of Return Value | Advanced Embedded C Language | Use of C Language Functions (2) - Study Notes (12)


Reference: Wheat Academy - Embedded C Language Advanced - Use of C Language Functions

space reading and writing

void fun(char *p);
const char *p read-only space, just to see
char *p; this space may be modified, almost always changed

strcpy();
Definition: char *strcpy(char *dest, const char *src);

sprintf();

effect

1. Modify int * short * long *
2. Space transfer
2.1 Sub-function to see the situation in the space const *
2.2 Sub-function reversely modify the content in the upper space char * void * all represent reverse modification
void fun(const int * p) //The first reaction is only the viewing function of the upper layer to the lower layer

Character space: The difference between the first address of the space and the end
flag End flag: 0x00 (1B) is stored in the memory, and the character space
non-character space 0x00 cannot be considered as the end.
void fun(char *p)
{ int i = 0; p[i]++; while(p[i]){ p[i] operation p[i] = x; a = p[i] + - i++; } }






Implement strlen

int strlen(const char *p){ / error handling, to determine whether the input parameters are legal? / if(p==NULL){ //return... } / Memory processing, one by one from beginning to end, traversal / while(p[i]){ //; i++; } }









Implement strcpy

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

""===>Initialize const char*
char buf[10]—>Initialize char*

non-character space

unsigned char *p;
end sign: quantity (number) B bytes
int *p unsigned char *p
short *p struct abc *p void *: sign size
of data space

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 value

Provide a form of expression of the opening function
Basic syntax
The return value or input address is passed to achieve the purpose of returning information
Return type Function name (input list)
{ return } caller a = fun(); //The life cycle is very short and called by: int fun() { return num; }








Copy
example:

#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

Return basic data type
Basic data: int char
pointer type: space, not an array
int fun(void)
{ return }

struct abc fun(void); //It is not recommended to use in engineering, the redundancy is too high, continuous space should be used

int fun1(void); int a; a = fun1(); // just define an identical type to receive

void fun2(int *p); int a = 0; fun2(&a);a //Through the input parameters, pass an address of int type to the input parameters, so that the upper layer can modify the lower layer

int fun(int *); //The connecting function can realize 2 functions

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

void fun2(int **p) // change direction to point to an address
int main()
{ int *p; fun2(&p); p; }



return continuous space type

return a[10];
the pointer is the only data type returned by the space
int *fun();
address: the legality of the pointer, as the designer of the function, you must ensure that the space pointed to by the address returned by the function is legal and cannot be local variable.

Example:

#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

User:
int *fun();
int *p = fun(); //Only receive the address of the program, just define a same receive.

Function internal implementation

Basic data type fun(void)
{ basic data type ret; xxxx; ret = xxxx; return ret; }




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

The address type is relatively complicated:
char *fun()
{ char *s = NULL; char buf[]; return buf;


} 1. Static area, char *fun(void) { static char buf[] = "hello world!"; // static is considered to be in the static area, data segment in the future of the
function life cycle


return buf;
}
2. Read-only area, meaningless in engineering, can be ignored,
char *fun(void)
{ //char buf[] = "hello world!"; //buf is a local variable managed by fun

return "hello world"; //The constant area is fixed and will not be recycled
}
3. Heap area: malloc function and free function

Example:

#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) ;
The return value points to the heap area or static area, the returned FILE is not a standard type, see the description for details, but if it is a heap area, it must be freed after use.
insert image description here

Guess you like

Origin blog.csdn.net/Medlar_CN/article/details/130291944