void* question

C

CBFalconer

Michael said:
Umh, I am not sure what you mean. Maybe there is a
misunderstanding. Say, we have the following "program":

___________________________________________
#include <stdarg.h>
#include <stdio.h>

int foo (int is_intptr, void *ptr);
int bar (int howmany, ...);

int main (void)
{
int baz = 77;

printf("foo: %d\t", foo(1, &baz));
printf("baz: %d\n", baz);

return 0;
}

int foo (int is_intptr, void *ptr)
{
if (is_intptr)
return bar(1,(int *) ptr); /*<---*/
else
return -1;
}

int bar (int howmany, ...)
{
int m, *p;
va_list args;

va_start(args, howmany);

m = 0;
while (0 < howmany--) {
p = va_arg(args, int *);
m += *p - *p%42;
*p |= m;
}
va_end(args);

return m&0x42;
}
___________________________________________

Would you leave out the cast at the marked line in foo()?
If yes: What if the representations of (void *)&baz and
(int *)&baz were different?

No, but that is because bar is variadic, and ALL parameter values
have to be of the type expected. There is no parameter type here
to specify the conversion.

I guess I spoke too soon in respect to variadic functions. But not
with respect to properly prototyped normal functions. After
pointers, variadic functions are one of the largest sources of
error in C programming.
 
D

Daniel Haude

On 9 Dec 2004 12:49:37 GMT,
Dan Pop said:
If it's homework, it's the most stupid kind of homework I have ever seen.

It sounds 100% like homework.
These are the things that have to be taught to the student, rather than
asking him to figure them out by himself.

I'm sure an attempt has been made to teach these things to Janice, but
since the effort seems to have been made in vain we're now treated to
homework questions.

--Daniel
 
H

Herbert Rosenau

Use a `void*' when you do not know the pointed-to
object's actual type, or do not care what it is, or
wish to conceal it.


When both the arguments and the returned value meet
one or more of the criteria above.


The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.

No. You can simple assign a void* to any other pointer type:

int *p_i;
void *pv;
....
p_i = pv;
pv = p_i;

NO cast needed.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
H

Herbert Rosenau

C'mon, Ben: Leave her professor *some* excuse for taking
off a mark or two, won't you?

Janice: If your questions aren't homework, I repeat my
offer to try to answer more clearly if you'll explain your
difficulty in greater detail. If they *are* homework, you
can find the answers to these and to all your other C questions
in "The Annotated ANSI C Standard" by Herbert Schildt.
Urgs. Don't point to crap like this. Schildt knows nothing about the
standard! The only the book WAS good for was that it printed out the
old standard for less one had to pay for the original - but any word
oh him is crap and has nothing to do with the thing he means he
annostated.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 
E

Eric Sosman

Herbert said:
Janice said:
[...]
How can we cast the void* pointer to the type we need?

The only way to cast anything is with a cast operator.
To convert a `void*' to an `int*', say, use `(int*)voidptr'.

No. You can simple assign a void* to any other pointer type:

int *p_i;
void *pv;
...
p_i = pv;
pv = p_i;

NO cast needed.

The question was "How can we cast," not "How can we
convert" or "How can we assign." I gave an exact and
literal answer rather than a possibly more helpful one
because I suspected homework.
 
R

Richard Bos

Eric Sosman said:
The question was "How can we cast," not "How can we
convert" or "How can we assign." I gave an exact and
literal answer rather than a possibly more helpful one
because I suspected homework.

Ordinarily, I'd agree with you. In this case, however, we're dealing
with casts. The OP might end up being yet another of the legion who
really do believe that you need to cast left, right and centre, and in a
few years' time we'll have to deal with malloc()-caster number thatmuch.

Richard
 

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
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top