The following code causes code blocks to stop. It is displayed as some problem caused the program to close. when i tried online compilation it showes as segmentation fault(core dumped). I couldn't find out what it is!
//Hash table
#include<stdio.h>
#include<string.h>
int main()
{
int option,i,key;
char value[10], hashtab[10][10];
for(i=0;i<10;i++)
{
strcpy(hashtab[i],'\0');
}
printf("\nEnter 1-Insert ANY-Exit");
scanf("%d",&option);
while(option==1)
{
printf("\nEnter the Value: ");
scanf("%s",value);
key=keygen(value);
strcpy(hashtab[key],value);
}
for(i=0;option==2&&i<10;i++)
{
printf("\n%s",hashtab[i]);
}
}
int keygen(char *value)
{
int i,key=0;
for(i=0;i<strlen(value);i++)
{
key=key+value[i];
}
return key;
}
strcpy used in the for loop should be arguments as below,
char *strcpy(char *dest, const char *src);
Ideally the second argument passed should be the string pointed to by src.
Making it to, strcpy(hashtab[i], "\0"); should work.
Related
I am constantly getting "Segmentation fault (Core dumped)" error in the following c program. Please help me debug the program.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
int numcmp(const char *a,const char *b){
if(atoi(a)==atoi(b)) return 0;
return -1;
}
int isequal(char *a,char *b,int (*cmp)(const char *,const char *)){
if(!cmp(a,b)) return 1;
return 0;
}
int main(int argc,char *argv[]){
if(argc!=3){
puts("Usage: Compare two string alphabeticaly or numericaly");
printf("Syntax: %s string1 string2\n",argv[0]);
exit(-1);
}
if(isequal(argv[1],argv[2],isalpha(argv[1])?strcmp:numcmp))
printf("%s and %s are equal\n",argv[1],argv[2]);
else
printf("%s and %s are not equal\n",argv[1],argv[2]);
}
isalpha only works for chars, not whole strings. You could write a function for that:
/* 1 - only letters, 0 - other characters */
int str_isalpha(const char* str) {
while (*str)
if (!isalpha(*(str++)))
return 0;
return 1;
}
and then use it appropriately. But actually, I don't see a reason for using numcmp here at all. Comparing ints and const char*s should yield the similar results.
I have one program called allloggedin.c, whose purpose is to display all logged in users.
Whenever I try to run it on terminal (using gcc), it gives me error as
"Segmentation fault (core dumped)".
#include<stdio.h>
#include<sys/utsname.h>
#include<utmp.h>
int main(void)
{
struct utmp *n;
char *a;
int i;
setutent();
n=getutent();
while(n!=NULL)
{
if(n->ut_type==7)
{
printf("%9s",n->ut_user);
printf("%12s",n->ut_line);
printf(" ");
for(i=4;i<16;i++)
printf("%c",a[i]);
printf(" (");
printf("%s10",n->ut_host);
printf(")");
printf("\n");
}
n=getutent();
}
}
Can anybody please explain me this program?
The Segmentation Fault you are getting is because of the line:
printf("%c",a[i]);
Since you didn't initialize the pointer a, it can point to any address, which may cause a Segmentation Fault when you dereference it.
I'm not sure what you are trying to print in the for loop so I removed it entirely from the code.
This is a cleaned version of the code:
#include<stdio.h>
#include<sys/utsname.h>
#include<utmp.h>
int main(void)
{
struct utmp *n;
setutent();
n=getutent();
while(n) {
if(n->ut_type==USER_PROCESS) {
printf("%9s%12s (%s)\n", n->ut_user, n->ut_line, n->ut_host);
}
n=getutent();
}
return 0;
}
I replaced the number 7 by the proper macro USER_PROCESS. Also, you can place all the prints in a single printf call.
So what the program basically does is to get the utmp struct for each user and print it in a fancy way.
For more information about the functions used and the utmp struct you can take a look at the utmp.h reference.
I am trying to implement this-
struct Car
{
int registrationNo;
char name[100];
};
struct ParkingLot
{
int ID;
struct Car *C;
}Park;
void Enter_ParkingLot()
{
Park.ID++;
int ID=Park.ID;
printf("Enter registraiotn no\n");
scanf("%d",&(Park.C+ID)->registrationNo);
}
int main()
{
Park.ID=-1;
Enter_ParkingLot();
return 0;
}
But I get this error-
Segmentation fault (core dumped)
I think there is something wrong with scanf statement.
You never allocate memory for Park.C.
I am getting a segmentation fault on the execution of the code below.The program compiles successfully ,but gives an error on execution.
What is a segmentation fault and how to correct it?
#include<stdio.h>
#include<malloc.h>
#include "sel.h"
#include<sys/time.h>
main(int argc,char *argv[])
{
struct timeval t1,t2;
struct timezone tz;
int i,n;
int *a;
char *num;
FILE *fp;
unsigned long long time1,time2;
fp=fopen("file1.txt","w");
sscanf(argv[1],"%d",&n);
a=(int*)malloc (sizeof(int)*n);
for(i=n;i>0;i--)
{
sprintf(num,"%d\n",i);
fputs(num,fp);
}
fclose(fp);
fp=fopen("file1.txt","r");
i=0;
while(fgets(num,255,fp)!=NULL)
{
sscanf(num,"%d",&a[i++]);
}
gettimeofday(&t1,&tz);
time1=t1.tv_sec*1000000+t1.tv_usec;
sel(a,n);
gettimeofday(&t2,&tz);
time2=t2.tv_sec*1000000+t2.tv_usec;
fclose(fp);
fp=fopen("file2","w");
for(i=0;i<n;i++)
{
sprintf(num,"%d\n",a[i]);
fputs(num,fp);
}
free(a);
printf("\n %llu",time2-time1);
}
Segmentation fault means you had a bad memory access. In this case, you never allocated any memory for num, so you are writing to an uninitialized pointer.
#include<stdio.h>
#include<stdlib.h>
int *id,N;
main()
{
FILE* file=fopen("a.txt","r");
int i,p,q,c;
fscanf(file,"%d",&N);
id=(int *)malloc(N*sizeof(int));
for(i=0;i<N;i++)
*(id+i)=i;
while(!feof(file))
{
fscanf(file,"%d %d",&p,&q);
if(!connected(p,q))
unn(p,q);
}
fclose(file);
c=1;
while(c==1)
{
scanf("%d %d",&p,&q);
printf("%d\nYes(1) or No(0) ",connected(p,q));
scanf("%d",&c);
}
}
connected(int p,int q)
{
return((root(p))==(root(q)));
}
unn(int p,int q)
{
int j=root(q);
int i=root(p);
*(id+j)=i;
}
root(int i)
{
while(i!=(*(id+i)))
i=*(id+i);
return(i);
}
On compiling, no error message is displayed. However, when I try to execute this program, it says"Segmentation Fault (core dumped). Why does this happen?
As may have noticed, this is an attempt to implement the quick union.
The quick find was implemented without any hassle using the same file "a.txt" with only a few tweaks to this code.
* The Yes or No is for the user's query.
I would guess this is the problem
root(int i)
{
int p;
while(p!=(*(id+p))) <-- p uninitialised
p=*(id+p);
return(p);
}
because p is uninitialised but you are using it as an offset in a pointer dereference. Even if it is not THE reason it is still a big problem.
You have since edited the question to leave
root(int i)
{
int i;
while(i!=(*(id+i)))
i=*(id+i);
return(i);
}
in which you redeclare an i variable locally as well as a i variable passed as function parameter.