rahul_patil
rahul_patil

Reputation: 25

Segmentation fault (core dumped) in programme in c?

I'm a student and I m learning c (programming in ANSI c -> fifth edition) and facing the below error:

I'm implementing one program with typedef

In below c program give an error:

main.c:8:6: warning: ‘gets’ is deprecated [-Wdeprecated-declarations]                                                                                                      
/usr/include/stdio.h:638:14: note: declared here                                                                                                                           
main.c:(.text+0x1f): warning: the `gets' function is dangerous and should not be used.                                                                                     
enter name:cara                                                                                                                                                            
Segmentation fault (core dumped) 

program:

#include <stdio.h>

char * read(void);   //here I m writing prototype but I don't know what is the prototype and why here write the prototype?
char * read(void)
{
     char * name;
     printf("enter name:");
     gets(name);  //get string input using gets(..) function
     return name;
}

void main()
{
   char * name;
   name = read();
   printf("welcome,%s\n",name);
}

above program is a complexity that is why I m using typedef in the below program:

this below program continuously run why?

#include <stdio.h>

typedef char * string;

string read(void);   
string read(void)
{
     string name;
     printf("enter name:");
     gets(name); 
     return name;
}

void main()
{
   string name;
   name = read();
   printf("welcome,%s\n",name);
}

What I'm doing wrong?

Upvotes: 1

Views: 89

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

The program has undefined behavior because you are using an uninitialized pointer that has an indeterminate value

 char * name;
 printf("enter name:");
 gets(name);

You need to allocate memory where you are going to read a string.

As for the function gets then it is indeed an unsafe function and is not supported by the C Standard anymore. Use instead the standard C function fgets.

Pay attention to that according to the C Standard the function main without parameters shall be declared like

int main( void )

And using a typedef like this

typedef char * string;

is a bad idea. For example using this typedef name you are unable to declare a pointer to constant data like

const char *p;

because this declaration

const string p;

is not equivalent to the above declaration but means the following declaration

char * const p;

The program can look for example the following way

#include <stdio.h>

char * read( char *s, size_t n )
{
    s[0] = '\0';

    printf( "Enter name: " );
    fgets( s, n, stdin );

    return s;
}

int main( void )
{
    enum { N = 100 };

   char name[N];

    printf( "%s", read( name, N ) );
}

Its output might look like

Enter name: rahul_
rahul_

Pay attention to that the function fgets can append the entered string with the new line character '\n'. To remove it you can use the following trick

#include <string.h>

//...

name[ strcspn( name, "\n" ) ] = '\0';

Upvotes: 0

Aplet123
Aplet123

Reputation: 35530

There are a couple of things wrong with this. When you do char * name, you define name as a char pointer, but you don't actually allocate any space for the string to be stored. Hence, when you try to write values into that string, you're writing values in a random place that may not be writable or may contain crucial data that cannot be overwritten. Instead, try declaring name as char name[256]; to allocate enough space for it. Also, don't use gets, as it can lead to very, very, nasty things. Instead, use fgets to read input, and provide an upper limit on the number of characters equal to the amount of data you allocated. So, if you declared name as char name[256];, call fgets with fgets(name, 256, stdin);

Upvotes: 1

Related Questions