pemo said:
Richard Heathfield said:
(e-mail address removed) said:
Yes, it would. Firstly, I forgot to observe last time that you are using
g++, which is a C++ compiler rather than a C compiler. I suggest you use
gcc if you wish to program in C. If you prefer to program in C++, please
ask your C++ questions in comp.lang.c++. Thank you.
Secondly, I would suggest that, to deal with text input of any kind, you
read it as a string with fgets, and then do any necessary conversions of
the data yourself using, for example, strtol or sscanf. Otherwise you'll
just litter your code with getchar calls, at the expense of clarity.
Wouldn't something like this work ...
/* could perhaps pass in the buffer size too */
int stdinGet(const char * format, void ** dest)
{
char buffer[100];
if(fgets(buffer, sizeof(buffer), stdin))
{
return sscanf(buffer, format, &(*dest));
}
return 0;
}
int stdinGet(const char * format, void ** dest)
{
char buffer[100];
if(fgets(buffer, sizeof(buffer), stdin))
{
return sscanf(buffer, format, &(*dest));
}
return 0;
}
What's the type of the *dest above?
When used like this ...
int n;
stdinGet("%d", (void *)&n);
The routine's fine, but, if dest's type is changed to void *, gcc complains
about a void * dereference. This seems like a right hack to me - my code
above - but *as ever*, I'm confused: About the type of *dest, why the
compiler doesn't complain about the code 'as is', and why the call to the
routine with 'just' a void * cast isn't complained about - the expression
casts the int * to a void *, yet when this address is passed to stdinGet as
a void **, there's no complaint.