[字符数组和字符串数组]代码和运行结果如下,有些没想明白

csdn89578289 2014-10-11 05:55:54
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define MAXLINE 1024

int findnumstring (char *outputsr, char *inputstr)
{
bool reset = false;
int i, j = 0, count = 0;
char *str[32] = {0};
char temp[MAXLINE] = {0};

for( i = 0;*(inputstr + i) != '\0';i++ )
{
if( *(inputstr + i) >= '0' && *(inputstr + i) <= '9' ) // '0'~'9'
{
reset = true;
temp[count] = *(inputstr + i);
count++;
}
else
{
if( reset )
{
str[j++] = temp;
printf("str[%d] = %s\n",j-1,str[j-1]);
count = 0;
//bzero(temp, sizeof(temp));
memset(temp, 0, sizeof(temp));
reset = false;
}
}
}
for(i = 0;str[i] != NULL;i++)
printf("str[%d] = %s\n",i, str[i]);
}

int main(int argc, char **argv)
{
char input[] = "abcd12345eee125sss123456789a";
char output[] = {0};

findnumstring(output, input);

exit(0);
}


输出结果:
str[0] = 12345
str[1] = 125
str[2] = 123456789
str[0] =
str[1] =
str[2] =


只是个写了一部分的代码,但是遇到这么个问题,求解
...全文
276 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
铖邑 2014-10-13
  • 打赏
  • 举报
回复
稍微改了一下,也不太合理,写代码思路要清晰 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #define MAXLINE 1024 int findnumstring (char *outputsr, char *inputstr) { bool reset = false; int i, j = 0, count = 0; char *str[32] = {0}; char *temp; for( i = 0;*(inputstr + i) != '\0';i++ ) { if( *(inputstr + i) >= '0' && *(inputstr + i) <= '9' ) // '0'~'9' { reset = true; count++; } else { if( reset ) { *(inputstr + i) = '\0'; str[j++] = temp; printf("str[%d] = %s\n",j-1,str[j-1]); count = 0; reset = false; } temp = (inputstr + i + 1); } } for(i = 0;str[i] != NULL;i++) printf("str[%d] = %s\n",i, str[i]); } int main(int argc, char **argv) { char input[] = "abcd12345eee125sss123456789a"; char output[] = {0}; findnumstring(output, input); exit(0); }
赵4老师 2014-10-13
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。 崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。
csdn89578289 2014-10-12
  • 打赏
  • 举报
回复
引用 1 楼 xpdavis 的回复:
str[j++] = temp; 改成 strcpy(str[j++], temp);
这样改的话,运行会出现段错误 - -
铖邑 2014-10-11
  • 打赏
  • 举报
回复
str[j++] = temp; 改成 strcpy(str[j++], temp);

70,023

社区成员

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

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