Segmentation fault

R

Ravikant

Hello,
Please check the following code .Let me know why its giving
the segmentation fault while running it.First it asks to enter the
name ,when entered it gives segmentation fault.
But if I dont use the insert() and written everything in
main(),it works fine.Please help me out.

#include<stdio.h>
typedef char* Name;
typedef struct node
{
Name name;
struct node *link;
}Data_node;
main()
{
insert();
}
insert()
{
Name Myname;
Data_node *p;
printf("Enter the name\n");
scanf("%s",Myname);
p = (Data_node *) malloc (sizeof (Data_node));
p->name = Myname;
printf("%s\n",Myname);
}

bye
Ravi
 
R

Richard Bos

But if I dont use the insert() and written everything in
main(),it works fine.

By accident. You're still invoking undefined behaviour.
#include<stdio.h>
typedef char* Name;
insert()
{
Name Myname;
Data_node *p;
printf("Enter the name\n");
scanf("%s",Myname);

Whoops... you never allocated memory for Myname to point at, so you're
writing into deep space. This causes undefined behaviour, which _may_
crash your program, _may_ accidentally work correctly, and _may_, at a
pinch, scribble over the stack so when this function returns it doesn't
jump back to main(), but to the routine to erase all files in the
current directory.
p = (Data_node *) malloc (sizeof (Data_node));

BTW, it isn't wise to cast malloc(). Doing so can (and in this case,
apparently, did) hide the mistake of not having a proper declaration for
malloc() in scope.
At the top of your file, where you #include <stdio.h>, you also need to
#include <stdlib.h>, which will declare malloc() for you.
Then, you should rewrite this line as

p = malloc(sizeof *p);

Note also the different sizeof; should the type of p ever change, this
line will automatically remain correct.

Richard
 
S

Sean Kenwrick

Ravikant said:
Hello,
Please check the following code .Let me know why its giving
the segmentation fault while running it.First it asks to enter the
name ,when entered it gives segmentation fault.
But if I dont use the insert() and written everything in
main(),it works fine.Please help me out.

#include<stdio.h>
typedef char* Name;
typedef struct node
{
Name name;
struct node *link;
}Data_node;
main()
{
insert();
}
insert()
{
Name Myname;
Data_node *p;
printf("Enter the name\n");
scanf("%s",Myname);
p = (Data_node *) malloc (sizeof (Data_node));
p->name = Myname;
printf("%s\n",Myname);
}

bye
Ravi

You have:

Name Myname;

Which is equivalent to

char * Myname;

Now figure out why:

scanf("%s",Myname);

causes a segmentation fault....

Sean
 
C

CBFalconer

Ravikant said:
Please check the following code .Let me know why its giving the
segmentation fault while running it.First it asks to enter the
name ,when entered it gives segmentation fault.

But if I dont use the insert() and written everything in
main(),it works fine.Please help me out.

#include<stdio.h>
typedef char* Name;
typedef struct node
{
Name name;
struct node *link;
}Data_node;
main()
{
insert();
}
insert()
{
Name Myname;
Data_node *p;
printf("Enter the name\n");
scanf("%s",Myname);
p = (Data_node *) malloc (sizeof (Data_node));
p->name = Myname;
printf("%s\n",Myname);
}

Start by properly indenting the code (tabs don't count), and
removing the ridiculous typedef of Name. Then read it yourself,
remembering that a dereferenced pointer should point somewhere.
 
A

Al Bowers

Ravikant said:
Hello,
Please check the following code .Let me know why its giving
the segmentation fault while running it.First it asks to enter the
name ,when entered it gives segmentation fault.
But if I dont use the insert() and written everything in
main(),it works fine.Please help me out.

Your defined type, Name, is a char pointer. You have not provided
any storage space for the string. Read the faq, especially read
question 7.1 located at: http://www.eskimo.com/~scs/C-faq/q7.1.html

You could make the typedef
typedef char Name[156]
This would allow storage for 156 characters.
#include<stdio.h>
Missing header
#include said:
typedef char* Name; tpyedef char Name[156];
typedef struct node
{
Name name;
struct node *link;
}Data_node;

missing prototype:

void insert(void);
main() int main(void)
{
insert(); return 0;
}
insert() void insert(void)
{
Data_node *p;
p = (Data_node *) malloc (sizeof (Data_node));
p = malloc(sizeof *p);
if(p)
{
> printf("Enter the name\n");
> scanf("%s",Myname);
scanf(" %155[^\n]",p->name);
}

Function insert as you wrote it is very poor. Because you do
not prototyped the function arguements or a return value, the
pointer to the dynamic allocations is lost and you have a memory
leak and you do not have any way to continue the link.

Redesigning the code some, you could do this:

#include<stdio.h>
#include <stdlib.h>

typedef char Name[156];
typedef struct node
{
Name name;
struct node *link;
}Data_node;

Data_node *insert(Data_node **head);

int main(void)
{
Data_node *head = NULL, *tmp;

insert(&head);
insert(&head);
for( ; head; head = tmp)
{
printf("The name: %s\n",head->name);
tmp = head->link;
free(head);
}
return 0;
}

Data_node *insert(Data_node **head)
{
Data_node *p;

p = malloc (sizeof *p);
if(p)
{
printf("Enter the name\n");
scanf(" %155[^\n]",p->name);
p->link = *head;
*head = p;
}
return p;
}
 
J

Jack Klein

Hello,
Please check the following code .Let me know why its giving
the segmentation fault while running it.First it asks to enter the
name ,when entered it gives segmentation fault.
But if I dont use the insert() and written everything in
main(),it works fine.Please help me out.

#include<stdio.h>
typedef char* Name;

Never, never, NEVER typedef names for pointer types unless they will
NEVER be dereferenced.
typedef struct node
{
Name name;
struct node *link;
}Data_node;
main()

Illegal under the current C standard, must be "int main()".
{
insert();

Illegal under the current C standard, call to function with no
prototype.

Invalid under all C standards where it is legal, main() is defined as
returning an (implicit) int, no such value returned.
}
insert()

Illegal under the current C standard, declarator lacks type.
{
Name Myname;

Never, never, NEVER hide a pointer that will be dereferenced behind a
typedef.
Data_node *p;
printf("Enter the name\n");
scanf("%s",Myname);
p = (Data_node *) malloc (sizeof (Data_node));

Undefined behavior under all versions of C, call to malloc() which
does NOT return an int with no prototype in scope.
p->name = Myname;
printf("%s\n",Myname);
}

bye
Ravi

Invalid human engineering, lack of sufficient use of white space.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,138
Messages
2,570,804
Members
47,349
Latest member
jojonoy597

Latest Threads

Top