#: 15742 S12/OS9/68000 (OSK)
02-Jul-92 06:58:37
Sb: #15741-#C_Question
Fm: Pete Lyall 76703,4230
To: LARRY OLSON 72227,3467 (X)
Larry -
Your problem are the lines:
char *a[5];
.... AND
gets(a[x]);
If that's the way the book reads, you should shoot the author!
In the statement: char *a[5]
You're stating that 'a' is an array of pointers with 5 elements. These pointers
point at SOMETHING of type 'char' - but WHAT? You haven't allocated any space
for them to point at, nor have you initialized the pointers to point AT
anything. There's no magic in C.. you have to do all the dirtywork yourself
(grin).
This problem doesn't rear its ugly head until:
gets(a[x]);
Which says: get a string [terminated by a return] from the keyboard (STDIN),
and stuff it in the location pointed to by the pointer I'm giving you.... Well,
we already know that the pointer is crap, and we have no idea what it points
to, AND whatever it DOES point to will be clobbered.
This program could be made to work by changing the declaration of 'a' to: char
a[5][256], which states that 'a' is an array of 5 arrays of 256 characters.
This pre-allocates space to put the strings in, and also implicitly supplies
pointers to the 0th character of each 256 character array when you call it
a[0], a[1], etc. (in C, an array NAME by itself is the same as the pointer to
the 1st [0th] element of the array... i.e. 'a' is the same as &a[0], and a[4]
is the same a[4][0].
Goo luk!
Pete
Theres1 Repy.
#: 15S12/S9/6800 (OS)
03-ul-92 01:1249
S1542-#C_uestio
Fm: LARRY ON 72227,3
To: Pete Lyal 7670,4230 X)
Pete,
Sounds like I beter thrat book awy, or at lest pu it awy
until I kno somethng abot C andn spo mistaes likethat. e was
using that routine toshow hthe follngBasic outine ionvrted to C.
10 DIM5)
20 FOR x=0 TO 3 INPUT a$()
40 T x
50 FOR x=0 TO
6RIT a$(x
70 NEXT x
80 EN
Thanks Pete, Ill seewhat oher truble can get ito
Larry
er is 1 Rply.
#: 15765 S12OS9/6800 (OS)
03-Jul92 1326:24
#1556-#C_uestio
Fm: Pete Lyll 7673,4230 To: LARRY OLSON 72227,34)
The problem her is tht Basicdoes atomati (dc)g alloationa
well as othvariables).You'reright ... oss th book Thatept ws at
such lemntary lvel that tis guound to hose ouup at the moe
complex levl.
*ABSOLUTE BEST* book I have eer seenhe C eophte (andthe oe thatok the ie forme afer buing ore than half dozen othes) was "Therimr PluThee Grou. Eading, ots ofexamplend a god
sense of hmor aswelll wort the 25 r soIW, the local ollege uses
it their C txtbook
There is Repl.
#: 15712/6800 (OSK)
04-Jul-9 03:22:1
Sb: #15765-C_Qion
Fm: LARRY OLSON 7227,3467
To: Pete Lyal 76703,423 (X)
Pete Thanks forthe tip onthe book(C Primer Pus), I'll pck it up.I've gota
dozenC books hee but none farry
|