473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

segmentation fault

i got almost every time the segmentation error during running my programme.
why this error occur? how it can be removed?

i have a programme.......


#include<stdio.h>
#include<stdlib.h>
struct node
{
char name[10];
struct node *link;
};
void add (struct node **,char *);
void print(struct node *);
void main()
{
struct node *p;
int i,n;
char s[10];
printf("Enter no. of node, want to add :");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
printf("Enter name:");
gets(s);
add(&p,s);

}
print(p);

return ;
}
void add (struct node **q,char *t)
{
struct node *temp,*r;
int i,j;
temp=(struct node *)malloc(sizeof(struct node));
temp=*q;
if(*q==NULL)
{
*q=temp;
temp->link=NULL;
for(i=0;i<=9;i++)
{
(temp->name)[i]=*t;
t++;
}
}

else
{
while(temp->link!=NULL)
temp=temp->link;
r=(struct node *)malloc(sizeof(struct node));
temp->link=r;
r->link=NULL;
for(j=0;j<9;j++)
{
(r->name)[j]=*t;
t++;
}
}

return ;
}
void print(struct node *q)
{
struct node *temp;
temp=q;
while(temp->link!=NULL)
{
printf("\n");
puts(temp->name);
temp=temp->link;
}
puts(temp->name);
return ;
}





it also gives segmentation fault....
why???
contact at.......
pavankumar7860@gmail.com
Aug 11 '07 #1
1 932
weaknessforcats
9,208 Expert Mod 8TB
void add (struct node **q,char *t)
{
struct node *temp,*r;
int i,j;
temp=(struct node *)malloc(sizeof(struct node));
temp=*q;
You are overwriting the address in temp that you just got from malloc() with the address inside the pointer of the function argument. Did you mean:
Expand|Select|Wrap|Line Numbers
  1. *q = temp;
  2.  
??
Aug 11 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.