免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2275 | 回复: 12
打印 上一主题 下一主题

关于struct和strcpy的一点疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-04-29 17:42 |只看该作者 |倒序浏览
在学习C的struct的时候,写了这么一段程序:

  1.       1 #include<stdio.h>
  2.       2 #include<string.h>
  3.       3
  4.       4
  5.       5 typedef struct b{
  6.       6     char*b;
  7.       7 }b;
  8.       8
  9.       9 typedef struct c{
  10.      10     b*structb;
  11.      11 }c;
  12.      12
  13.      13
  14.      14 int
  15.      15 main()
  16.      16 {
  17.      17 char buffer[265];
  18.      18 char*tmp;
  19.      19 char*chap;
  20.      20 printf("input chap:");
  21.      21 tmp=fgets(buffer,256,stdin);
  22.      22 strcpy(chap,tmp);
  23.      23 int*i=0;
  24.      24 struct c *tmpc;
  25.      25 printf("input b:");
  26.      26 tmp=fgets(buffer,256,stdin);
  27.      27 strcpy(tmpc->structb->b,tmp);
  28.      28 printf("%s",tmpc->structb->b);
  29.      29 printf("%s",chap);
  30.      30 }
复制代码

编译通过没问题,问题是在运行的时候出现"Bus error"的提示。
于是我改了第22行和27行的代码如下:
修改《一》
  1.      22 chap=tmp;
  2.      27 tmpc->structb->b=tmp;
复制代码

或者这样:
修改《二》
  1.      21 chap=fgets(buffer,256,stdin);
  2.      25 tmpc->structb->b=fgets(buffer,256,stdin);
复制代码

编译运行都没有问题,但是又出现这样的结果:

input chap:fdf
input b:sd
sd
sd

让我非常的不理解,为什么分别赋值过了,b和chap的结果还是一样的呢?
难道我这样做了只是把b和chap指向了tmp的地址?如果我想要分别给这两个指针指向的地址赋值。
还有一个问题就是,如果我在声名struct b或者struct c的时候,如果写成这样:

  1.       5 typedef struct b{
  2.       6     char*b;
  3.       7 };
  4.       8
  5.       9 typedef struct c{
  6.      10     b*structb;
  7.      11 };
复制代码

在编译的时候会报错,这两种写法紧紧是在最后少了一个struct自己的名字而已,就会有这么大的不同。这两种写法有什么区别呢?
谢谢!

论坛徽章:
0
2 [报告]
发表于 2006-04-29 17:47 |只看该作者
没有为chap分配空间

论坛徽章:
0
3 [报告]
发表于 2006-04-29 17:52 |只看该作者
那在付值tmpc->structb->b的时候也没分配内存空间啊。
如果没有分配内存空间的话为什么还会printf出结果呢?

论坛徽章:
0
4 [报告]
发表于 2006-04-29 18:05 |只看该作者

  1. char *p;
  2. strcpy(p, buf);
复制代码

看来,这种情况是很多学C的人的必经的成长之路,
真是growing pains。

论坛徽章:
0
5 [报告]
发表于 2006-04-29 18:15 |只看该作者
那这一段应该怎么写才对呢?

论坛徽章:
0
6 [报告]
发表于 2006-04-29 19:07 |只看该作者
阿弥陀佛!

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. typedef struct b {
  5.         char *b;
  6. } b;

  7. typedef struct c {
  8.         b *structb;
  9. } c;

  10. int main()
  11. {
  12.         int *i = 0;
  13.         char buffer[256];
  14.         char *tmp;
  15.         char *chap;
  16.         struct c *tmpc;

  17.         printf("input chap: ");
  18.         tmp = fgets(buffer, 256, stdin);
  19.         chap = (char *)malloc(strlen(buffer) + 1);
  20.         strcpy(chap, tmp);
  21.        
  22.         printf("input b: ");
  23.         tmp = fgets(buffer, 256, stdin);
  24.         tmpc = (struct c *)malloc(sizeof(struct c) + strlen(buffer) + 1);
  25.         tmpc->structb = (struct b *)tmpc + sizeof(struct c);        
  26.         tmpc->structb->b = (char *)tmpc + sizeof(struct c);
  27.         /* or:
  28.         tmpc = (struct c *)malloc(sizeof(struct c));
  29.         tmpc->structb = (struct b *)malloc(sizeof(struct b));
  30.         tmpc->structb->b = (char *)malloc(strlen(buffer) + 1);
  31.         */
  32.         strcpy(tmpc->structb->b, tmp);

  33.         printf("%s", tmpc->structb->b);
  34.         printf("%s", chap);

  35.         return 0;
  36. }

复制代码

[ 本帖最后由 westgarden 于 2006-4-29 19:10 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2006-04-29 19:28 |只看该作者

路过

malloc哦.......

论坛徽章:
0
8 [报告]
发表于 2006-04-29 20:15 |只看该作者
啊? 是介个意思啊? 还以为不要用malloc呢,不好意思啊。
那这个struct呢?多一个b 跟不多 b 有什么去别呢? 为什么编译的时候会出错呢 ?

[ 本帖最后由 stellit 于 2006-4-29 20:25 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2006-04-30 08:55 |只看该作者
楼主,请注意你的代码版式。不要在代码中间申请变量,这个习惯不好。
从一开始就要养成好习惯,多读书哦!

论坛徽章:
0
10 [报告]
发表于 2006-05-04 18:52 |只看该作者
  1. tmpc = (struct c *)malloc(sizeof(struct c));
  2.         tmpc->structb = (struct b *)malloc(sizeof(struct b));
  3.         tmpc->structb->b = (char *)malloc(strlen(buffer) + 1);
复制代码

Thank you ! I think it's very useful !
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP