ndk-c 基本数据类型 指针 函数 宏 结构体 共用体

1.基本数据类型

		1.signed  有符号 可修饰 char.、int  
		2.unsighed  无符号 可修饰 int 、char
整型字节
int4
unsigned4
short2
unsigned2
long4
unsigned longlong
char1
unsigned char1
浮点型字节
float4
double8
long double8

2.格式化

//sprintf  用法
char str[100];
sprintf(str,"img/png_%03d.png",1);
// printf
int a = 100;
printf("%d\n",a);

3.内存操作

// 求变量存储空间
int b = 100;
printf("%d\n",sizeof(b));

// malloc 动态存储空间申请 包含于 stdlib.h
int *p = (int *)malloc(sizeof(int));

char*str;
str = (char *)malloc(10 * sizeof(char));
// memset 包含于 string.h 中 用于初始化
memset(str,0,10)

// calloc 申请内存并且初始化
// 初始化 10 个 int 类型的元素
int *p1 = (int *)calloc(10,sizeof(int));

// realloc 对malloc 申请的内存进行大小调整
char *a = (char *)malloc(10);
realloc(a,20); // 扩展为20

// alloca 在栈中申请内存 因此无需释放
int * p = (int *)alloca(10 * sizeof(int));

4. 指针

1.指针是一个变量,用来存储地址
2.声明指针或者不在使用后都 都要将其置零
3.野指针  为初始化的指针
4.悬空指针 最初只想的内存已经被释放了的一种指针
int *a; // 定义指针变量 
int* a;
int *a,b;//

// 使用
int i = 10;
int *p = &i;
printf("%#x\n",&i); // 输出地址
printf("%#x\n",&p);

// 解引用
int i = 10;
int *p = &i;
int pv = *p;
*p = 100 

//指针运算
int i1[] = {11,22,33,44,55};
int *p1 = i1;
for(int i = 0;i < 5;i++){
	printf("%d\n",*p1++);
}

5.数组和指针

int i1[] = {11,22,33,44,55};
printf("%#x\n",i1); // 数组的首地址
printf("%d\n",*i1); // 打印 数组首元素

int array[2][3] = {{11,22,33},{44,55,66}};
// 数组指针
int (*array1)[3] = array;  // array1 是一个指向二维数组的指针
array[1][1]; // 下标获取数组元素
int i = *(*(array1 + 1) + 1);

// 指针数组
int *array2[2];
array2[0] = &i;
array2[1] = &j;

6.const 关键字

1.const 类似于 java 中的 final  表示常量
2. 从左往右 读  观察const 修饰 内容
char str[] = "hello";
const char *p = str;
str[0] = 'c';
p[0] = 'c'; // 报错 const char 不能被修改
p = "12345"; // 这是可以的

char const *p1; // 同 const char *

char *const p2 = str;
p2[0] = 'd';
p2 = "12345" // 错误,const 修饰 p2,p2 指向不能改变

// 指向和内容都不能被改变
char const *const p3 = str;

7.多级指针

指向指针的指针
int a = 10;
int *i = &a;
int **j = &i;
printf("%d\n",**j);

8.函数

// 传值调用: 把参数的值复制给函数的形式参数,修改形参不会影响实参
// 引用调用: 形参为指向实参地址的指针,可以通过指针修改实参
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

/*
	可变参数
*/
#include<stdarg.h>
int add(int num,...){
	va_list valist;
	int sum = 0;
	va_start(valist,num);
	for(int i = 0; i < num;i++){
		// 访问所有赋给 valist 的参数
		int j = va_arg(valist,int);
		printf("%d\n",j);
		sum += j;
	}
	va_end(valist);
	return sum;
}

/*
	函数指针: 指向函数的指针变量
*/
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");

typedef void (*Callback)(int);
void test(Callback callback){
	callback("suc");
	callback("fail");
}
void callback(char *msg){
	printf("%s\n",msg);
}
test(callback);

9.预处理

1.预处理不是编译器,而是编译过程中的一个单独步骤
2.预处理器是一个文本替换工具
3.预处理命令都是以 # 开头
常用预处理器:
预处理器说明
#include头文件导入
#ifif
#elifelse if
#elseelse
endif结束if
#define宏定义
#ifdef如果定义了宏
#ifndef如果为定义宏
#undef取消宏定义

10.宏

// 常量宏
#define A 1
// 宏函数 
#define test(i) i > 10 ? 1 : 0

// 连接符
#define DN_INT(arg) int dn_ ## arg
DN_INT(i) = 10; // int dn_ ## i = 10
dn_i = 100;

// 换行符
#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__);

11.结构体

结构体是C 中一种用户自定义的数据 类型。
	struct Student{
		char name[50];
		int age;
	}student,a; // 定义结构体变量 student, a
typedef struct {
	char name[50];
	int age;
} Student; 
Student stu // 使用 typedef 的别名 进行定义

// 结构体需要内存过大,使用动态内存申请,结构体占用字节数和结构体内字段有关,指针占用内存就是4/8 字节,因此 指针效率更高
Student *s = (Student *)malloc(sizeof(Student));
memset(s,0,sizeof(Student));
// 结构中 的字符数组成员 赋值用stcpy 函数
strcpy(s ->name,"tom"); // 成员字符数组赋值

// 结构体 字节对齐
/*
	1.某个变量存放的起始地址相对于结构体起始位置的偏移量是该变量字节数的整数倍
	2.结构所占用的总字节数是结构中字节数最长的变量 的字节数的整数倍
*/

struct stru1{
	short a;
	int b;
	short c;
};
struct stru2{
	short a;
	short c;
	int b;
}
sizeof(stru1); // 12 Bytes
sizeof(stru2); // 8 Bytes

12. 共用体

1.在相同的内存位置存储不同的数据类型
2.公用体占用的内存应足够存储共用体中的最大成员
union Data{
	int i;
	short j;
};
union Data data;
data.i = 1;
data.j = 12; // i 的数据将失效 不起作用
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值