二维数组
Posted porkerface
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二维数组相关的知识,希望对你有一定的参考价值。
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> typedef struct _Ateacher { int id; char *name; char **stu; }Teacher_t; Teacher_t *createTeacher(int n) { int i = 0, j = 0; Teacher_t *st = NULL; st = (Teacher_t *)malloc(n*sizeof(Teacher_t)); for (i = 0; i < n; i++) { st[i].name = (char *)malloc(100); st[i].stu = (char **) malloc(sizeof(char *)); for (j = 0; j < n; j++) { st[i].stu[j] = (char *)malloc(100); } } return st; } void printst(Teacher_t *st) { int i = 0, j = 0; for( i = 0; i < 3; i++) { printf("Tname: %s, Tid: %d ->>> stu info", st[i].name, st[i].id); for ( j = 0; j < 3; j ++) { printf(" Sname%d: %s", j, st[i].stu[j]); } printf(" "); } } int main() { Teacher_t *st = NULL; int i = 0, j = 0; st = createTeacher(3); for( i = 0; i < 3; i++) { st[i].id = i; printf("input name [%d]: ", sizeof(st[i].name)); scanf("%s", st[i].name); printf("input stu info "); for ( j = 0; j < 3; j ++) { printf(" [%d]input name%d: ", sizeof(st[i].stu[j]), j); scanf("%s", st[i].stu[j]); } } printst(st); return 0; }
二维指针及结构体使用
以上是关于二维数组的主要内容,如果未能解决你的问题,请参考以下文章
leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段