/*
比如定义了一个长度为10的结构体数组,但是现在只用了5个长度,如何只输出这5个有效长度的信息?
*/
4 回答
已采纳
MinRam
TA贡献87条经验 获得超24个赞
#include<stdio.h>
#include<malloc.h>
typedef struct {
int useLen; // 有效长度
int *data; // 实际数据
int allLen; // 实际容量
}StruTest;
#define ALL_LEN 10
int main(void) {
int len;
StruTest * tmp = (StruTest *)malloc(sizeof(StruTest)); // 申请一个结构体
printf("有效长度:");
scanf("%d", &len);
tmp->allLen = ALL_LEN;
tmp->useLen = len;
tmp->data = (int *)malloc(ALL_LEN*sizeof(int));
printf("输入数组");
for (int i = 0; i < tmp->useLen; ++i) {
scanf("%d", &tmp->data[i]);
}
printf("该结构体的内容如下:");
for (int i = 0; i < tmp->useLen; ++i) {
printf("%d ", tmp->data[i]);
}
free(tmp->data);
free(tmp);
}
Xyino_Snake
TA贡献31条经验 获得超22个赞
你所说的话有歧义。我按两个意思分别回答你。
1.用带长度的指针模板。
tpl<tpn type>
stc hn_type
{
type * h;
int n;
//其他方法自己写吧。
};
h是指针,n是长度。只需再接一个专门接口即可。
2.后面的就不用处理也就是了。
- 4 回答
- 1 关注
- 1979 浏览
添加回答
举报
0/150
提交
取消
