将字符复制到char指针数组时的堆栈转储
发布时间:2020-12-16 09:29:20 所属栏目:百科 来源:网络整理
导读:gcc 4.4.3 c89 我有以下源文件. 但是,我在以下行获得堆栈转储: printf("dest: %d [ %s ]n",i,dest[i]); 原因是如果无法从for循环中断.我已经在for循环中尝试了不同的条件,以便在打印之前尝试打破. i != NULLi != ' 'i != 0 但是,以上所有都无法奏效.我可
gcc 4.4.3 c89
我有以下源文件. 但是,我在以下行获得堆栈转储: printf("dest: %d [ %s ]n",i,dest[i]); 原因是如果无法从for循环中断.我已经在for循环中尝试了不同的条件,以便在打印之前尝试打破. i != NULL i != ' ' i != 0 但是,以上所有都无法奏效.我可以使用sizeof(src)作为条件.但我只想知道为什么我使用当前方法获取此堆栈转储. 当我使用gdb检查时,所有数组元素都已初始化为nul: (gdb) p dest $8 = {0x0 <repeats 11 times>} 源代码: void copy_characters() { /* static character array */ char src[] = "sourcefile"; /* array of pointers to char */ char *dest[sizeof(src)] = {0}; size_t i = 0; /* Display the source characters */ for(i = 0; src[i] != ' '; i++) { printf("src [ %c ]n",src[i]); } /* Copy the characters */ for(i = 0; i < sizeof(src); i++) { dest[i] = &src[i]; } /* Display them */ for(i = 0; dest[i] != ' '; i++) { printf("dest: %d [ %s ]n",dest[i]); } } 非常感谢任何建议, 解决方法
首先 – 我不确定你在这里做什么.但:
dest [i]是指向字符的指针. 您应该测试dest [i]指向的值是否为null,如下所示: for(i = 0; *(dest[i]) != ' '; i++) { (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |