SL> Be warned... I am a "newbie" to C. (Please don't hurt me
SL> too much...)
DM> Cool... a user from my own BBS... I'll only hurt you locally. ;->
SL> As opposed to globally huh? *chuckle*
Or statically. :-)
SL> char *sTemp;
SL> int iTemp;
SL> printf("Enter a number: ");
SL> scanf("%s", sTemp);
DM> Ooo.... so far, so bad. sTemp doesn't point anywhere. You need to
DM> give it some memory.
SL> I suppose that was a big no-no, huh? All those dang C
SL> tutorials are USELESS I tell you USELESS.
Yup - always use memory to store things in. :-) And only *some* tutorials
are useless... most of them are marginally useful to extremely useful. :-)
SL> BTW, why DOES it have to point to a memory location? And
SL> why do you have to define a specific maximum length to the string?
Where did you want to store the string? In memory, right? ;-) Therefore
you must grab exclusive hold of that memory. Think about it for a second as
this:
char *sTemp; /* say it points to 8000:0000 by random fluke */
When you put something into sTemp, which points to video memory (I think),
garbage appears on your screen. When you ask the runtime library to hand you
memory, it will make sure you don't get memory that points to a hardware
device.
As for the maximum length: because you need to reserve the memory before it
is used, thus you need to have a definable length to reserve. You choose
whether you want 30, 80, or 512 (or more!). The tradeoff is obviously having
less memory available for other tasks.
DM> #define TEMP_LEN 30
DM> char* sTemp = malloc(TEMP_LEN) * sizeof(char)); /*
DM> remember to free it
DM> afterwards */
SL> Okay. This means to allocate 30 chars with of 1 byte each,
SL> right? And I use free(sTemp) afterwards!
Woops - make that "malloc(TEMP_LEN * sizeof(char))" - had an extra
parenthesis there. :-) But, yes, that's what it means.
SL> (BTW, is there a difference between "char* sTemp" and
SL> "char *sTemp"?)
To the compiler, no. I use the former simply because I like to think of my
variables (even in C) as objects... in this case, sTemp is of type "char*".
The way you type it says that *sTemp is a char. Both are correct, just
different ways of looking at it.
DM> #define _STR(x) #x
DM> #define STR(x) _STR(x)
SL> All right, you've gone and lost me here. Please explain
SL> those two previous #defines...
Those #defines are in snippets... with an explanation, I believe. :-)
DM> Let's try two different solutions at this point.
DM> 1. Use scanf.
SL> Okay. I understood that.
DM> 2. Parse everything manually
SL> Got this one too...
My prefered option is #2.
DM> What a lot of work to do error-correction
DM> (error-protection?)!
DM> Thus
DM> the reason for so many generic user-entry modules... so we don't have
DM> to continually do all of this... :-)
SL> Generic user-entry modules? You mean extra... dang, I
SL> don't know the C terminology...
I mean a function that you tell it what you want, it goes and gets it and
validates it. For example, a function that gets a float between x and y. Or
a function that gets a last name. This can be done in text mode, or in a
windowing system (Windows, OS/2's PMShell, Unix's X-clones, etc.) via a text
box or other input mechanism.
---
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)
|