By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

fscanf and pointer address question

P: n/a
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 ;)

Jan 18 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ha*****@gmail.com wrote:
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.

--
Peter

Jan 18 '07 #2

P: n/a
ha*****@gmail.com wrote:
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.

Jan 18 '07 #3

P: n/a
Okay, I understand now. Many thanks :)

Jan 18 '07 #4

P: n/a
ha*****@gmail.com wrote:

<snip>
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>
--
Nick Keighley

Jan 18 '07 #5

P: n/a
ha*****@gmail.com 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 18 '07 #6

P: n/a
<ha*****@gmail.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
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.

--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)
Jan 18 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.