一、结构体与指针
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#define up_to_down(uuu) (downdemo_t *)(uuu->beg)
#define __plc__
typedef struct updemo_s{uint8_t *head;uint8_t *beg;uint8_t *end;
}updemo_t;
typedef struct downdemo_s{uint8_t value;uint16_t type;uint8_t mmentry[0];
}downdemo_t;
typedef struct mmsg_s{uint8_t hh;uint8_t hhh;
}mmsg_t;
__plc__ int main(int argc, char* argv[])
{updemo_t *uu;downdemo_t *dd;mmsg_t *mm;uu=(updemo_t *)malloc(sizeof(updemo_t));uu->beg=malloc(sizeof(downdemo_t));dd=up_to_down(uu);dd->value=4;dd->type=8;mm=(mmsg_t *)dd->mmentry;mm->hh=10;mm->hhh=12;printf("dd->value: %d\n",dd->value);printf("dd->type: %d\n",dd->type);printf("mm->hh: %d\n",mm->hh);printf("mm->hhh: %d\n",mm->hhh);return 0;
}
二、sizeof
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char* argv[])
{uint8_t a[1];uint8_t b[0];uint8_t *c;printf("a[1]: %ld a: %\n",sizeof(a));printf("b[0]: %ld\n",sizeof(b));printf("*c: %ld\n",sizeof(c));return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
int main(int argc,char* argv[])
{uint8_t a[1];uint8_t b[0];uint8_t *c;int *d;char e;int f;char g[]={"1234\0"};printf("a[0]: %d sizeof(a): %ld\n",a[0],sizeof(a));printf("b[0]: %d sizeof(b): %ld\n",b[0],sizeof(b));printf("*c: %d sizeof(c): %ld\n",*c,sizeof(c));printf("sizeof(d): %ld\n",sizeof(d));printf("sizeof(e): %ld\n",sizeof(e));printf("sizeof(f): %ld\n",sizeof(f));printf("sizeof(g): %ld\n",sizeof(g));return 0;
}
三、static
#include <stdio.h>
void func1(void);
static int count=10;
int main()
{while(count--){func1();}return 0;
}
void func1(void)
{static int thingy=5;thingy++;printf("thingy is %d ,count is %d\n",thingy,count);
}
四、extern
first.c
#include <stdio.h>int count;
extern void write_extern();int main()
{count=5;write_extern();
}
support.c
#include <stdio.h>extern int count;void write_extern(void)
{printf("count is %d\n",count);
}
五、函数指针数组
#include <stdio.h>
#include <stdlib.h>int a()
{printf("\n1\n");return 1;
}
int b()
{printf("\n2\n");return 2;
}
int c()
{return 3;
}
int d()
{return 4;
}
int (*oper_func[])(void)={a,b,c,d};
int main()
{for(int i=0;i<4;i++){printf("i:%d oper_func[i]:%d *oper_func[i]:%d oper_func[i]():%d\n",i,oper_func[i],*oper_func[i],oper_func[i]());}
}
六、函数指针
#include <stdio.h>int (*p1)(int, int);
int max(int x, int y)
{return x > y ? x : y;
}int main()
{int (*p2)(int, int)= max;p1=max;int a,b,c,d;printf("请输入三个数:");scanf("%d %d %d",&a, &b, &c);d=p1(p1(a,b),c);printf("p1最大的数d是:%d p1:%d\n",d,p1);printf("p2(p2(a,b),c):%d p2:%d\n",p2(p2(a,b),c),p2);return 0;
}
七、回调函数
#include <stdio.h>
#include <stdlib.h>void populate_array(int *array, size_t arraySize, int(*getNextValue)(void))
{for(size_t i=0; i<arraySize;i++){array[i]= getNextValue();}
}int getNextRandomValue(void)
{return rand();
}int main()
{int myarray[10];populate_array(myarray,10,getNextRandomValue);for(int i=0; i<10; i++){printf("%d\n",myarray[i]);}return 0;
}