2
#include<iostream.h>
#include<stdlib.h>

using namespace std;

char*   Gets(char *s)
{
  char ch,*p;
  p=s;
  while ( (ch=getchar()) != '0' )
  {
   *s=ch;
   s++;
  }
 s='\0';
 return p; //return the address of S stored in P.

}

int main(int argc,char* argv[])
{
  //char s[200];
  char *s;
  s=Gets(s);
  cout<<"\n After Gets Value of S=["<<s<<"] \n";
  return 0;
}

If I use char *s I am getting the output as

Segmentation fault:11

If I use char s[200] there is no error. Why am I getting segmentation fault?

abhishek_naik
  • 1,287
  • 2
  • 15
  • 27
Riz
  • 31
  • 3
  • 1
    You need to allocate memory in the heap Irvin the stack to hold 200 chars. – James McLeod Aug 30 '16 at 02:25
  • 2
    `using namespace std;` and `cout<<"\n After Gets Value of S=["< – chux - Reinstate Monica Aug 30 '16 at 02:40
  • Do you actually want to end the string when it reads the character `'0'`? – Dmitri Aug 30 '16 at 02:51
  • If you're trying to mimic the (obsolete and not recommended) `gets()` function from the (old) standard library, you need to end your string when you finish the line... so look for `'\n'`, not `'0'`. Also, `getchar()` returns an `int` not a `char` -- if you assign to a `char` directly, you won't be able to detect errors/EOF properly. – Dmitri Aug 30 '16 at 02:55
  • See [Why the `gets()` function is too dangerous to be used — ever!](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) for why you should not attempt to write a simulation for the function — it won't be safe to use it, and using it will frequently lead to segmentation faults. That said, your problem is that you aren't allocating space for your `Gets()` to write to when it crashes — which invokes undefined behaviour. Undefined behaviour is undefined behaviour, and crash is an acceptable response to invoking undefined behaviour. Don't! – Jonathan Leffler Aug 30 '16 at 03:45

2 Answers2

2

There is no memory allocation for the string. The declaration char *s; merely allocates a pointer to a string, not the string itself. Try char s[<some number>]; instead.

Also s = '\0'; should be something like *s = 0;.

Lastly s = Gets(s); should just be Gets(s); as you are passing in a pointer to allocated memory.

Phil Wallach
  • 3,318
  • 20
  • 19
2

why am i getting segmentation fault.

Code does not properly terminate the character array s='\0'; nor use assign memory with char *s;


gets(), as of C11, is no longer part of the standard C library. Usage of it is not robust programming as it does not prevent buffer overruns.

OP likely wanted the following. Corrections noted.

char *Gets(char *s) {
  // Use int to distinguish the typical 257 different returns values of getchar() 
  int ch;
  char *p = s;

  // stop when a \n or EOF encountered 
  // while ( (ch=getchar()) != '0' )
  while ( (ch=getchar()) != '\n' && ch != EOF) {
    // Note lack of check for adequate array space
    *s = (char) ch;
    s++;
  }

  // Append a null character, not assign a zero to the pointer
  // s='\0';
  *s = '\0';
  return p;
}

int main(void) {
  // Use an array, not an unassigned character pointer
  char s[200];
  // char *s;

  // Cannot assign to an array
  // s=Gets(s);
  Gets(s);

  // Use C code
  //cout<<"\n After Gets Value of S=["<<s<<"] \n";
  printf("\n After Gets Value of S=[" "%s" "] \n", s); 
  return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256