473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

void arg to function

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,
--
Pushkar Pradhan
Nov 13 '05 #1
9 7468
"Pushker Pradhan" <pu*****@erc.msstate.edu> writes:
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[i]. 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.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
Nov 13 '05 #2
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" <bl*@cs.stanford.edu> wrote in message
news:87************@pfaff.stanford.edu...
"Pushker Pradhan" <pu*****@erc.msstate.edu> writes:
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[i]. 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.
--
"C has its problems, but a language designed from scratch would have some

too, and we know C's problems."
--Bjarne Stroustrup

Nov 13 '05 #3
"Pushker Pradhan" <pu*****@erc.msstate.edu> writes:
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[b] 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 *.
--
"The expression isn't unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop
Nov 13 '05 #4

"Pushker Pradhan" <pu*****@erc.msstate.edu> wrote in message
But then it seems like using void is totally unnecessary? Can you show > what I need to do inside convolve.c?
void convolve(void *x, float *convx, uint32 numrowsx, uint32
numcolsx, float *h, uint32 hlen, dwt_process ROWSORCOLS)

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.

Nov 13 '05 #5
>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
Nov 13 '05 #6
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" <bl*@cs.stanford.edu> wrote in message
news:87************@pfaff.stanford.edu...
"Pushker Pradhan" <pu*****@erc.msstate.edu> writes:
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[b] 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 *.
--
"The expression isn't unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop

Nov 13 '05 #7
"Pushker Pradhan" <pu*****@erc.msstate.edu> writes:
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 *'.
Nov 13 '05 #8
On Sat, 13 Sep 2003 04:28:05 UTC, "Pushker Pradhan"
<pu*****@erc.msstate.edu> wrote:
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.

--
Tschau/Bye
Herbert

eComStation 1.1 Deutsch Beta ist verügbar
Nov 13 '05 #9
> 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.
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
6
by: dam_fool_2003 | last post by:
Hai, I thank those who helped me to create a single linked list with int type. Now I wanted to try out for a void* type. Below is the code: #include<stdlib.h> #include<stdio.h>...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
5
by: Giannis Papadopoulos | last post by:
I have the following code #include <stdio.h> void a(void) { printf("a called.\n"); } int b(void) { printf("b called.\n");
7
by: Peter Oliphant | last post by:
I just found out the following code compiles: ref class ClassA { public: ClassA() { x = 0 ; X_To_1( ) ; void X_To_2( ) ; // ****
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
4
by: Andreas Klimas | last post by:
hello, no many words, I will start with an example instead int foo(void *r) {...} void test(void) { int *x=0; foo(x); /* seems to me as valid */ }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.