结构体与结构体指针数组
1. 上节课作业讲解
#include <stdio.h>
#include <string.h>
void substrAction1(char * result, char * str, int start, int end) {
char * temp = str;
int count = 0;
while (*temp) {
if (count >= start && count < end) {
*result = *temp;
result++;
}
temp++;
count++;
}
}
void substrAction2(char ** result, char * str, int start, int end) {
char * temp = str;
char resultArr[end - start];
int count = 0;
for (int i = start; i < end; ++i) {
resultArr[count] = *(temp + i);
count++;
}
strcpy(*result, resultArr);
printf("%s\n", resultArr);
}
void substrAction3(char * result, char * str, int start, int end) {
for (int i = start; i < end; ++i) {
*(result++) = *(str+i);
}
}
void substrAction4(char * result, char * str, int start, int end) {
strncpy(result, str+start, end - start);
}
int main() {
char * str = "Derry is";
char * result;
substrAction4(result, str, 2, 5);
printf("main 截取的内容是:%s", result);
return 0;
}
#include <stdio.h>
int main() {
int a[] = {6,4,8,3,1,2,9,7,0,5};
int result = a[*(a+*(a+4))];
printf("result的值是:%d\n", result);
return 0;
}
2. 结构体定义与使用
#include <stdio.h>
#include <string.h>
struct Dog {
char name[10];
int age;
char sex;
};
int main() {
struct Dog dog;
printf("name:%s, age:%d, sex:%c \n", dog.name, dog.age, dog.sex);
strcpy(dog.name, "旺财");
dog.age = 3;
dog.sex = 'G';
printf("name:%s, age:%d, sex:%c \n", dog.name, dog.age, dog.sex);
return 0;
}
#include <stdio.h>
struct Person {
char * name;
int age;
char sex;
} ppp = {"Derry", 33, 'M'},
ppp2,
ppp3,
pppp4,
pppp5
;
int main() {
printf("name:%s, age:%d, sex:%c \n", ppp.name, ppp.age, ppp.sex);
pppp5.name = "DerryO";
pppp5.age = 4;
pppp5.sex = 'M';
printf("name:%s, age:%d, sex:%c \n", pppp5.name, pppp5.age, pppp5.sex);
return 0;
}
#include <stdio.h>
struct Study {
char * studyContent;
};
struct Student {
char name[10];
int age;
char sex;
struct Study study;
struct Wan {
char * wanContent;
} wan;
};
int main() {
struct Student student =
{"李元霸", 88, 'm',
{"学习C"},
{"王者农药"}
};
printf("name:%s, age:%d, sex:%c,study:%s, wan:%s \n",
student.name, student.age, student.sex, student.study.studyContent, student.wan.wanContent);
return 0;
}
3. 结构体指针与动态内存开辟
|