fscanf and pointer address question

H

haloman

Why does:
char *myString = (char *) malloc(WORDLIMIT*sizeof(char));
fscanf(openFile, "%s", myString) != EOF;

work yet

char *myString = (char *) malloc(WORDLIMIT*sizeof(char));
fscanf(openFile, "%s", &myString) != EOF;

doesn't (crashes, doesn't result in a compiler error)? I thought the
prior was incorrect, and the latter correct.

Many thanks.

P.S. Yes, I know I shouldn't be using fscanf like this, there are
buffer overflow problems and that I probably should be shot already,
but that's not the question being asked ;)
 
P

Peter Nilsson

Why does:
char *myString = (char *) malloc(WORDLIMIT*sizeof(char));
fscanf(openFile, "%s", myString) != EOF;

work yet

char *myString = (char *) malloc(WORDLIMIT*sizeof(char));
fscanf(openFile, "%s", &myString) != EOF;

doesn't (crashes, doesn't result in a compiler error)? I thought the
prior was incorrect, and the latter correct.
<snip>

You're confusing the situation of arrays as opposed to pointers. See
the FAQ:

http://c-faq.com/stdio/scanf1a.html
http://c-faq.com/aryptr/aryvsadr.html

Taking the address of a pointer yeilds a pointer to _that_ pointer,
not what that pointer points to.
 
S

santosh

Why does:
char *myString = (char *) malloc(WORDLIMIT*sizeof(char));

In C, you don't need to cast the return value of malloc. Also take the
size of the object using sizeof, rather than the size of the object's
type.
fscanf(openFile, "%s", myString) != EOF;

work yet

char *myString = (char *) malloc(WORDLIMIT*sizeof(char));
fscanf(openFile, "%s", &myString) != EOF;

doesn't (crashes, doesn't result in a compiler error)? I thought the
prior was incorrect, and the latter correct.

fscanf expects a pointer to the object meant to hold the result of each
conversion. In the second snippet, you've passed it a value of type
pointer to pointer to char.
 
N

Nick Keighley

(e-mail address removed) wrote:

char *myString = (char *) malloc(WORDLIMIT*sizeof(char));

the cast is unnecessary, and may hide an error. sizeof(char) is 1 by
definition
hence some would omit it.

char *myString = malloc (WORDLIMIT);

<snip>
 
R

Richard Heathfield

(e-mail address removed) said:
P.S. Yes, I know I shouldn't be using fscanf like this, there are
buffer overflow problems and that I probably should be shot already,
but that's not the question being asked ;)

In my experience, it's best to fix the stuff you know how to fix before
worrying about the stuff you don't know how to fix. That way, you don't get
distracted.
 
D

David T. Ashley

Why does:
char *myString = (char *) malloc(WORDLIMIT*sizeof(char));
fscanf(openFile, "%s", myString) != EOF;

work yet

char *myString = (char *) malloc(WORDLIMIT*sizeof(char));
fscanf(openFile, "%s", &myString) != EOF;

doesn't (crashes, doesn't result in a compiler error)? I thought the
prior was incorrect, and the latter correct.

Many thanks.

P.S. Yes, I know I shouldn't be using fscanf like this, there are
buffer overflow problems and that I probably should be shot already,
but that's not the question being asked ;)

Problem is "pointer to pointer to char" rather than "pointer to char", as
I'm sure you discovered.

One final note: "pointer to pointer to thingie" has its place. Sometimes
when you are using data structures where either:

a)The location might change in memory, OR

b)The canonical form of the empty set is a NULL pointer,

it is necessary to pass a pointer to a pointer to a data structure to a
function. That way, the function can swap out the pointer pointed to if
necessary.

But, as I'm sure you discovered ... not with scanf.
 

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

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,207
Latest member
HelenaCani

Latest Threads

Top