String Handling

V

Vijay

When we use scanf to enter a string in a charecter array then we do not
use & sign before the string name because the name of the string points
to the address of the first charecter of the string.
 
S

santosh

Vijay said:
When we use scanf to enter a string in a charecter array then we do not
use & sign before the string name because the name of the string points
to the address of the first charecter of the string.

Specifically the name of an array resolves to a constant pointer to the
start of the array. So, if you pass an array of characters to scanf(),
passing the array name is sufficient.

Generally, don't use scanf() to input a string from an interactive
stream like stdin. When you omit the width modifier, it suffers from
the same problem as gets(), i.e. it can write past the end of the
buffer supplied to it. When width modifier *is* used, then scanf()
could leave characters waiting in the input stream's internal buffer,
which could confuse subsequent calls to scanf() itself or other input
functions.

Use fgets() or CBFalconer's ggets() routines and then use sscanf() to
convert the value if needed.
 
R

Richard Heathfield

Vijay said:
When we use scanf to enter a string in a charecter array

....we stop, we think very carefully, and then we almost certainly reconsider
our choice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top