🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by morano Mon, 26 Nov 2001 13:14
scan string to a pointer
#103
Author: morano
Date: Mon, 26 Nov 2001 13:14
7 lines
1315 bytes
Hi,
I was trying to scan a string from the keyboard

scanf and puts would work if my string was declared as an array.

However, My string is declared as a pointer, how can i scan a value to this
pointer location. I tried gets(&string) even gets(*string) yet didnt work
Re: scan string to a pointer
#104
Author: Joona I Palaste
Date: Mon, 26 Nov 2001 13:17
32 lines
2437 bytes
morano <alomrani@emirates.net.ae> scribbled the following
on comp.lang.c:
> Hi,
> I was trying to scan a string from the keyboard

> scanf and puts would work if my string was declared as an array.

> However, My string is declared as a pointer, how can i scan a value to this
> pointer location. I tried gets(&string) even gets(*string) yet didnt work

In C, you scanf a "pointer string" just like you scanf an "array
string". When used as rvalues (i.e. when not assigning stuff with the
= operator or its friends) (other than with the sizeof operator), arrays
and pointers are equivalent.
So if you have...
char string[100];
scanf("%s", string);
then you can just as well have...
char *string;
string=malloc(100);
scanf("%s", string);
Also, gets(string) works, but it should be avoided, since you have no
way of preventing buffer overflow.

-- 
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
and vow excitedly zooming."
   - JIPsoft
Re: scan string to a pointer
#105
Author: kers
Date: Mon, 26 Nov 2001 14:00
35 lines
2549 bytes
In article <9ttf05$9od3@news-dxb.emirates.net.ae>,
	"morano" <alomrani@emirates.net.ae> writes:
> Hi,
> I was trying to scan a string from the keyboard
> 
> scanf and puts would work if my string was declared as an array.
> 
> However, My string is declared as a pointer, how can i scan a value to this
> pointer location. I tried gets(&string) even gets(*string) yet didnt work

First hint: since the Dark Genesis hasn't happened yet, there aren't
many telepaths around. Without seeing your code it's a trifle difficult
to debug it.

Second hint: if you're trying to read into a "string declared as a
pointer", by which I guess you to mean something like:

    char *myString;

then not that you'll *have* to assign something sensible to `myString`.
Declaring it as a pointer doesn't make it point anywhere; it's just
junk. You'll probably need to use `malloc`.

Third hint: random guessing isn't a good way to learn how to program.
Get a decent book on C. Read & understand it. It will help a *lot*.

Zeroth hint: There is infinite time. You are finite. Zathras is finite. 
gets ... is the wrong tool. [rummaging] No, not good. Not good. No, never 
use gets. 

(garbled in the original)

-- 
Chris "Zathras trained in crisis management. Need Zathras NOW!" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads