C语言实现动态数组的问题

lzh3ng 2009-10-07 04:37:31
那位大侠帮忙看看,为什么不能实现动态增加?
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 int insert(char (**namep)[20], int (**scorep)[5],unsigned int *np,
5 char *namebuf, int *scorebuf)
6 {
7 char (*temp_name)[20] = NULL;
8 int (*temp_score)[5] = NULL;
9
10 if ( (namep==NULL) || (scorep==NULL) || (np==NULL)
11 || (namebuf==NULL) || (scorebuf==NULL))
12 {
13 return -1;
14 }
15
16 temp_name = (char (*)[20])malloc(sizeof(char(*)[20])*(*np+1));
17
18 if (temp_name == NULL)
19 {
20 return -1;
21 }
22
23 temp_score = (int (*)[5])malloc(sizeof(int(*)[5])*(*np+1));
24
25 if (temp_score == NULL)
26 {
27 free(temp_name);
28 return -1;
29 }
30 free(temp_name);
31
32 temp_name = *namep;
33 free(temp_score);
34 temp_score = *scorep;
35
36 memcpy(temp_name, *namep, sizeof(char(*)[20])*(*np));
37 temp_name += *np;
38 memcpy(temp_score, *scorep, sizeof(int(*)[5])*(*np));
39 temp_score += *np;
40
41 memcpy(temp_name, namebuf, sizeof(char(*)[20])*(*np+1));
42 memcpy(temp_score, scorebuf, sizeof(int(*)[5])*(*np+1));
43
44 *np +=1;
45 return 0;
46
47 }
48
49 void print(char (*name)[20], int (*score)[5], int n)
50 {
51 int i, j;
52 printf("\tsubject1 subject2 subject3 subject4 subject5\n");
53
54 for (i=0; i<n; i++)
55 {
56 printf("%s", name[i]);
57
58 for (j=0; j<5; j++)
59 {
60 printf("%9d",score[i][j]);
61 }
62
63 printf("\n");
64 }
65
66 }
67
68 int main(void)
69 {
70 char (*name)[20] = NULL;
71 int (*score)[5] = NULL;
72 unsigned int n=0;
73 int a, i;
74 char *namebuf = NULL ;
75 int *scorebuf = NULL;
76
77
78 while (1)
79 {
80 printf("\t\tmemu\n");
81 printf("1.insert\n");
82 printf("2.print\n");
83 printf("3.exit\n");
84
85 scanf("%d", &a);
86 getchar();
87
88 if (a==1)
89 {
90 system("clear");
91 printf("input name:\n");
92 scanf("%s",namebuf);
93 getchar();
94
95 for (i=0; i<5; i++)
96 {
97 printf("input subjet%d:\n", i+1);
98 scanf("%d", scorebuf);
99 getchar();
100 }
101
102 insert(&name, &score, &n, namebuf, scorebuf);
103 }
104
105 else if (a==2)
106 {
107 system("clear");
108 print(name,score, n);
109 }
110
111 else if (a==3)
112 {
113 break;
114 }
115
116 else
117 {
118 printf("input error!\n");
119 }
120 }
121
122 return 0;
123 }
...全文
198 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
qepjatdwj 2009-10-08
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090828/08/bf479912-b538-4412-9a00-525ae9ee9339.html
lzh3ng 2009-10-08
  • 打赏
  • 举报
回复
就是在这里把我搞糊涂了,怎么调试都不对。
viky2003 2009-10-07
  • 打赏
  • 举报
回复
这个代码的本意应该是将分配好的空间付给传进来的参数:har (**namep)[20], int (**scorep)[5]
为什么上面malloc好了,又free掉呢?看起来比较乱。


free(temp_name);
31
32 temp_name = *namep;
33 free(temp_score);
34 temp_score = *scorep;
35
36 memcpy(temp_name, *namep, sizeof(char(*)[20])*(*np));
37 temp_name += *np;
38 memcpy(temp_score, *scorep, sizeof(int(*)[5])*(*np));
39 temp_score += *np;


这个char的指针直接付给char(*)[20]:
memcpy(temp_name, namebuf, sizeof(char(*)[20])*(*np+1));
是否觉得比较奇怪。
wanjingwei 2009-10-07
  • 打赏
  • 举报
回复
你的几个指针都没分配空间,后面又往里面写内容肯定就出错了
lzh3ng 2009-10-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 reff1 的回复:]
楼主,我不想说你了
一堆错误
[/Quote]

能指出来一下吗?
不甚感激!
reff1 2009-10-07
  • 打赏
  • 举报
回复
楼主,我不想说你了
一堆错误
lzh3ng 2009-10-07
  • 打赏
  • 举报
回复
编译时出现如下警告:
dynamic_array.c: 在函数‘insert’中:
dynamic_array.c:36: 警告:隐式声明函数‘memcpy’
dynamic_array.c:36: 警告:隐式声明与内建函数‘memcpy’不兼容
运行时出现段错误:
input name:
jkk
input subjet1:
input subjet2:
input subjet3:
345
段错误
lzh3ng 2009-10-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 pcboyxhy 的回复:]
scanf("%d", scorebuf);  //这个buf初始化了吗?
用linux的赞一个
[/Quote]

我感觉buf有问题,但就是不知道出了什么问题。
lzh3ng 2009-10-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 thy38 的回复:]
可以动态增加啊,LZ是什么问题?
[/Quote]

实现不了,段错误!我找不出问题在哪?
pcboyxhy 2009-10-07
  • 打赏
  • 举报
回复
scanf("%d", scorebuf); //这个buf初始化了吗?
用linux的赞一个
thy38 2009-10-07
  • 打赏
  • 举报
回复
可以动态增加啊,LZ是什么问题?
lzh3ng 2009-10-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 aizibion 的回复:]
不重载[]想增加?除非先分配足够的内存预留起
[/Quote]

是动态增加的,但我没实现了,不知道哪错了。
aizibion 2009-10-07
  • 打赏
  • 举报
回复
不重载[]想增加?除非先分配足够的内存预留起

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧