| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1555 人关注过本帖
标题:谁能解决问题,重谢!
取消只看楼主 加入收藏
demon90s
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:44
专家分:121
注 册:2016-1-3
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
谁能解决问题,重谢!
在GCC下编译,运行错误。在VC下,没问题


代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(void)
{
int count = 5;


char *p, *q;
int i, j;
char **trans = (char**)malloc(count * 4);
char buf[] = "hahahahahha";


//q = (char*)malloc(strlen(p) + 1);
//strcpy(q, "hiiii");
//printf("%p: %s\n", q, q);


trans[0] = (char*)malloc(strlen(buf) + 1);
strcpy(trans[0], "hello");


for (i = 1; i < count; i++)
{
trans[i] = (char*)malloc(strlen("qwerty") + 1);
strcpy(trans[i], "qwerty");

printf("%p: %s\ttrans[0]: %s\n", trans[i], trans[i], trans[0]);
}


//printf("%p: %s\n", q, q);
//free(q);


getchar();


return 0;
}


GCC下运行的结果:
0x1fd7050: qwertytrans[0]: hello
0x1fd7070: qwertytrans[0]: hello
0x1fd7090: qwertytrans[0]: hello
0x1fd70b0: qwertytrans[0]: ?p?


第四次输出,trans[0]所指的内容发生了变化,但我不知道为什么
搜索更多相关主题的帖子: include qwerty hello count 
2016-01-03 15:23
demon90s
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:44
专家分:121
注 册:2016-1-3
收藏
得分:0 
回复 2楼 ehszt
我在LINUX虚拟机里面用GCC编译出错了,但是跑到windows下面用GCC编译又正确了。

不知道是不是我的LINUX虚拟机的问题。。
2016-01-04 11:00
demon90s
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:44
专家分:121
注 册:2016-1-3
收藏
得分:0 
回复 5楼 rjsp
问题不在这里
2016-01-04 11:00
快速回复:谁能解决问题,重谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011845 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved