Inconsistent Program Results
- From: Francine.Neary@xxxxxxxxxxxxxx
- Date: 6 Mar 2007 14:41:41 -0800
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);
}
.
- Follow-Ups:
- Re: Inconsistent Program Results
- From: John Bode
- Re: Inconsistent Program Results
- From: Martin Ambuhl
- Re: Inconsistent Program Results
- From: D. Power
- Re: Inconsistent Program Results
- From: Jens Thoms Toerring
- Re: Inconsistent Program Results
- From: Richard Heathfield
- Re: Inconsistent Program Results
- Prev by Date: Re: Can someone please tell me where I am going wrong??
- Next by Date: Re: Is there a real need to use keyword static with functions?
- Previous by thread: Bitfield casting
- Next by thread: Re: Inconsistent Program Results
- Index(es):
Relevant Pages
|
|