且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我的代码将输出作为分段错误?

更新时间:2022-11-07 23:34:08

引用:

我的代码正在编译但显示分段错误错误为什么?



编译只是意味着代码尊重语言语法,但ut并不意味着它值得普利策。



使用调试器,它会帮助你找到崩溃的原因,也可能是原因。



当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看你的代码是什么这样做。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - ***,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。



正确的缩进有助于阅读你的代码:

#include < stdio.h >
int main()
{
int num [ 5 ] ,没有;
for (i = 0 ; i< 5; i ++)
{
scanf( %d,& num [i]);
}
printf( 输入数字\ n);
scanf( %d,& no);
s =搜索(不,& num [ 0 ]);
printf( %d \ n,s);
search1(no,num);
}

int i = 0 ;
int search( int no, int * num [i])
{
int x,i;
for (i = 0 ; i< 5; i ++)
{
if (no == * num [i])
return 1 跨度>;
}
return 0 ;
}

int search1( int no, int * num)
{
int i,x,k;
printf( 你的号码%d出现在列表,no);
printf( number \tfrequency);
for (i = 0 ; i< 5; i ++)
{
for (x = 0 ; x< i + 1; x ++)
{
k = 0 ;
if (num [i] == num [x]&& i!= x)
{
k = 0
;
break ;
}
其他
{
k = 1 ;
继续;
}
}
如果(k == 1
printf( %d,num [i]);
}
}


经典代码行:-)

 int I = 0; 
int search(int no,int * num [i]){///哦真的



我真正留下深刻印象的是你把我带到了使其定义的功能



提示你可能会关注如何将数组传递给函数search1 ...提示

 int search1(int no,int * num){


my code is compiling but showing segmentation fault error why?

What I have tried:

#include<stdio.h>
int main()
{int num[5],i,s,no;
for(i=0;i<5;i++)
{
    scanf("%d",&num[i]);
}
  printf("enter a number\n");
    scanf("%d",&no);
s=search(no,&num[0]);
printf("%d\n",s);
search1(no,num);
}

int i=0;
int search(int no,int *num[i])
{int x,i;
    for(i=0;i<5;i++)
    {
    if(no==*num[i])
    return 1;
    }
    return 0; 
}

int search1(int no,int *num)    
{   int i,x,k; 
    printf("your number %d is present in the list",no);
    printf("number\tfrequency");
    for(i=0;i<5;i++)
    {
        for(x=0;x<i+1;x++)
        {
        k=0;
        if(num[i]==num[x] &&i!=x)
        {k=0;
        break;}
        else
        {k=1;
        continue;}
        }
        if(k==1)
        printf("%d",num[i]);
}
}

Quote:

my code is compiling but showing segmentation fault error why?


Compiling just mean that the code respect language syntax, but ut doesn't mean it is worth a Pulitzer.

Use the debugger, it will help you to find where is the crash and probably the reason.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Proper indentation helps reading your code:

#include<stdio.h>
int main()
{
	int num[5],i,s,no;
	for(i=0;i<5;i++)
	{
		scanf("%d",&num[i]);
	}
	printf("enter a number\n");
	scanf("%d",&no);
	s=search(no,&num[0]);
	printf("%d\n",s);
	search1(no,num);
}

int i=0;
int search(int no,int *num[i])
{
	int x,i;
	for(i=0;i<5;i++)
	{
		if(no==*num[i])
			return 1;
	}
	return 0;
}

int search1(int no,int *num)
{
	int i,x,k;
	printf("your number %d is present in the list",no);
	printf("number\tfrequency");
	for(i=0;i<5;i++)
	{
		for(x=0;x<i+1;x++)
		{
			k=0;
			if(num[i]==num[x] &&i!=x)
			{
				k=0;
				break;
			}
			else
			{
				k=1;
				continue;
			}
		}
		if(k==1)
			printf("%d",num[i]);
	}
}


classic lines of code :-)
int i=0;
int search(int no,int *num[i]){ /// Oh really


What I was really impressed with is you took i outside the function to make it defined

The hint is you may care to look at how you passed the array into the function search1 ... hint

int search1(int no,int *num){