void arg to function

P

Pushker Pradhan

I've a function which accepts void * as the arg because the actual datatype
can be char or float or anything.
The fuction:
void convolve(void *x, float *convx, uint32 numrowsx, uint32 numcolsx, float
*h,
uint32 hlen, dwt_process ROWSORCOLS)

Usage in main()
convolve((float*)x, xLP, tileLength, tileWidth, ld, filterlen, DWT_ROWS);

However I get these compile errors
Redstone[93] pushkar$ gcc -g -o main dwt.c convolve.c dnsample.c
getWaveletCoeffs.c -ltiff -lm
convolve.c: In function `convolve':
convolve.c:46: warning: dereferencing `void *' pointer
convolve.c:46: void value not ignored as it ought to be
convolve.c:62: warning: dereferencing `void *' pointer
convolve.c:62: void value not ignored as it ought to be

Can anyone tell what's wrong with my code? Thanks,
 
B

Ben Pfaff

Pushker Pradhan said:
I've a function which accepts void * as the arg because the actual datatype
can be char or float or anything.
The fuction:
void convolve(void *x, float *convx, uint32 numrowsx, uint32 numcolsx, float
*h,
uint32 hlen, dwt_process ROWSORCOLS)

Usage in main()
convolve((float*)x, xLP, tileLength, tileWidth, ld, filterlen, DWT_ROWS);

However I get these compile errors
Redstone[93] pushkar$ gcc -g -o main dwt.c convolve.c dnsample.c
getWaveletCoeffs.c -ltiff -lm
convolve.c: In function `convolve':
convolve.c:46: warning: dereferencing `void *' pointer
convolve.c:46: void value not ignored as it ought to be

You didn't show us the function convolve(), but the compiler says
the error is there. This makes it difficult to help you.

However, you probably wrote, within convolve, something like *x
or x. As the warning says, you can't dereference a void
pointer. You have to convert it to another kind of pointer,
then dereference the converted pointer.
 
P

Pushker Pradhan

Sorry I forgot to show the usage, it's just like you said:
*convx += *(x + n - m) * *(h + m);

So does that mean inside convolve.c I need to convert it to float or
something first?
But then it seems like using void is totally unnecessary? Can you show what
I need to do inside convolve.c?

--
Pushkar Pradhan
Ben Pfaff said:
Pushker Pradhan said:
I've a function which accepts void * as the arg because the actual datatype
can be char or float or anything.
The fuction:
void convolve(void *x, float *convx, uint32 numrowsx, uint32 numcolsx, float
*h,
uint32 hlen, dwt_process ROWSORCOLS)

Usage in main()
convolve((float*)x, xLP, tileLength, tileWidth, ld, filterlen, DWT_ROWS);

However I get these compile errors
Redstone[93] pushkar$ gcc -g -o main dwt.c convolve.c dnsample.c
getWaveletCoeffs.c -ltiff -lm
convolve.c: In function `convolve':
convolve.c:46: warning: dereferencing `void *' pointer
convolve.c:46: void value not ignored as it ought to be

You didn't show us the function convolve(), but the compiler says
the error is there. This makes it difficult to help you.

However, you probably wrote, within convolve, something like *x
or x. As the warning says, you can't dereference a void
pointer. You have to convert it to another kind of pointer,
then dereference the converted pointer.
 
B

Ben Pfaff

Pushker Pradhan said:
Sorry I forgot to show the usage, it's just like you said:
*convx += *(x + n - m) * *(h + m);

Why are you using obfuscatory pointer arithmetic *(a + b) syntax
instead of clarifying and normal array access a syntax?
So does that mean inside convolve.c I need to convert it to float or
something first?
But then it seems like using void is totally unnecessary? Can you show what
I need to do inside convolve.c?

I am confused about your actual situation. You claimed that
(presumably a pointer to) any type is expected by convolve() and
that therefore you were using void * for the parameter type. Now
you're claiming that you want to do arithmetic on the values
pointed to. These two are close to mutually exclusive: you can't
do arithmetic on a value unless you know its type, and if you
know its type then you might as well pass the proper pointer type
instead of void *.
 
M

Malcolm

Pushker Pradhan said:
But then it seems like using void is totally unnecessary? Can you show >
what I need to do inside convolve.c?The value x cannot be used in convolve until it is cast to a pointer to an
actual data type.

For instance if you know that x is an array of floats

float xf = x;

xf[0] = 1.0;

What is the point of making x a void * ? If you know that x is always a
floating point array, none. However it may be that sometimes x is allowed to
be double. The function convolve then needs some way of knowing what data it
has been passed in x. You could pass a flag as an extra paameter to achieve
this.
 
G

Gordon Burditt

I've a function which accepts void * as the arg because the actual datatype
can be char or float or anything.

I hope you mean "POINTER to char or POINTER to float or POINTER to anything".
The fuction:
void convolve(void *x, float *convx, uint32 numrowsx, uint32 numcolsx, float
*h,
uint32 hlen, dwt_process ROWSORCOLS)

Usage in main()
convolve((float*)x, xLP, tileLength, tileWidth, ld, filterlen, DWT_ROWS);

However I get these compile errors
Redstone[93] pushkar$ gcc -g -o main dwt.c convolve.c dnsample.c
getWaveletCoeffs.c -ltiff -lm
convolve.c: In function `convolve':
convolve.c:46: warning: dereferencing `void *' pointer
convolve.c:46: void value not ignored as it ought to be
convolve.c:62: warning: dereferencing `void *' pointer
convolve.c:62: void value not ignored as it ought to be

Can anyone tell what's wrong with my code? Thanks,

You didn't show the code that has the problem in it.

Don't dereference a void * pointer. First you figure out what the
pointer really points to, and NO, the compiler is not going to
help. Normally you end up having to pass an argument that tells
the function what you're giving it (printf() uses a format string
for this purpose). If you LIE about what you passed, your
function will probably screw up.

Then cast the void * pointer to a pointer to the appropriate type,
and dereference that.

Gordon L. Burditt
 
P

Pushker Pradhan

Here's the use in convolve:
void convolve(void *x, float *convx, uint32 numrowsx, uint32 numcolsx, float
*h,
uint32 hlen, dwt_process ROWSORCOLS)

Use:
*convx += *(x + n - m) * *(h + m);
*(convx + n*numcolsx + c) += *(x + (n - m)*(numcolsx) + c) * *(h + m);

Since this doesn't compile I cast the pointer to float like this:
*convx += *((float*)x + n - m) * *(h + m);
*(convx + n*numcolsx + c) += *((float*)x + (n - m)*(numcolsx) + c) * *(h +
m);

This compiles but I don't know whether it will work.

"x" can be unsigned char or float or unsigned 16 bit (it's a image data).
But the above mentioned computation cannot be done between an unsigned
char/int and floats, right? So I just cast x to float,
Please tell me if this is okay.
--
Pushkar Pradhan
Ben Pfaff said:
Pushker Pradhan said:
Sorry I forgot to show the usage, it's just like you said:
*convx += *(x + n - m) * *(h + m);

Why are you using obfuscatory pointer arithmetic *(a + b) syntax
instead of clarifying and normal array access a syntax?
So does that mean inside convolve.c I need to convert it to float or
something first?
But then it seems like using void is totally unnecessary? Can you show what
I need to do inside convolve.c?

I am confused about your actual situation. You claimed that
(presumably a pointer to) any type is expected by convolve() and
that therefore you were using void * for the parameter type. Now
you're claiming that you want to do arithmetic on the values
pointed to. These two are close to mutually exclusive: you can't
do arithmetic on a value unless you know its type, and if you
know its type then you might as well pass the proper pointer type
instead of void *.
 
B

Ben Pfaff

Pushker Pradhan said:
Here's the use in convolve:
void convolve(void *x, float *convx, uint32 numrowsx, uint32 numcolsx, float
*h,
uint32 hlen, dwt_process ROWSORCOLS)

Use:
*convx += *(x + n - m) * *(h + m);
*(convx + n*numcolsx + c) += *(x + (n - m)*(numcolsx) + c) * *(h + m);

Since this doesn't compile I cast the pointer to float like this:
*convx += *((float*)x + n - m) * *(h + m);
*(convx + n*numcolsx + c) += *((float*)x + (n - m)*(numcolsx) + c) * *(h +
m);

This compiles but I don't know whether it will work.

"x" can be unsigned char or float or unsigned 16 bit (it's a image data).
But the above mentioned computation cannot be done between an unsigned
char/int and floats, right? So I just cast x to float,

Casting x to `float *', then dereferencing, will only work if x
actually points to floats. If x always points to floats, then
you should just declare it as `float *' instead of as `void *'.
 
T

The Real OS/2 Guy

I've a function which accepts void * as the arg because the actual datatype
can be char or float or anything.
The fuction:
void convolve(void *x, float *convx, uint32 numrowsx, uint32 numcolsx, float
*h,
uint32 hlen, dwt_process ROWSORCOLS)

Usage in main()
convolve((float*)x, xLP, tileLength, tileWidth, ld, filterlen, DWT_ROWS);

However I get these compile errors
Redstone[93] pushkar$ gcc -g -o main dwt.c convolve.c dnsample.c
getWaveletCoeffs.c -ltiff -lm
convolve.c: In function `convolve':
convolve.c:46: warning: dereferencing `void *' pointer
convolve.c:46: void value not ignored as it ought to be
convolve.c:62: warning: dereferencing `void *' pointer
convolve.c:62: void value not ignored as it ought to be

Can anyone tell what's wrong with my code? Thanks,

You tries to dereference a pointer to void. That is an impossible
mission.
void is an incomplete data type (no known size).
void * is a pointer to a sizeless data type, so it can't be
dereferenced.
void * works as container for a pointer of any real type.
tell the compiler what realy data type you means
when you needs access the data itself - but be sure
you're select the one the data is really to avoid
undefined behavior.
 
M

MikeyD

This compiles but I don't know whether it will work.
"x" can be unsigned char or float or unsigned 16 bit (it's a image data).
But the above mentioned computation cannot be done between an unsigned
char/int and floats, right? So I just cast x to float,
Please tell me if this is okay.

You say it's image data, what do you mean by this? Is it just a sixteen bit
piece of data without a real c type? In that case just make x be a pointer
to any 16-bit data type, then call the function with whatever. You may get a
"mismatched pointer conversion warning" on compile, but if you know it's 16
bit and what all the bits mean, just ignore it.
 

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