[2] NDK series pointer, the function, the preprocessor

1, the pointer

Pointer is a variable whose value is the address.

Statement pointer or after it is no longer used to be set to 0 (NULL)

Wild pointer uninitialized pointer

Dangling pointer pointer initially points to memory that has been released a pointer

int *a; 正规
int* a;
int * a;
//因为 其他写法看起来有歧义
int* a,b;

Specific use

//声明一个整型变量
int i = 10;
//将i的地址使用取地址符给p指针
int *p = &i;

//输出 0xffff 16进制地址
printf("%#x\n", &i);     0xdaf3bab4
printf("%#x\n", &p);     0xdaf3baa8

How many bytes pointer? Point to address is the address

In the 32-bit address pointer 64 occupies 4 bytes of 8

//32位:
sizeof(p) == 4;
//64位:
sizeof(p) == 8;

Dereference

Parses and returns the value stored in memory address

int i = 10;
int *p = &i;
//解引用  
//p指向一个内存地址,使用*解出这个地址的值 即为 10
int pv = *p;
//修改地址的值,则i值也变成100
//为解引用的结果赋值也就是为指针所指的内存赋值
*p = 100;

printf("%d\n", i);    100
printf("%d\n", *p);   100
printf("%d\n", pv);   10

Pointer arithmetic

int i1[] = {11,22,33,44,55};
int *p1 = i1;
//*p1 指向第一个数据 11,移动指针就指向第二个了
for (size_t i = 0; i < 5; i++)
{
	printf("%d\n", *p1++); 
}

! [Pointer movement] (Art \ pointer movement .png)

Arrays and pointers

In the c language, pointers and arrays were expressed Address

1, an array is a contiguous data memory.

2, the pointer is a variable memory space

int i1[] = {11,22,33,44,55};

//直接输出数组名会得到数组首元素的地址
printf("%#x\n",i1);   0xdaf3baa0

//解引用
printf("%d\n",*i1);   11
    
//将数组名赋值给一个指针,这时候指针指向数组首元素地址
int *p1 = i1;

Array pointer

//二维数组类型是 int (*p)[x]

int array[2][3] = { {11,22,33},{44,55,66} };

//array1 就是一个 int[3] 类型的指针
int (*array1)[3] = array;


//怎么取 55 ?

//通过下标
array[1][1] == array1[1][1]
//通过解引用
int i = *(*(array1 + 1) + 1);

 printf("%d\n", i);      55

Pointer array

int *array2[2];
array2[0] = &i;
array2[1] = &j;

const

** const char ***: Constant = final

char str[] = "hello";
const char *p = str;
str[0] = 'c'; //正确
p[0] = 'c';   //错误 不能通过指针修改 const char


// 可以修改p的指向,指向其他的数据
p = "12345";

**char const ***

//性质和 const char * 一样
char const *p1;

char * const

char str[] = "hello";
//p2是一个const指针 指向char类型数据
char * const p2 = str;
p2[0] = 'd';  //正确
p2 = "12345"; //错误

char const const*

//p3是一个const的指针变量 意味着不能修改它的指向
//同时指向一个 const char 类型 意味着不能修改它指向的字符
//集合了 const char * 与  char * const
char const* const p3 = str;

Multi-level indicators

A pointer to a pointer

It contains a pointer to the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, the second pointer to an actual position values.

int a = 10;
int *i = &a;
int **j = &i;
// *j 解出 i   
printf("%d\n", **j);

Multi-level pointers sense!

Cited by value function

2, function

The function C no difference with the java. Is a set of statements together to perform a task, but also by the function of the head and the body of the function configuration

Declared before use

Call-by

Copies the value of the parameter to the formal parameters of the function. Modified parameter argument does not affect

Call by reference

Parameter argument is a pointer to the address may be modified by the pointer argument.

void change1(int *i) {
	*i = 10;
}
void change2(int *i) {
	*i = 10;
}
int i = 1;

change1(i);
printf("%d\n",i); //i == 1

change2(&i);
printf("%d\n",i); //i == 10

variable parameter

Like Java, C which are also variable parameters

#include <stdarg.h>

int addR(int num, ...) {
    va_list valist;
    int sum = 0;
    // 初始化  
    va_start(valist, num);
    for (size_t i = 0; i < num; i++) {
        //访问参数
        int j = va_arg(valist, int);
        printf("%d\n", j);
        sum += j;
    }
    //清理
    va_end(valist);
    return sum;
}

   // 调用
   int sum =  addR(3, 21, 122, 32);
   printf("sum :%d\n", sum);    175

Function pointer

Function pointer variable is a pointer to function

void println(char *buffer) {
	printf("%s\n", buffer);
}

//接受一个函数作为参数
void say(void(*p)(char*), char *buffer) {
	p(buffer);
}

void(*p)(char*) = println;
p("hello");

//传递参数
say(println, "hello");

//typedef 创建别名 由编译器执行解释
typedef void(*Fun)(char *);
Fun fun = println;
fun("hello");
say(fun, "hello");



//类似java的回调函数
typedef void(*Callback)(int);

void test(Callback callback) {
	callback("成功");
	callback("失败");
}
void callback(char *msg) {
	printf("%s\n", msg);
}

test(callback);

// 不使用别名则是这么调用
void (*p)(char *) = callback;
test(p);

3, Preprocessor

Not the compiler pre-processor, it is compiled in a single process step.

The preprocessor is a text replacement tool

All orders are pre-processor with a pound sign (#) at the beginning

Common preprocessor

Preprocessor Explanation
#include Import header file
#if if
#elif else if
#else else
#endif End if
#define Macro definition
#ifdef If you define macros
#ifndef If the macro is not defined
#undef Cancel the macro definition

Macros

The preprocessor is a text replacement tool

Acer is the text replacement

//宏一般使用大写区分
//宏变量
//在代码中使用 A 就会被替换为1
#define A 1

Macro Functions

//宏函数
#defind test(i) i > 10 ? 1: 0

other

// \ 换行符
#define PRINT_I(arg) if(arg) { \
 printf("%d\n",arg); \
 }
PRINT_I(dn_i);

//可变宏
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,"NDK", __VA_ARGS__);



Macro Functions

Advantages:

Text replacement, each use to place the macro definition will be replaced.

It will not cause the overhead of function calls (open stack space, record the return address, the parameter push to return from the function but also the release of the heap

Stack. )

Disadvantages:

The goal of generating a large file, does not execute code checks

Inline functions

Macro function and mode of operation is similar, but two different concepts, the first is a function, then there will be but can also debug type checking
at compile time to insert an inline function.

Can not contain complex control statements, while, switch, and an inline function itself can not call itself directly.
If the function inline function body is too large, the compiler will automatically put the inline function becomes normal function.

Code

https://github.com/ddssingsong/AnyNdk

Guess you like

Origin blog.csdn.net/u011077027/article/details/92838144