Inconsistent Program Results



I am learning C, having fun with strings & pointers at the moment!

The following program is my solution to an exercise to take an input,
strip the first word, and output the rest. It works fine when you give
it 2 or more words, but when there's only 1 word the results vary
depending on whether it's on Windows or Linux: under MSVC it displays
no output (as it should); under gcc/Linux it instead gives
"Segmentation fault".

Any ideas what's going on?

TIA!


#include <malloc.h>
#include <stdio.h>
#include <memory.h>

#define LEN 1000

void rdinpt();

void main()
{
char *s, *restrict;
rdinpt(&s);
restrict=strchr(s,' ');
printf("%s\n", ++restrict);
free(s), s=restrict=0;
return (0);
}

void rdinpt(char **s)
{
*s=(char *) malloc( (unsigned int) LEN);
(void) gets(*s);
return(0);
}

.



Relevant Pages

  • Re: Inconsistent Program Results
    ... The following program is my solution to an exercise to take an input, ... strip the first word, and output the rest. ... declaring main with a return type of void is a gross error. ...
    (comp.lang.c)
  • race condition during termination process
    ... Have you ever heard about lupg's multithread tutorials? ... There are 2 exercise at the last part of chapter 7.7(Threads ... int request_index; ...
    (comp.programming.threads)
  • Re: InitializeCriticalSection code in kernel
    ... After calling the same for about ... I think this exercise brings out the purpose of my initial mail. ... void createcritical; ... privilege apps can crash themselves but nobody else, ...
    (microsoft.public.windowsce.platbuilder)
  • Re: realloc(): invalid next size
    ... void *s2=NULL; ... but a size zero is. ... it's based on compiler secrets. ... I understand this is nothing more than an exercise in second ...
    (comp.lang.c)
  • Re: sizeof(ptr) = ?
    ... A void * has the same representation as a char *. ... Char pointers which require additional data ... iff the default offset is 0. ...
    (comp.lang.c)