🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: att.lang.c++
Article #104

Re: scan string to a pointer

#104
From: 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
Technical Details
Message-ID: <9ttfe5$4e4$2@oravannahka.helsinki.fi>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.media.kyoto-u.ac.jp!news.cc.tut.fi!news.helsinki.fi!not-for-mail
References: <9ttf05$9od3@news-dxb.emirates.net.ae>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 109fba,3cb615a1bc522f6e
X-Google-Attributes: gid109fba,public
X-Google-Thread: 1014db,3cb615a1bc522f6e
X-Google-Attributes: gid1014db,public
X-Google-Thread: 10d4ba,3cb615a1bc522f6e
X-Google-Attributes: gid10d4ba,public
X-Google-ArrivalTime: 2001-11-26 05:21:43 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.media.kyoto-u.ac.jp!news.cc.tut.fi!news.helsinki.fi!not-for-mail
From: Joona I Palaste <palaste@cc.helsinki.fi>
Newsgroups: att.lang.c++,comp.lang.c,comp.lang.c++
Subject: Re: scan string to a pointer
Date: 26 Nov 2001 13:17:57 GMT
Organization: University of Helsinki
Lines: 32
Message-ID: <9ttfe5$4e4$2@oravannahka.helsinki.fi>
References: <9ttf05$9od3@news-dxb.emirates.net.ae>
NNTP-Posting-Host: sirppi.helsinki.fi
X-Trace: oravannahka.helsinki.fi 1006780677 4548 128.214.205.27 (26 Nov 2001 13:17:57 GMT)
X-Complaints-To: usenet@news.helsinki.fi
NNTP-Posting-Date: 26 Nov 2001 13:17:57 GMT
User-Agent: tin/1.4.2-20000205 ("Possession") (UNIX) (OSF1/V4.0 (alpha))