Hello everyone,
I am writing a function that takes a void* as an argument. In this
function, there is some data that needs to be given back in the form
of a void pointer. This is the simple test that I am trying to do:
////////////////////////CODE/////////////////////////////////
#include <stdio.h>
long f(int i, void *b){
float*fp;
char* a = "Hello";
float*tp = (float *)a;
printf("tp = %p\tb = %p\n", tp, b);
b = tp;
printf("tp = %p\tb = %p\n", tp, b);
return 0;
}
int main() {
void* pb;
printf("pb = %p\n", pb);
f(2, pb);
printf("pb = %p\n", pb);
return 0;
}
///////////////////////////END CODE///////////////////////
///////////////////////////OUTPUT//////////////////////////
pb = CCCCCCCC
tp = 00422030 b = CCCCCCCC
tp = 00422030 b = 00422030
pb = CCCCCCCC
///////////////////////////END OUTPUT//////////////////////
But what I want is that inside the function that I call, the void
pointer should be redirected, i.e. pb should be equal to 00422030, pb
= b.
(I know the above code is weird but I want to illustrate the fact that
I am trying to make the void* point to another place).
I think that this should be possible and the void pointer should point
to the address that I want to in the function.
But this is not happenning. Please help.
Thanks for everyone who replies.
With best regards,
Mark Antony 4 1925
"Mark Antony" <m_********@yahoo.com> wrote in message
news:5a**************************@posting.google.c om... Hello everyone,
I am writing a function that takes a void* as an argument. In this function, there is some data that needs to be given back in the form of a void pointer. This is the simple test that I am trying to do:
////////////////////////CODE/////////////////////////////////
#include <stdio.h>
long f(int i, void *b){ float*fp; char* a = "Hello"; float*tp = (float *)a;
printf("tp = %p\tb = %p\n", tp, b); b = tp; printf("tp = %p\tb = %p\n", tp, b);
return 0; }
int main() { void* pb;
printf("pb = %p\n", pb); f(2, pb); printf("pb = %p\n", pb);
return 0; }
///////////////////////////END CODE/////////////////////// ///////////////////////////OUTPUT////////////////////////// pb = CCCCCCCC tp = 00422030 b = CCCCCCCC tp = 00422030 b = 00422030 pb = CCCCCCCC ///////////////////////////END OUTPUT//////////////////////
But what I want is that inside the function that I call, the void pointer should be redirected, i.e. pb should be equal to 00422030, pb = b.
(I know the above code is weird but I want to illustrate the fact that I am trying to make the void* point to another place).
Function f() is only altering the value of its local copy of the pb
variable, not the value of the variable itself, which is what you're after.
Parameters that are to have their values changed by a function should be
passed "by reference" - i.e. pass the address of the parameter, not its
value, and should then be dereferenced inside the function.
To do that, change the "void *b" in the declaration of f() to "void **b",
and change each use of the variable b in f()'s definition to *b, e.g. change
"b = tp" to "*b = tp".
Then each call to f() needs to pass the address of the pointer to be
changed, e.g. in main() change "f(2, pb)" to "f(2, &pb)".
Cheers,
Todd
"Mark Antony" <m_********@yahoo.com> wrote in message
news:5a**************************@posting.google.c om... Hello everyone,
I am writing a function that takes a void* as an argument. In this function, there is some data that needs to be given back in the form of a void pointer. This is the simple test that I am trying to do:
////////////////////////CODE/////////////////////////////////
#include <stdio.h>
long f(int i, void *b){ float*fp;
Why don't use long* here ?
char* a = "Hello"; float*tp = (float *)a;
printf("tp = %p\tb = %p\n", tp, b); b = tp;
Here b is the value contained in pointer "void* b". If you want to change the pointer "void* b"
itself, you have to use "pointer of pointer".
void **b
printf("tp = %p\tb = %p\n", tp, b);
return 0; }
int main() { void* pb;
printf("pb = %p\n", pb); f(2, pb); printf("pb = %p\n", pb);
return 0; }
///////////////////////////END CODE/////////////////////// ///////////////////////////OUTPUT////////////////////////// pb = CCCCCCCC tp = 00422030 b = CCCCCCCC tp = 00422030 b = 00422030 pb = CCCCCCCC ///////////////////////////END OUTPUT//////////////////////
But what I want is that inside the function that I call, the void pointer should be redirected, i.e. pb should be equal to 00422030, pb = b.
(I know the above code is weird but I want to illustrate the fact that I am trying to make the void* point to another place).
I think that this should be possible and the void pointer should point to the address that I want to in the function.
But this is not happenning. Please help.
Thanks for everyone who replies.
With best regards, Mark Antony
--
Jeff
Thanks to Jeff and Todd for their reply.
What you told me is correct. But I want to do something like this:
//////////////////////////////////////CODE///////////////////////////
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
long f(int a, void* b) {
float* c;
char* df1;
long q = 123456;
long* df2 = &q;
*b = q;
return 0;
}
int main() {
long w;
f(2, &w);
printf("w = %u\n", w);
return 0;
}
/////////////////////////////////END CODE///////////////////////////
I know that this does not work but what I want to do is to write the
f() function in such a way the pointer returns whatever value it is
asked and the function declaration cannot change. The output should be
"w = 123456". But this is not the case.
I searched the newsgroups for examples with void* in their arguments
and I found this. I have tried this and it works:
//////////////////////////////////////////CODE//////////////////////
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
long f(int a, void* b) {
float* c;
char* df1;
long q = 123456;
long* df2 = &q;
*(long*)b = q;
return 0;
}
int main() {
long w;
f(2, &w);
printf("w = %u\n", w);
return 0;
}
///////////////////////////////////////////END
CODE///////////////////////////
The output is what I desire: "w = 1234656". But this means that the
function will have to have as one of its arguments a parameter
specifying what the datatype should be. But I cannot change the
function declaration and yet the void* should point to the right data.
Please help.
With best regards,
Mark Antony m_********@yahoo.com (Mark Antony) wrote in message news:<5a**************************@posting.google. com>... Hello everyone,
I am writing a function that takes a void* as an argument. In this function, there is some data that needs to be given back in the form of a void pointer. This is the simple test that I am trying to do:
////////////////////////CODE/////////////////////////////////
#include <stdio.h>
long f(int i, void *b){ float*fp; char* a = "Hello"; float*tp = (float *)a;
printf("tp = %p\tb = %p\n", tp, b); b = tp; printf("tp = %p\tb = %p\n", tp, b);
return 0; }
int main() { void* pb;
printf("pb = %p\n", pb); f(2, pb); printf("pb = %p\n", pb);
return 0; }
///////////////////////////END CODE/////////////////////// ///////////////////////////OUTPUT////////////////////////// pb = CCCCCCCC tp = 00422030 b = CCCCCCCC tp = 00422030 b = 00422030 pb = CCCCCCCC ///////////////////////////END OUTPUT//////////////////////
But what I want is that inside the function that I call, the void pointer should be redirected, i.e. pb should be equal to 00422030, pb = b.
(I know the above code is weird but I want to illustrate the fact that I am trying to make the void* point to another place).
I think that this should be possible and the void pointer should point to the address that I want to in the function.
But this is not happenning. Please help.
Thanks for everyone who replies.
With best regards, Mark Antony
On Sat, 9 Aug 2003, Mark Antony wrote: Thanks to Jeff and Todd for their reply. What you told me is correct. But I want to do something like this:
long f(int a, void* b) { float* c; char* df1; long q = 123456; long* df2 = &q;
It's not really clear what all these variables are doing here.
*b = q;
A simple
*(long *)b = 123456L;
would do. return 0; }
int main() { long w;
f(2, &w);
Okay, presumably you're going to expand the above function
so that the first ('int') argument specifies the "real type"
of 'b', and you assign something different to it depending
on the value of the argument.
printf("w = %u\n", w);
Nit: %u should be %ld there, because 'w' is a 'long'. return 0; }
I know that this does not work but what I want to do is to write the f() function in such a way the pointer returns whatever value it is asked and the function declaration cannot change. The output should be "w = 123456". But this is not the case.
With the changes '*b'->'*(long*)b' and '%u'->'%ld', it should be
the case. ...And you go on to say that it is. So, good.
[snip slightly-more-correct code]
The output is what I desire: "w = 1234656". But this means that the function will have to have as one of its arguments a parameter specifying what the datatype should be. But I cannot change the function declaration and yet the void* should point to the right data.
Please help.
Well, that's *the* way to do it. You can't use the parameter unless
you know what type it is, and you can't know what type it is unless
you let yourself know. Which means passing information to the
function. Which means:
1) Use a function parameter (recommended).
enum FOO_TYPE { FOO_TYPE_LONG, FOO_TYPE_CPTR, FOO_TYPE_INT };
void foo (enum FOO_TYPE what_type, void *param);
foo(FOO_TYPE_CPTR, "bar");
foo(FOO_TYPE_LONG, &mylong);
2) Use a global variable (icky but probably what you asked for).
static enum FOO_TYPE GlobalFooParamIcky;
void foo (void *param);
GlobalFooParamIcky = FOO_TYPE_CPTR;
foo("bar");
GlobalFooParamIcky = FOO_TYPE_LONG;
foo(&mylong);
3) Use a cutesy method (not recommended unless you are
otherwise seriously considering #2).
void *FOO_TYPE_CPTR = &FOO_TYPE_CPTR;
void *FOO_TYPE_LONG = &FOO_TYPE_LONG;
void *FOO_TYPE_INT = &FOO_TYPE_INT;
void foo (void *param)
{
static int step = 0;
static void *type = NULL;
if (param == NULL) {
step = 1;
return;
}
if (step == 1) {
type = param;
step = 0;
return;
}
if (type == FOO_TYPE_CPTR) {
printf("%s\n", param);
}
else if (type == FOO_TYPE_LONG) {
*(long*)param = 123456L;
}
}
foo(NULL); foo(FOO_TYPE_CPTR); foo("bar");
foo(NULL); foo(FOO_TYPE_LONG); foo(&mylong);
Those are your options. As far as I can tell, they're
basically your *only* options (although alternative
methods are welcome).
I hope one of these helps you. m_********@yahoo.com (Mark Antony) wrote in message news:<5a**************************@posting.google. com>... Hello everyone,
A: Yes.
Q: Even if I'm only quoting myself?
A: Because it's hard to read.
Q: Why do you say not to top-post?
HTH,
-Arthur This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: BrianJones |
last post by:
Hi, if you have a function, how is it possible to return an array? E.g.:
unsigned long function(...) // what I want to do, obviously illegal
I do know such would be possible by using a dynamic...
|
by: Siemel Naran |
last post by:
Hi. I have found one advantage of returning values through the argument
list. It's that we have to store the return value. But when we return by
value, we may forgot to store the return value.
...
|
by: Materialised |
last post by:
I am writing a simple function to initialise 3 variables to pesudo
random numbers.
I have a function which is as follows
int randomise( int x, int y, intz)
{
srand((unsigned)time(NULL));
x...
|
by: Dawn Minnis |
last post by:
Hey guys - this code when called with parameters:
driver.o n n 12 12 12 12 12 12 2.6 3.2
is kicking back a segmentation fault.
I've read the rest of the postings but am still confused. Can...
|
by: Guha |
last post by:
I have a problem with returning a 2D array using a function which is
called in main(). The piece of the code is given below. This is a test
code only.
#include"stdio.h"
#include"alloc.h"
...
| |
by: CptDondo |
last post by:
I am working on an embedded platform which has a block of battery-backed
RAM. I need to store various types of data in this block of memory -
for example, bitmapped data for control registers,...
|
by: jza |
last post by:
Hello all,
I am fairly new to c, coming from a Java background. I am working on a mathematical program and I have a function that needs to return a 2-d array. After some web searching, I have...
|
by: Pradyut Bhattacharya |
last post by:
Hi,
in this program i cannot retrive the values from the pointer j....
The code :-
--------------------------------------------------------------------------------------------------------------...
|
by: TBass |
last post by:
Hi.
I wrote a "tag" class to store values. The user gets to store most any
type he would need. Instead of getting too complicatated, I decided I
would store the value as a stringstream, then...
|
by: ctj951 |
last post by:
I have a very specific question about a language issue that I was
hoping to get an answer to. If you allocate a structure that
contains
an array as a local variable inside a function and return...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |