长时间不用c基础代码,导致有些基础语法忘记了,今天抽空写个小demo回顾下
#include <cstdio> #include "main.h" typedef struct { int a; int b; }obj; // void func(obj* pInfo) { int m = pInfo->a; int n = pInfo[0].a;//此写法与上面写法相同 printf("%d,%d ", m, n); } void func1(obj(*pInfo)[10] ) { for (int i = 0; i < 10; i++) { (*pInfo)[i].a = 10; (*pInfo)[i].b = 11; pInfo[0][i].a = 10;//此写法与上面写法相同 pInfo[0][i].b = 11; } } int main() { //对象指针 obj slaveInfo; slaveInfo.a = 10; slaveInfo.b = 11; func(&slaveInfo); //对象数组指针 obj slaveInfos[10]; func1(&slaveInfos); for (int i = 0; i < 10; i++) { printf("%d,=%d ", i,slaveInfos[i].a); } return 0; }
执行结果: