C语言代码,段错误,看不出为什么错

题目表述:
对于给定的一个字符串,统计其中数字字符出现的次数。
错误描述:
编译器提示段错误
但是没找出为什么会发生段错误

# include<stdio.h>
# include<string.h>
int main()
{
    int i,n,len,num;
    char *ch;
    scanf("%d",&n);
    while(n--)
    {
        num=0;
        scanf("%s",ch);
        len=strlen(ch);
        for(i=0;i<len;i++)
        {
            if(ch[i]>='0'&&ch[i]<='9')
                num++;
        }
        printf("%d\n",num);
    }   
    return 0;
}

4个回答

char *ch;
定义了指针,但是没有分配空间

char *ch; = (char *)malloc(100*sizeof(char));

如果问题解决,麻烦点下回答右边的采纳,谢谢

caozhy
贵阳老马马善福专门编写代码的老马就是我! 回复Hold_C: 也谢谢你
接近 2 年之前 回复
Hold_C
Hold_C 多谢了,已采纳!
接近 2 年之前 回复
caozhy
贵阳老马马善福专门编写代码的老马就是我! char *ch; = 这里多了一个分号,不好意思
接近 2 年之前 回复

没有实例化ch指针,所以你是不能存放东西的,具体的实例化可以用1楼的方法

没有实例化ch指针,还有for循环也错了,for(i=0;i<len;i++){}

Hold_C
Hold_C 多谢提醒! 忘了用代码块包裹起来,所以有些部分就没有显示出来,已修改
接近 2 年之前 回复

定义指针时需要分配空间,没有实例化ch指针 主要是指针问题

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
C语言段错误详解
里面整理了,我收集的c语言段错误分析的文章,和core调试方法
请教:c语言段错误
在下在linux下编了一个简单的关于数据结构中"栈"的程序,编译一切正常,但是无法运行,而且提示是段错误,希望各位大侠客不吝赐教.rn以下我将程序的大致思路贴出来,希望大家帮帮我分析一下,谢谢了.rnrn#include rn#define MAXNUM 888 rn /*定义栈结构-顺序栈*/ rn typedef structrn rn int stack[MAXNUM];/*循序栈*/ rn int top;/*栈指针*/ rn STACK,*PSTACK;rn /*栈的初始化*/ rn void init_stack(PSTACK head)rn rn head->top=-1;rn// return 0;rn rn /*入栈*/ rn void push_stack(PSTACK head,int x)rn rn// if(head->top>=MAXNUM-1)/*栈满,无法入栈*/rn// return 0;rn head->stack[++head->top]=x;rn// return 1; rn rn /*出栈*/ rn void pop_stack(PSTACK head) rn rn// if(head->top<0)/*空栈/rn// return 0;rn return head->stack[head->top--];rn rnrnvoid main(void)rnrnPSTACK pp;rninit_stack(pp);rnpush_stack(pp,1);rnpush_stack(pp,2);rnpush_stack(pp,3);rnpop_stack(pp)rnpop_stack(pp)rnpop_stack(pp)rn
c语言 段错误 linux
当写c语言时,经常遇到段错误,很多人都是用printf,但是当程序比较大时,就比较难找了,或者当程序运行时并不会立即出现段错误,而是经过很长一段时间才会出现那么用printf显然不行。这时就可以使用gdb来调试了 首先查看区块数(内核数据转存core文件的上限,单位为区块)详细查看ulimit使用(控制系统资源) ulimit -c unlimited 设置为无限 ulimit -c  查
c语言问题(段错误)
字符串s1中出现类似"a-f"这样的在字符串中展开为“abcdef",rn我只是编写了对小写字母有用的程序rn输出的时候,a-y可以展开,可是只要出现z就提示”段错误“rnrn代码:rn//head.hrnrn#ifndef __HEAD_Hrn#define __HEAD_Hrnint search(char);rnvoid expand(char *,char*);rn#endifrnrnrn//main.crn#includern#include"head.h"rn#define MAX 200rnchar small[26]='a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',rn 'p','q','r','s','t','u','v','w','x','y','z';rnint main()rnrn char s1[MAX],s2[MAX],ch;rn int i=0;rn printf("please input a string:\n");rn while((ch=getchar())!=EOF)rn rn s1[i]=ch;rn i++;rn rn s1[i]='\0';rn expand(s1,s2);rn printf("\n%s\n",s2);rn return 0;rnrnrnrnrn//expand.crnextern char small[26];rnvoid expand(char *s1_ptr,char *s2_ptr)rnrn char *current=s1_ptr;rn int i;rn while((*current)!='\0')rn rn if((*current>='a')&&(*current<='z'))rn rn if((*(current+1)=='-')&&((*(current+2)>='a')&&(*(current+2)<='z')))rn rn i=search(*current);rn while(small[i]<=*(current+2))rn rn *s2_ptr=small[i];rn i++;rn s2_ptr++;rn rn current=current+3;rn rnrn rn elsern rn *s2_ptr=*current;rn s2_ptr++;rn current++;rn rn rn *s2_ptr='\0';rnrnrnrnrn//search.hrnrnextern char small[26];rnint search(char ch)rnrn int i=0;rn for(;i<26;i++)rn rn if(ch==small[i])rn return i;rn rnrnrnrn希望大家给我解释一下,现在搞不明白,不知道怎么修改
C语言,段错误的原因
[code=C/C++]rn//声明一个常指针rnchar* const name = "xxxkxkxkx";rn//更改字符串的值rnname[3] = 'h';rn[/code]rn以上代码通过GCC编译时,引发段错误,rn我困惑的是:char* const name = "xxxkxkxkx"; 声明的是常指针,rn为什么我修改指向单元的内容,会引发segment fault
C语言段错误总结
段错误是很常见的,一般是什么问题引起呢?文档里面有详细总结。
C语言嵌入AT&T汇编 代码段错误
int sum(int N,int arry[])rn int i = 0;rn int s = 0;rn for(i = 0;i < N;i++)rn s += arry[i];rn return s;rnrn#include rn#include rnint sum(int N,int arry[]);rnint main(int argc,char *argv[])rn int i;rn int temarr[argc];rn temarr[0] = 0;rn for(i = 1;i < argc; i++)rn temarr[i] = atoi(argv[i]);rn printf("Total is %d \n",sum(argc,temarr));rn return 0;rnrn这是C语言。现在要求是把main中调用sum()换成内嵌汇编的方式。我做的是直接把sum.o反汇编,直接截取其中的汇编代码,但是报错!Segmentation fault (core dumped)。初步判断是反汇编之后标号和地址没有改变。内嵌汇编如下:rn#include rn#include rnint main(int argc,char *argv[])rn int i;rn int total = 0;rn int temarr[argc];rn temarr[0] = 0;rn for(i = 1;i < argc; i++)rn temarr[i] = atoi(argv[i]);rn __asm__ __volatile__ (rn "pushq %%rbp;\n\t"rn "movq %%rsp,%%rbp;\n\t"rn "movl %%edi,-0x14(%%rbp);\n\t"rn "movq %%rsi,-0x20(%%rbp);\n\t"rn "movl $0x0,-0x8(%%rbp);\n\t"rn "movl $0x0,-0x4(%%rbp);\n\t"rn "movl $0x0,-0x8(%%rbp);\n\t"rn "jmp 7f;\n\t"rn "8:\tmovl -0x8(%%rbp),%%eax;\n\t"rn "cltq;\n\t"rn "leaq 0x0(,%%rax,4),%%rdx;\n\t"rn "movq -0x20(%%rbp),%%rax;\n\t"rn "addq %%rdx,%%rax;\n\t"rn "movl (%%rax),%%eax;\n\t"rn "addl %%eax,-0x4(%%rbp);\n\t"rn "addl $0x1,-0x8(%%rbp);\n\t"rn "7:\tmovl -0x8(%%rbp),%%eax;\n\t"rn "cmpl -0x14(%%rbp),%%eax;\n\t"rn "jl 8b;\n\t"rn "movl -0x4(%%rbp),%%eax;\n\t"rn "popq %%rbp;\n\t"rn "retq;\n\t"rn "nopw %%cs:0x0(%%rax,%%rax,1);\n\t"rn "nopl (%%rax);\n\t"rn : "=a" (total)rn : "b" (argc),"a" (temarr)rn : "memory"rn );rn printf("Total is %d \n",total);rn return 0;rnrn求大神告知解决办法!!
C语言段错误 总结 段错误整理
C语言段错误 总结 段错误整理 C语言段错误 总结 段错误整理
C语言段违例段错误
[code=c]#include rn#include rnrnvoid bubble(int *s,int length);rnvoid swap(int *p,int *q);rnrnint main()rn int j;rn int s[10] = 6,5,8,41,5,3,50,6,10;rn bubble(s,10);rn printf("the result of array is:/n");rn for(j = 0;j <10;j++)rn printf("%d",s[j]);rn printf("/n"); rnrn system("pause");rn rn rn rnvoid bubble(int *s,int length)rn int i,j;rn for(i = 0;i < length;i++)rn for(j = length-1;j > i;j--)rn if (*(s+i) > *(s+j))rn swap((s+i),(s+j)); rn rn rnrnrnrnvoid swap(int *p,int *q)rn int temp;rn temp = *p;rn *p = *q;rn *q = temp;rn[/code]rn出现段违例了,怎么回事的?求各位大神帮帮忙
C语言的内存分配和段错误的处理
在嵌入式的程序设计中对于程序变量的内存的管理尤为重要,因为嵌入式产品的内存资源十分有限,合理分配内存是程序设计人员最优先考虑的问题。当然,一些变量的内存分配不合理有时会导致程序的崩溃(即是段错误)等错误。本文针对这些问题进行了详细的解析并给予合理的解决方案。 首先说下程序和进程:         进程:处于运行状态的程序,需要占据系统资源(内存,cpu,时间片),会参与操作系统的调度,具有生命
这句代码为什么引起段错误?
程序出现段错误:rnrnProgram received signal SIGSEGV, Segmentation fault.rnmain () at data_struct_report.c:389rn389 sq_list *sq = (sq_list *)malloc(sizeof(sq_list));rnrnrn这句代码为什么为出现段错误?
有一个错,为什么。(c语言)
编译时有错。rn可这样又没有错rn#includernmain()rn int c;rngetchar();rn getchar();rnrn c= getchar();rn printf("%d", c);rnrn这是什么原理呀。rn
帮忙找错,看不出来啊
输出名字都有错,为什么啊rnrn[code=C/C++]rn#include rnusing namespace std;rnrntypedef enum coltypernrn TYPE_TIME = 0,rn TYPE_INT,rn TYPE_DOUBLE,rn TYPE_STRINGrnColtype;rnrntypedef enum colunitrnrn UNIT_1,rn UNIT_2,rn UNIT_3rnColunit;rnrnstruct col_infornrn char *name;rn Coltype type;rn Colunit unit;rn double precision;rn long long scope;rn;rnrnint main()rnrn struct col_info *col1 = new struct col_info;rn col1->name = "col1fff";rn col1->precision = 0.0;rn col1->scope = 10;rn col1->type = TYPE_TIME;rnrn struct col_info *col2 = new struct col_info;rn col1->name = "col2";rn col1->precision = 0.0;rn col1->scope = 10;rn col1->type = TYPE_INT;rnrn struct col_info *col3 = new struct col_info;rn col1->name = "col3";rn col1->precision = 0.0;rn col1->scope = 10;rn col1->type = TYPE_STRING;rnrn cout<name<name<name<
c语言找错。
C语言程序运行错误。rn下面C语言程序的 p=n->head->next; 用gdb调试的时候报错,Program received signal SIGSEGV, Segmentation fault.rn0x0804919b in simplify_polynomial (n=0x804c188) at test7.c:475rn475 p=n->head->next; 找了很久没发现错误在哪里。 请问错误在哪里? 完整程序在:http://hi.baidu.com/lijr03/blog/item/dd824f7e2cb804f42e73b3a1.html。谢谢rnrnrnpolynomial simplify_polynomial(polynomial n)rnrn polynomial p;rn polynomial_node *p1, *s;rn rn rn p=n;rn p=n->head->next;rn while(p!=NULL)rn rn p->m=simplify_monomial(p->m);rn p=p->next;rn rnrn p1=p->head->next;rn p=p->head->next;rn s=p;rn rn while(p!=NULL) /* merge the terms with the same monomials */rn rn while(p1!=NULL)rn rn if(compare_monomials(p->m, p1->m)==1)rn rn p->m->coeff=p1->m->coeff + p->m->coeff;rn p=delete_monomial(p, p1->m);rn p1=s;rn rn p=s->next;rn rn p1=p1->next;rn rn p=p->next;rn s=p; /* s records the position of p */rn p1=p;rn rnrn rn p1=p->head->next;rn while(p1 != NULL)rn rn if(p1->m->coeff==0)rn rn p=delete_monomial(p, p1->m);rn rn p1=p1->next;rn rn rn return p;rn rn
代码段错误?
下错哪错了,我一直查不出来rnrn rnrnrn错误提示:rn at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:457)rn at org.apache.jsp.login$jsp._jspService(login$jsp.java:266)rn at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)rn at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)rn at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)rn at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)rn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)rn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)rn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)rn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)rn at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2344)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)rn at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)rn at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:462)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)rn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)rn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:163)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)rn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)rn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)rn at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1011)rn at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1106)rn at java.lang.Thread.run(Unknown Source)rnrnrnroot cause rnrnjava.sql.SQLException: No data foundrn at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)rn at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source)rn at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(Unknown Source)rn at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)rn at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)rn at org.apache.jsp.login$jsp._jspService(login$jsp.java:151)rn at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)rn at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)rn at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)rn at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)rn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)rn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)rn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)rn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)rn at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2344)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)rn at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)rn at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:462)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)rn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)rn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:163)rn at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)rn at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)rn at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)rn at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1011)rn at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1106)rn at java.lang.Thread.run(Unknown Source)rnrn
代码为什么错?
rn TTreeNode *rNode,*mNode;rn AnsiString S="er";rn rNode=TreeView1->Selected;rn mNode=TreeView1->Items->AddChild(rNode,S);rn TreeView1->ReadOnly=false;rn Table1->Active=true;rn Table1->Edit();rn Table1->AppendRecord(OPENARRAY(TVarRec,("11",S,4)));rn Table1->Post();rn这段代码有何错误,为什么表不能编辑?
以下代码为什么错
<%rndim ors ,oconn,szXMLrnset oconn=server.CreateObject("adodb.connection")rnset ors=server.CreateObject("adodb.recordset")rnoconn.open "DRIVER=SQL server;SERVER=(local);uid=sa;DATABASE=pubs"rnsql="select * from authors"rnors.Open sql,oconn,1,3rnset strRecordset=server.CreateObject("adodb.stream")rnors.Save strRecordset,adPersistXMLrnszXML=strRecordset.ReadText '这一行出错!rnResponse.Write (szXML)rnrn%>rn错误报告:错误类型:rnADODB.Stream (0x800A0C93)rn/test/test1.asp, 第 10 行rnrn
这行代码为什么错?
以下是我代码的一部分rnrn'建立数据库连接rnrnset R=Server.CreateObject("adodb.recordset")rnsql="select * from regist where name='" & UserName & "'"rnR.open sql,MyConnection,3,3rnrn'如果存在相同的用户名字则跳到提示页面rnrnif not R.eof thenrn R.closern response.redirect "wrong_haveexist.asp"rnEnd if rnrnR.closernrnsql="insert into regist(name,password) value('" & UserName & "','" & UserPassword & "')"rnR.open sql,MyConnection,3,3 ****************就是这里报错rnR.closernrnrnrn为什么出错,请告诉我后面那两个参数分别有什么取值,不同的值代表什么意思?
怎样解决这个C语言段错误?
[code=C/C++]rn#include rnrn#define MAX 100rnrntypedef struct rn int data[MAX];rn int len; /* the length of the seqlist */rnSeqList;rnrnmain()rnrn SeqList *p;rn int i = 0;rn char ch = 0;rnrn printf("Please input an array:\n");rn while (i < MAX && ch != EOF)rn scanf("%d%c", &p->data[i++], &ch); rn p->len = i;rn for (i = 0; i < p->len; i++)rn printf("%5d", p->data[i]);rnrnrn[/code]rn编译后出现段错误,只知道可能while语句中指针使用错误,但始终没想明白究竟是怎么回事,只有求助于大家
C语言gdb分析段错误的core文件
C语言gdb分析core文件
C语言 递归中的段错误与栈溢出
C语言中,每次递归调用都会多一个栈帧——和普通的函数调用并没有什么不同。由于使用了调用栈,C语言自然支持了递归。在C语言的函数中,调用自己和调用其他函数并没有任何本质区别,都是建立新栈帧,传递参数并修改当前代码行。在函数体执行完毕后删除栈帧,处理返回值并修改当前代码行。 递归调用时新建了一个栈帧,并且跳转到了函数开头处执行,同一时刻可以有多个栈帧,但“当前代码行”只有一个。 “段”(segme
关于c语言解析json,nalloc段错误问题
![图片说明](https://img-ask.csdn.net/upload/201512/10/1449713765_614660.png)n![图片说明](https://img-ask.csdn.net/upload/201512/10/1449713772_782149.png)nn如图,我现在有四条数据,主函数传进来,前三条没问题, 第四条的时候段错误ngdb显示段错误位置在malloc,求助,为什么会这样
C语言strcpy()函数导致段错误
程序功能:循环单词rn如果一个单词通过循环右移获得的单词,我们称这些单词都为一种循环单词。 例如:picture 和 turepic 就是属于同一种循环单词。 现在给出n个单词,需要统计这个n个单词中有多少种循环单词。 rn输入:输入包括n+1行:rn第一行为单词个数n(1 ≤ n ≤ 50)rn接下来的n行,每行一个单词word[i],长度length(1 ≤ length ≤ 50)。由小写字母构成rn输出描述:rn输出循环单词的种数rnrn#include rn#include rn//求单词循环右移中最小的排列rnvoid string_sort(char *s,int n)rnrn char buf_1[50],buf_2[50];rn char st;rn strcpy(buf_1,s);rn strcpy(buf_2,s);rn for(int i=0;i=1;j--)rn buf_2[j]=buf_2[j-1];rn buf_2[0]=st;rn if(strcmp(buf_1,buf_2)>0)rn strcpy(buf_1,buf_2);rn rn strcpy(s,buf_1);rnrnrnint main()rnrn int n,length,num=1;rn char word[50][50];rn char str1[50];rn rn //输入rn scanf("%d",&n);rn for(int i=0;i0)rn rn strcpy(str1,word[i]);rn strcpy(word[i],word[j]);rn strcpy(word[j],str1);rn rn rn rn rn //求不同的循环单词数rn for(int i=0;i
C代码错在哪?
[b]大侠们,这不是改错题,但是为什么这段连编译都过不去?错在哪了?顺便说一下这段代码主要靠的是什么知识点?[/b]rnrn[code=C/C++]rn 1 #include rn 2 #include rn 3 rn 4 int f(int &ival)rn 5 rn 6 static int icount = ival;rn 7 ival++;rn 8 rn 9 return (icount * ival);rn 10 rn 11 rn 12 int main(void)rn 13 rn 14 int total = 0;rn 15 int ival = 1;rn 16 int i;rn 17 rn 18 for(i=0;i<2;i++)rn 19 total += f(ival);rn 20 rn 21 rn 22 printf("total = %d \n", total);rn 23 printf("ival = %d \n", ival);rn 24 rn 25 return 0;rn 26 rn[/code]
段错误,我要疯了,C语言数据结构
![图片说明](https://img-ask.csdn.net/upload/201810/03/1538580226_700700.png) rn所以问题出在哪里,我很菜,轻喷rnrn附上完整代码rnrn#includern#includern#includern#define OK 1rn#define ERROR 0rn#define TURE 1rn#define FALSE 0rn#define ElemType intrnrntypedef struct Nodernrn ElemType data;rn struct Node *next;rn Node,*LinkList;rnrnvoid CreateTail(LinkList L);rnint DelList(LinkList L,int i,ElemType e);rnvoid Get(LinkList L,int i);rnvoid InitList(LinkList *l);rnint InsList(LinkList L,int i,ElemType e);rnint ListLength(LinkList L);rnvoid Locate(LinkList L,ElemType e);rnrnvoid main()rnrn LinkList k;rn int m; //要查找的序号rn int n; //要插入的位置rn int t; //要删除的位置rn char a; //要查找的元素rn InitList(&k);rn Node *p;rn p=k->next;rn printf("用尾插法建立单链表,请输入链表数据,以$结束!\n");rn rn CreateTail(k);rn rn while(p!=NULL)rn rn printf("%c\n",p->data );rn p=p->next;rn rn rn printf("请输入要查找的序号:");rn scanf("%d",&m);rn printf("查找到的元素是:");rn Get(k,m);rn rn printf("\n请输入要查找的元素:");rn scanf("%c",&a);rn Locate(k,a);rn rn printf("\n请输入要插入的位置:");rn scanf("%d",&n);rn printf("插入的元素是:");rn scanf("%c",&a);rn InsList(k,n,a);rn printf("插入后的链表是:");rn p=k->next;rn rn while(p!=NULL)rn rn printf("%c",p->data );rn p=p->next;rn rn rn printf("\n请输入要删除的位置:");rn scanf("%d",&t);rn rn rn printf("删除的元素是:%c\n",a);rn scanf("%c",&a);rn DelList(k,t,a);rn printf("删除后的链表是:");rn p=k->next;rn rn while(p!=NULL)rn rn printf("%c",p->data );rn p=p->next;rn rn printf("\n链表的长度是:%d\n",ListLength(k));rnrnrnvoid InitList(LinkList *k)rnrn *k=(LinkList)malloc(sizeof(Node));rn (*k)->next=NULL;rnrnrnvoid CreateTail(LinkList L)rnrn char c;rn Node *r,*s;rn int flag=1;rn r=L;rn rn while(flag)rn rn c=getchar();rn if(c!='$')rn rn s=(Node*)malloc(sizeof(Node));rn s->data =c;rn r->next=s;rn r=s; rn rn elsern rn flag=0;rn r->next=NULL;rn rn rnrnrnvoid Get(LinkList L,int i)rnrn int j;rn Node *p;rn p=L;j=0;rn rn while(p->next!=NULL&&jnext;rn j++;rn rn rn if(i==j)rn printf("%c",p->data );rn elsern printf("FALSE");rnrnrnvoid Locate(LinkList L,ElemType e)rnrn Node *p;rn p=L->next;rn rn while(p!=NULL)rn if(p->data!=e )rn p=p->next;rn elsern break;rn if(p->data ==e)rn printf("TURE");rn elsern printf("FALSE"); rnrnrnint InsList(LinkList L,int i,ElemType e)rnrn Node *pre,*s;rn int k;rn pre=L;k=0;rn rn while(pre!=NULL&&knext;rn k=k+1;rn rn rn if(k!=i-1)rn rn printf("插入位置不合法!");rn return ERROR;rn rn rn s=(Node*)malloc(sizeof(Node));rn s->data =e;rn s->next=pre->next;rn pre->next=s;rn return OK;rnrnrnint DelList(LinkList L,int i,ElemType e)rnrn Node *p,*r;rn int k;rn p=L;k=0;rn rn while(p->next!=NULL&&knext;rn k=k+1;rn rn rn if(k!=i-1)rn rn printf("删除结点的位置不合法!");rn return ERROR;rn rn rn r=p->next;rn p->next=p->next->next;rn e=r->data ;rn free(r);rn return OK;rnrnrnint ListLength(LinkList L)rnrn Node *p;rn p=L->next;rn int j=0;rn rn while(p!=NULL)rn rn p=p->next;rn j++;rn rn rn return j;rn
C语言sprintf使用出现段错误
#include #include void Block(int function_no,int instrument_no) { char *function; char *instrument; char *result; sprintf(function,"%d",function_no); sprintf(instrument,"%d",instrument_no);
C语言 使用链表的时候造成 段错误
1.非关联化一个空指针总是导致段错误!   #include &amp;lt;stdio.h&amp;gt;   int main (void) { int *ptr = NULL; *ptr = 0; return 0; } 输出结果: 段错误(核心已转储   非关联化一个空指针,然后分配(写一个值到一...
C语言学习(十二)——段错误(一)
学习C语言有一段时间了,作为一个新手,写的东西总是会出现很多错误,也该了不少错误。其中,特别难改的就是段错误;所以在这里总结一些遇到的段错误: 首先什么是段错误?一旦一个程序发生了越界访问,cpu 就会产生相应的保护,于是 segmentation fault 就出现了,通过上面的解释,段错误应该就是访问了不可访问的内存,这个内存区要么是不存在的,要么是受到系统保护的,还有可能是缺少文件或者文件
这段代码有什么错,我实在看不出
rnrnrnrn rnrnrn rn BBS Using M$ SQL Server 2000rnrnrn rn rn rn rn rn rn 为什么诺顿杀毒那么慢啊!rn rn rn rn rn rn rn rnrn rn 为什么诺顿杀毒那么慢啊! 我的是WIN-XP,为了内存和速度只好用这个啊!rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rn Re:为什么诺顿杀毒那么慢啊!rn rn rn rn rn rn rn rnrn rn 是的,诺顿杀毒是慢,不过驻留占用较少,查毒好,还是优选的一种杀毒软件rn rn rn rn rn rn rn rn rn rn rn rn rn Re:为什么诺顿杀毒那么慢啊!rn rn rn rn rn rn rn rnrn rn 普通用户还是不要用xp了,太浪费资源rn rn rn rn rn rn rn rn rnrnrnrnrnrnrn
用c连接mysql数据库出现段错误为什么
void main()rnrn MYSQL mysql;rn MYSQL_RES *mysql_result;rn MYSQL_ROW mysql_row; /* Row data */rn int f1, f2, num_row, num_col;rn mysql_init(&mysql); rn if(!mysql_real_connect(&mysql,NULL,"root","nrsagroup","nrsa",0,NULL,0)) rn rn printf("Failed to connect to database : Error: %s\n", rn mysql_error(&mysql)); rn return;rn rn if(mysql_query(&mysql, "create table huhe(eventid int not null,eventname text)"))rn rn (void) printf("Create table fails: Error: %s\n",mysql_error(&mysql));rn rn if(mysql_query(&mysql, "insert into huhe(eventid,eventname) values (1,'go to lunch')"))rn rn (void) printf("Insert fails: Error: %s\n",mysql_error(&mysql));rn rn rn if(mysql_query(&mysql, "insert into huhe(eventid,eventname) values (2,'go to supper')"))rn rn (void) printf("Insert fails: Error: %s\n",mysql_error(&mysql));rn rn rn if (mysql_query(&mysql, "select * from huhe") == 0)rn rn mysql_result = mysql_store_result(&mysql);;rn num_row = mysql_num_rows(mysql_result); /* Get the no. of row */rn num_col = mysql_num_fields(mysql_result); /* Get the no. of column */rn for (f1 = 0; f1 < num_row; f1++) rn rn for (f2 = 0; f2 < num_col; f2++)rn rn mysql_row = mysql_fetch_row(mysql_result); /* Fetch one by one */rn printf("[Row %d, Col %d] ==> [%s]\n", f1, f2, mysql_row[f2]);rn rn rn rn else rn rn (void) printf("Query fails: Error: %s\n",mysql_error(&mysql));rn rn mysql_free_result(mysql_result);rn mysql_close(&mysql);rnrnrnrn运行结果显示rn[Row 0, Col 0] ==> [1]rn[Row 0, Col 1] ==> [go to lunch]rn[Row 2, Col 0] ==> [2]rn段错误
c语言代码 c语言代码
c语言代码c语言代码c语言代码c语言代码c语言代码c语言代码c语言代码c语言代码
Linux下的C语言程序发生段错误,为什么呢?
#include rn#include rn#include rn#define NMEMB 50rn#define SIZE 10rnint compar (const void *a, void *b)rnrnreturn (strcmp((char *)a,(char *)b));rnrnint main()rnrnchar data[NMEMB][SIZE]="Linux","freebsd","solzris","sunos","windows";rnchar key[80],*base,*offset;rnint i,j, nmemb=NMEMB,size=SIZE;rnfor(i=1;i<5;i++)rnrnfgets(key,sizeof(key),stdin);rnkey[strlen(key)-1]='\0';rnbase=data[0];rnoffset=(char*)lfind(key,base,&nmemb,size,compar);rnif(offset == NULL)rnrn[color=#FF0000]printf("%s not found!\n",key);rn//j++;rnoffset=(char*)lsearch(key,base,&nmemb,size,compar);rn//printf("Add %s to data array\n",data+j);rnprintf("Add %s to data array\n",offset);[/color]rnrnelsernrnprintf("found: %s\n",offset);rnrnrnrnrnrnrn[wyq@localhost lfindbsearch]$ gcc lfind.c -o lfindrn[wyq@localhost lfindbsearch]$ ./lfindrnLinuxrnfound: Linuxrnnihaornnihao not found!rnAdd [img=http://img.my.csdn.net/uploads/201303/22/1363962619_1015.jpg][/img] to data array[color=#FF0000](怎么会有乱码?)[/color]rnhellornhello not found!rnAdd hello to data arrayrn[color=#FF0000]Segmentation fault[/color]rnrn[color=#FF0000]为什么会发生段错误?rn经过调试:[/color]rnrn(gdb) rrnStarting program: /home/wyq/Linux-C1/di3zhang/lfindbsearch/lfind rnnihaornnihao not found!rnrn[color=#FF0000]Program received signal SIGSEGV, Segmentation fault.rn0x00920e0b in strlen () from /lib/libc.so.6[/color]rn(gdb) qrn[color=#FF0000]好像意思是strlen()的原因,可我觉得不是。。。[/color] rnrn求解:谢谢.
请教,为什么下面代码发生段错误?
#includern#includernint main()rnrn char *p[2];rnrn sprintf(p[1],"%s", "aaaaaaaaaa");rnrn printf(p[1]);rnrn return 0;rnrnrn——————分割线————————rn如果把sprintf(p[1],"%s", "aaaaaaaaaa");rnrn换成rnsprintf(p[0],"%s", "aaaaaaaaaa");rnrn把rnprintf(p[1]);rnrn换成rnprintf(p[0]);rnrn程序就可以正确运行。为什么会这样?
删除数据为什么错?代码错在何处?
mm:=DBEdit1.text;rnADOQuery4.Close;rn ADOQuery4.SQL.Clear;rn ADOQuery4.sql.Add('delete * from unit where name='+quotedstr(mm));rn ADOQuery4.ExecSQL;rn DBEdit1.text:='';rn self.Close;rn self.Free;rn form7.show;
c语言c代码
#include <reg52.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int uchar code DSY_CODE[]= { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0x82,0xf8,0x80,0x90,0xff }; void DelayMS(uint x) { uchar t; while(x--) for(t=120;t>0;t--); } void main() { uchar i=0; P0=0x00; while(1) { P0=~DSY_CODE[i]; i=(i+1);/*显示0-9*/ DelayMS(200); } }
c语言图书管理系统改个错..
#includern#includern#includern#includerntypedef structrnrn char name[10];//姓名rn char call[120];//电话rn char name1[20];//书名rn char numb[30];//书号rnrndatatype;rnrntypedef struct nodernrnrnrndatatype data;rnrnstruct node *next;rnlistnode;rnrntypedef listnode *linklist;rnrnlinklist head;rnrnlistnode *p;rnrn//图书用户信息的建立rnrnrnlinklist Create(int a)rnrnlinklist head=(listnode *)malloc(sizeof(listnode));rn listnode *p,*q;rn q=head;rn while(a--)rn rnp=(listnode *)malloc(sizeof(listnode));rnprintf("姓名 电话 书名 书号 \n");rnscanf("%s%s%s%s\n",p->data.name,p->data.call,p->data.name1,p->data.numb);rnq->next=p;rnq=p;rnrnrnq->next=NULL;rnrnreturn head;rnrnrnrn//图书管理信息的添加rnrnrnvoid Add(linklist head,int b)rnrnrnlistnode *m,*n,*p;rnrnm=head;rnn=m->next;rnrnwhile(n->next!=NULL)rnrnm=n;rnn=n->next;rnrnwhile (b--)rnrnrnrn p=(listnode *)malloc(sizeof(listnode));rn printf("姓名 电话 书名 书号\n");rnprintf("***********************\n");rnrnrn scanf("%s%s%s%s",p->data.name,p->data.call,p->data.name1,p->data.numb);rnrn n->next=p;rn n=p;rnrnrn n->next=NULL;rnrnrn//按姓名查找用户信息rnrnrn listnode *Find(linklist head)rnrnrnrn listnode *p;rn char name[10];rn int i;rn rn printf("*******************\n");rn printf("*******************\n");rn printf("姓名查找,按1表示查找:");rnp=head->next;rnrn scanf("%d",&i);rnrn if(i==1)rnrnrn printf("请输入要查询的姓名:");rnrn scanf("%s",&name);rnrn while(p&&strcmp(p->data.name,name)>0)rnrn p=p->next;rnrn if(p==NULL||strcmp(p->data.name,name)<0)rnrn p=NULL;rnrn rnrnrn return p;rnrnrnrnrn//按书名查找信息rnrnlistnode *Find1(linklist head)rnrnrnrn listnode *p;rnrn char name1[20];rnrnint i;rnrn printf("********************\n");rn printf("********************\n");rn printf("书名查找,按1表示查找:");rnrn p=head->next;rn rn scanf("%d",&i);rnrnrnif(i==1)rnrnrnrnrn printf("请输入要查询的书名:");rnrn scanf("%s",&name1);rnrn while(p&&strcmp(p->data.name1,name1)>0)rnrn p=p->next;rnrn if(p==NULL||strcmp(p->data.name1,name1)<0)rnrn p=NULL;rnrn rnrnreturn p;rnrnrn//按姓名信息的修改 rnrnvoid Alter(linklist head)rnrnrn listnode *p;rnrn p=Find(head);rnrn if(p==NULL)rnrnrnrn printf("用户信息不存在。\n");rnrnrnrn elsernrn rnrn printf("姓名 电话 书名 书号 \n");rn scanf("%s%s%s%s\n",p->data.name,p->data.call,p->data.name1,p->data.numb);rnrn printf("用户信息已经修改 !\n");rnrn rnrnrnrnrn//用户信息的删除rnrnrnvoid Delete(linklist head)rnrnrnrnint i;rnrnlistnode *p,*q;rnrnp=Find(head);rnrnif(p==NULL)rnrnrnrnrn printf("信息不存在!\n");rnrn return;rnrnrnelse rnrnrn printf("要删除的话就按1:\n");rnrn scanf("%d",&i);rnrn if(i==1)rnrnrnrnrn q=head;rnrn while(q!=NULL&&q->next!=p)rnrnrn q=q->next;rnrn q->next=p->next;rnrnrn free(p);rnrn printf("信息已经删除!\n");rnrn rnrnrnrnrnrnrn //信息的输出rnrnrnrnvoid List(linklist head)rnrnrn listnode *p;rnrn p=head;rn rn printf("姓名 电话 书名 书号 \n");rnrn printf("***********************\n");rnrn while(p->next!=NULL)rnrnrn rn printf("%s %s %s %s\n",p->next->data.name,p->next->data.call,p->next->data.name1,p->next->data.numb);rnrn p=p->next;rnrn rnrnrnrnrn int Menu()rnrnrnrnrn int i;rnrnrn printf(" \n");rnrn printf(" \n");rnrnrn printf(" 图书管理系统 \n");rnrn printf("****************************************************************************\n");rnrn printf("****************************************************************************\n");rnrn printf("** 1.用户信息的创建 **\n");rnrn printf("** 2.用户信息的添加 **\n");rnrn printf("** 3.用户姓名的查询 **\n");rnrn printf("** 4.图书书名的查询 **\n");rnrn printf("** 5.图书信息的修改 **\n");rnrn printf("** 6.用户信息的删除 **\n");rnrn printf("** 7.用户信息的输出 **\n");rnrn printf("** 0.退出管理系统 **\n");rnrn rn printf("****************************************************************************\n");rnrn printf("****************************************************************************\n");rnrn printf(" \n");rnrn printf(" 请选择0~7 \n");rnrnfor(;;)rnrn rnrn scanf("%d",&i);rnrn printf("\n");rnrn if(i<0||i>9)rnrn printf(" 输出错误,重选0~7: \n");rnrn else rnrn break;rnrnrn rnrnreturn i;rn rnrn //图书管理系统主函数部分rnvoid main()rnrn for(;;)rnrn rnrn switch(Menu())rn rn rnrnrnrn case 1:rnrn printf("*******************************用户信息的建立******************************\n");rnrn int i;rnrn printf("输入你想输入的数目:");rnrn scanf("%d",&i);rnrn head=Create(i);rnrn break;rnrn case 2:rnrn printf("*******************************用户信息的添加******************************\n");rnrn int a;rnrn printf("输入你想添加的人数:");rnrn scanf("%d",&a);rnrn Add(head,a);rnrn break;rnrnrn case 3:rnrn printf("*******************************用户姓名的查询******************************\n"); rn rn p=Find(head);rnrn if(p!=NULL)rnrnrn rnrnrn printf("姓名 电话 书名 书号\n");rnrn printf("**********************\n");rnrn printf("%s %s %s %s\n",p->data.name,p->data.call,p->data.name1,p->data.numb);rnrn printf("\n");rnrn printf("*********************\n");rn rn rnrn elsern rn printf("该用户的信息不存在!\n");rnrn break;rnrnrn case 4:rnrn printf("*******************************图书书名的查询******************************\n");rnrn p=Find1(head);rnrn if(p!=NULL)rnrn rnrnrn printf("姓名 电话 书名 书号\n");rnrn printf("**********************\n");rnrn printf("%s %s %s %s\n",p->data.name,p->data.call,p->data.name1,p->data.numb);rnrn printf("\n");rnrn printf("*********************\n");rn rn rnrn elsern rn printf("该用户的信息不存在!\n");rnrn break;rnrnrn case 5:rn rn printf("*******************************图书信息的修改******************************\n");rn rn Alter(head);rnrn break;rnrnrn case 6:rnrnrn printf("*******************************用户信息的删除******************************\n");rn rn List(head);rn rn break;rnrn case 7:rnrnrn printf("*******************************用户信息的输出******************************\n");rnrn List(head);rnrn break;rnrn case 0:rnrn printf("*******************************退出图书管理******************************\n");rn rn exit(0);rnrn rnrnrnrnrnrnrn rnrnrnrnrnrn rnrnrn return 0;rnrn
c语言帮忙看下错
printf("%3s %-20s %-3d %7d %7d %4d %7d %9d %5d %3.1f",rn Stu[i].SNo,Stu[i].SName,Stu[i].SAge,Stu[i].SResult[0],Stu[i].SResult[1],Stu[i].SResult[2],rn Stu[i].SResult[3],Stu[i].SResult[4],sum,sum/5);rn这段话出现runtime error R6002
C“段错误” : 快速定位“段错误”位置
1、 编译时gcc 1.c -g  ,后边加上-g在运行时产生段错误时会自动生成core.xxxx的文件    (若没有产生这个文件,则在命令行输入ulimit -c umlimited,重新编译运行) 2、用gdb调试    gdb a.out core.xxxx 3、输入where查看详细信息,既可以定位段错误的位置
C语言,错在哪里了?怎么改?
#include int main() long i = 50; long m; m = fac(i); printf("%ld",m); getch(); return 0; long fac(long n) int a,b,c; if(n == 0) return 1; else a = n-1; b = fac(n); c = b*a; return (c);
c语言这个错在哪?
我刚刚开始学的,很多地方都不懂,希望大家多多指教,这段求三角形面积的错在哪呢?rn或者怎么更好的去实现呢?rnrnrn#includern#includernvoid main()rnrn float a,b,c,s;rn float area;rn printf("input three numbers\n");rn scanf("%d%d%d",&a,&b,&c);rn if((a+b>c)&&(a+c>b)&&(b+c>a))rn rn s=(a+b+c)/2;rn area=sqrt(s*(s-a)*(s-b)*(s-c));rn printf("%d\n",area);rn rn elsern printf("the numbers is wrong\n");rn
相关热词 c# stream 复制 android c# c#监测窗口句柄 c# md5 引用 c# 判断tabtip 自己写个浏览器程序c# c# 字符串变成整数数组 c#语言编程写出一个方法 c# 转盘抽奖 c#选中treeview