🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: be.comp.programming
Article #12844

Re: Segmentation Fault

#12844
From: Wim Dumon
Date: Wed, 26 Sep 2001 14:27
61 lines
2866 bytes
Karel wrote:
> 
> 
> main(void)
> {
>  char *string1, *string2;
>  printf("Geef getal: ");
>  scanf("%s",string1);
>  printf("Het ingevoerde getal is %s.",string1);
>  printf("\nGeef tweede getal: ");
>  scanf("%s",string2);
>  printf("\nHet ingevoerde getal is %s.",string2);
> }
> 

Uit de manpage van scanf, met als optie %s:

s: Matches  a  sequence of non-white-space characters;
   the next pointer must be a pointer to char, and the
   array  must  be  large  enough  to  accept  all the
   sequence and the terminating  NUL  character.   The
   input string stops at white space or at the maximum
   field width, whichever occurs first.

U definieert wel pointers, maar die wijzen naar nergens. Je
pointers moeten wijzen naar een array 'large  enough  to
accept all the sequence and the terminating NUL character'

Bij scanf is dat altijd een beetje gokken, want je weet
nooit wat de user zal invullen. Als de user niet teveel
typt, zijn dit mogelijke oplossingen voor uw probleem:

{
 // string1 en 2 laten pointen naar memory op de heap
 char *string1, *string2;
 string1 = (char *)malloc(20);
 string2 = (char *)malloc(20);
 printf("Geef getal: ");
 scanf("%s",string1);
 printf("Het ingevoerde getal is %s.",string1);
 ...
 free(string1);
 free(string2);
}

oftewel:

{
 // string1 en 2 laten pointen naar memory op de stack
 char string1[20], string2[20];
 printf("Geef getal: ");
 scanf("%s",string1);
 printf("Het ingevoerde getal is %s.",string1);
 ...
 // geheugen wordt automatisch vrijgegeven door afbraak
 // van de stack
}


Groetjes,
Wim.
Technical Details
Message-ID: <3BB1E5E3.219F7166@easics.be>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!news.tele.dk!small.news.tele.dk!193.190.198.17!newsfeeds.belnet.be!news.belnet.be!news2.kpn.net!news.kpn.net!nslave.kpnqwest.net!nloc2.kpnqwest.net!nmaster.kpnqwest.net!reader3.kpnqwest.net.POSTED!not-for-mail
References: <ofks7.72306$6x5.15824854@afrodite.telenet-ops.be> <MFks7.72338$6x5.15835954@afrodite.telenet-ops.be> <BMks7.72346$6x5.15839206@afrodite.telenet-ops.be>

Show Full Headers
Complete Article Headers
X-Google-Language: DUTCH,ASCII-7-bit
X-Google-Thread: fcea4,235d965c7bd3acb4
X-Google-Attributes: gidfcea4,public
X-Google-ArrivalTime: 2001-09-26 07:27:48 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!news.tele.dk!small.news.tele.dk!193.190.198.17!newsfeeds.belnet.be!news.belnet.be!news2.kpn.net!news.kpn.net!nslave.kpnqwest.net!nloc2.kpnqwest.net!nmaster.kpnqwest.net!reader3.kpnqwest.net.POSTED!not-for-mail
Message-ID: <3BB1E5E3.219F7166@easics.be>
From: Wim Dumon <wim@easics.be>
X-Mailer: Mozilla 4.76 [en] (X11; U; Linux 2.4.2-2 i686)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: be.comp.programming
Subject: Re: Segmentation Fault
References: <ofks7.72306$6x5.15824854@afrodite.telenet-ops.be> <MFks7.72338$6x5.15835954@afrodite.telenet-ops.be> <BMks7.72346$6x5.15839206@afrodite.telenet-ops.be>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 61
Date: Wed, 26 Sep 2001 14:27:47 GMT
NNTP-Posting-Host: 193.74.198.7
X-Complaints-To: abuse@Belgium.EU.net
X-Trace: reader3.kpnqwest.net 1001514467 193.74.198.7 (Wed, 26 Sep 2001 16:27:47 MET DST)
NNTP-Posting-Date: Wed, 26 Sep 2001 16:27:47 MET DST
Organization: KPNQwest customer news service