Click here to Skip to main content
16,003,345 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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]);
}
}
Posted
Updated 2-Mar-17 17:20pm
Comments
jeron1 2-Mar-17 16:48pm    
    for(i=0;i<5;i++)
    {
        for(x=0;x<i+1;x++)
        {
        k=0;
        if(num[i]==num[x] &&i!=x)  <====


What happens when i equals 4,
x = 5 and num[5] is invalid (valid indexes are 0 - 4). There may be other things though
. Also, the formatting is brutal. A debugger would prove useful here.
Member 13015247 2-Mar-17 17:10pm    
x<5 at that time when i=4.the program i correct there is problem in *num like thing but i could not specify the error.
jeron1 2-Mar-17 17:29pm    
Doh! My mistake...sorry. Could you set a breakpoint and step through the code?
jeron1 2-Mar-17 17:55pm    
How is it compiling? search1 should return an int but doesn't.
Graeme_Grant 2-Mar-17 18:38pm    
This sounds like a homework question to me. You know you are going to struggle to find anyone who will do your homework for you.

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:
C++
#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]);
	}
}
 
Share this answer
 
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){
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900