JNI之C入门

什么是jni

C

数据类型

输出函数

不乱用占位符

也可以是 双引号

输入函数

内存地址

内存地址修改

指针入门

指针常见的错误

指针必须指向 是代码里申请的内存,才能操作指针

指针用处:交换两个数的值

指针用处:一个函数返回多个值(其实直接改)

数组和指针之间的关系

指针变量长度

多级指针

多级指针应用

15_栈内存和静态内存分配

堆内存_动态内存分配

#include<stdio.h>    
#include<stdlib.h>    
/**
java new对象就会申请一块堆内存
c   malloc memory allocation 内存分配 
c的堆内存 程序员手动申请手动释放  malloc
free 
申请一块堆内存 动态内存分配 
堆内存 不连续的 
堆内存大小不固定 取决机器的状态 
*/
main(){ 
        //malloc 接收的参数 申请内存大小 返回一个内存地址值 申请到的也是一块连续的内存空间   
      int* pointer = malloc(sizeof(int)*5);
      *(pointer+0) = 1;
      *(pointer+1) = 2;
      *(pointer+2) = 3;
      *(pointer+3) = 4;
      *(pointer+4) = 5;
      //C for 循环 循环的临时变量i 要先声明再使用 
      int i;
      for(i = 0;i<5;i++){
              printf("第%d个元素的值= %d\n",i,*(pointer+i));
              }
      free(pointer);
      printf("第一个元素的值%d\n",*(pointer+0));
       system("pause"); 
       } 

复制代码

16_学生学号管理系统

扩展内存

#include<stdio.h>    
#include<stdlib.h>    
/**
保存班级人数
申请一块堆内存保存学生的学号
来了几个插班生
扩展一下堆内存
保存插班生的学号 
realloc re- 
*/
main(){ 
       printf("请输入班级的人数:");
       int count;
       scanf("%d",&count);
       //申请一块堆内存
       int* pointer = malloc(sizeof(int)*count);
       int i;
       for(i = 0;i<count;i++){
             printf("请输入第%d个学生的学号:",i+1);
             scanf("%d", pointer+i);
             }  
       for(i = 0;i<count;i++){
             printf("第%d个学生的学号是:%d\n",i+1,*(pointer+i));  
             } 
       printf("请输入插班生的人数:");
       //声明一个变量increment用来保存 插班生的人数 
       int increment;
       //接受用户的输入 
       scanf("%d",&increment);
       //重新申请一块足够大的内存 
       //如果 malloc申请到的内存后面还有足够的空间 realloc会在malloc申请的内存空间后继续申请足够大的内存空间
       //如果 malloc申请到的内存后面没有足够的空间 realloc会找到一块足够大的堆内存 并且把 malloc申请到的内存中的值复制过来 
      pointer = realloc(pointer,sizeof(int)*(count+increment));
      for(i = count;i<count+increment;i++){
             printf("请输入第%d个学生的学号:",i+1);
             scanf("%d", pointer+i);
            }
      for(i = count;i<count+increment;i++){
            printf("第%d个学生的学号是:%d\n",i+1,*(pointer+i));  
            }
       system("pause"); 
       } 

复制代码

结构体

函数指针

结构体指针

#include<stdio.h>    
#include<stdlib.h>    
/**
c结构体 类似java的class  struct来声明c的结构体 
结构体的大小大于等于结构体中每一变量的占字节数的和
 结构体的大小是最大的那个变量所占字节数的整数倍 
 C结构体中不能定义函数  
 
 函数指针的定义   返回值(*函数指针变量名字)(返回值); 
 -> 间接引用运算符   
*/
void study(){
           printf("good good study!\n");
           }
typedef struct Student{
      int age;  //8
      int score;  // 4
      char sex;   //1
      void(*studypointer)();
      } stud;
main(){    
 stud stu = {18,100,'f'};
 stu.studypointer = &study;
 stu.studypointer();
 struct Student* stuPointer = &stu;
 printf("*stuPointer.age = %d\n",(*stuPointer).age);
 (*stuPointer).sex ='m';
 printf("stu.sex = %c\n",stu.sex);
 printf("stuPointer->age = %d",stuPointer->age);
 //printf("stu.age = %hd\n",stu.age);
 //printf("stu.score = %d\n",stu.score);
 //printf("stu.sex = %c\n",stu.sex);
// printf("结构体student占%d个字节\n",sizeof(stu));
       system("pause"); 
       } 

复制代码

联合体

枚举

自定义类型

转载于:https://juejin.im/post/5cfe0797518825063257e5c1

猜你喜欢

转载自blog.csdn.net/weixin_34337265/article/details/93169166
今日推荐