段错误(吐核)有关整理字符串数组中得整数!

凌兵 2011-09-02 12:27:28
#include<stdio.h>
#include<ctype.h>
#include<string.h>

int main()
{

int i;
int j,k,n,el,sum;
char *s;
int a[100]={0};

scanf("%s",&s);
j=sum=n=0;
el=1;
char *p=s;

while(*s !='\0')
{
p=s;
while(*p>='0' && *p <= '9')
{
j++;
p++;
}
if(j>0)
{
for(j=j-1;j>=0;j--)
{
sum+=((*(s+j)-48)*el);
el=el*10;
}
a[n]=sum;
printf("%d\n",a[n]);
n++;
j=0;
sum=0;
el=1;
}
s=p;
if(*s !='\0')
{
s++;
}
}

for(i=0;i<n;i++)
{
printf("%d",*(a+i));
}
printf("the number of integers is %d\n",n);
return 0;

}
...全文
266 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ningto.com 2011-09-02
  • 打赏
  • 举报
回复
malloc,最后要free掉
jackyjkchen 2011-09-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 longaction2012 的回复:]

引用 2 楼 jackyjkchen 的回复:
char *s;
没有分配内存

我要窃窃地问一下怎么分配?
[/Quote]

char *s = (char*)malloc(100);
scanf("%s",s)
不过你得保证scanf不得输入超过100个字符

另外你最好保存一个s的原始地址,你程序里有s++,地址变了,free会错的
iaccepted 2011-09-02
  • 打赏
  • 举报
回复
char * s只做了定义也就是分配了指针的空间,没有用new为其分配空间,所以程序在运行时出错。
可以改为char s[100],然后再scanf("%s",s)这样就可以了。欢迎交流学习
凌兵 2011-09-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jackyjkchen 的回复:]
char *s;
没有分配内存
[/Quote]
我要窃窃地问一下怎么分配?
jackyjkchen 2011-09-02
  • 打赏
  • 举报
回复
char *s;
没有分配内存
無_1024 2011-09-02
  • 打赏
  • 举报
回复
s没有分配空间
limang89 2011-09-02
  • 打赏
  • 举报
回复
syntax error : missing ';' before 'type'
缺分号。。。
tomljr 2011-09-02
  • 打赏
  • 举报
回复
1>e:\11\test\test\test.c(13) : warning C4996: 'scanf' was declared deprecated
1> c:\program files\microsoft visual studio 8\vc\include\stdio.h(295) : see declaration of 'scanf'
1> Message: 'This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
1>e:\11\test\test\test.c(16) : error C2143: syntax error : missing ';' before 'type'
1>e:\11\test\test\test.c(20) : error C2065: 'p' : undeclared identifier
1>e:\11\test\test\test.c(20) : warning C4047: '=' : 'int' differs in levels of indirection from 'char *'
1>e:\11\test\test\test.c(21) : error C2100: illegal indirection
1>e:\11\test\test\test.c(21) : error C2100: illegal indirection
1>e:\11\test\test\test.c(40) : warning C4047: '=' : 'char *' differs in levels of indirection from 'int'
1>生成日志保存在“file://e:\11\test\test\Debug\BuildLog.htm”
1>test - 4 个错误,3 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
凌兵 2011-09-02
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xiayefanxing 的回复:]
如果是在linux下得话,编译的是加-g, gcc -g -o test test.c,然后用gdb调试,gdb ./test,就会显示到你是哪行出现了段错误。改那里就可以。你可以试试。
[/Quote]
gdb很强大,我们在学。
夏夜繁星 2011-09-02
  • 打赏
  • 举报
回复
如果是在linux下得话,编译的是加-g, gcc -g -o test test.c,然后用gdb调试,gdb ./test,就会显示到你是哪行出现了段错误。改那里就可以。你可以试试。
凌兵 2011-09-02
  • 打赏
  • 举报
回复
我表示大家很给力,很强大。作为一个新手,能得到高人指点,真的狠幸运》》》》》

69,369

社区成员

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

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