473,395 Members | 1,863 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,395 software developers and data experts.

assigning void * type value

Hello all,

I am having a little problem getting my (void *) assignment to work
correctly. Basically it is some code for a circular queue buffer I am
trying to implement, but getting the deferrencing of the (void *) type
is giving me problems... For brevity sake not all the functions are
defined, but the name gives a clue to what it does...
[------------Code Begins------------]
#define FAIL 0x01
#define SUCCESS 0x00
typedef unsigned long UINT32;

typedef struct {
void* data[CBQ_MAX_QUEUE_SIZE];
INT8 head,
tail;
} CIRCULAR_BUF_DESCRIPTOR;
STATUS_RESULT_TYPE PopCbq( CIRCULAR_BUF_DESCRIPTOR* QueueDescriptor,
void* objectRef )
{
UINT32 test;
UINT32* testRef;

// Check for valid arguments
if ( QueueDescriptor == NULLPTR || objectRef == NULLPTR ) { return
FAIL; }

// Check to make sure the queue isn't empty first.
if ( IsCbqEmpty(QueueDescriptor) ) { return FAIL; }

test = 0x5555;

testRef = &test;

*testRef = (void *)QueueDescriptor->data[QueueDescriptor->tail++];

// The following line is where I am having the problems... I can get
// the above line to work, but not this one... I am having
indirection
// problems, accoding to Visual C++ v6
*objectRef = (void)QueueDescriptor->data[QueueDescriptor->tail++];

// *objectRef = ((void *)test);

return SUCCESS;
}
[------------Code Ends------------]
So I am not sure what I am doing wrong.. I have tried various
declarations, but none of them work. What am I doing wrong or might be
a better approach? This function is suppose to take care or different
types of data which the application later needs to cast it to the
appropriate data type. Thanks for any help...

Mark

Nov 14 '05 #1
4 2614
Lord...@hotmail.com wrote:
Hello all,

I am having a little problem getting my (void *) assignment to work
correctly.
Don't cast the right operand of the assignment. The whole purpose
of void * is that you _don't_ need to cast it when assigning to
a compatible pointer type. e.g....

T *p = malloc(N * sizeof *p);
Basically it is some code for a circular queue buffer I am
trying to implement, but getting the deferrencing of the (void *) type is giving me problems... For brevity sake not all the functions are
defined, but the name gives a clue to what it does...
...
STATUS_RESULT_TYPE PopCbq( CIRCULAR_BUF_DESCRIPTOR* QueueDescriptor,
void* objectRef ) {
UINT32 test;
UINT32* testRef;

// Check for valid arguments
if ( QueueDescriptor == NULLPTR || objectRef == NULLPTR ) { return
FAIL; }

// Check to make sure the queue isn't empty first.
if ( IsCbqEmpty(QueueDescriptor) ) { return FAIL; }

test = 0x5555;

testRef = &test;

*testRef = (void *)QueueDescriptor->data[QueueDescriptor->tail++];
Since testRef is a pointer to an integer, *testRef is an integer
(lvalue). But you try to assign a void pointer, which is an
incompatible type, indeed the code is a constraint violation.

I'm surprise compilation doesn't stop at this line.
// The following line is where I am having the problems... I can get // the above line to work, but not this one... I am having
indirection
// problems, accoding to Visual C++ v6
*objectRef = (void)QueueDescriptor->data[QueueDescriptor->tail++];
You can't assign void objects.

// *objectRef = ((void *)test);

return SUCCESS;
}


--
Peter

Nov 14 '05 #2
Peter,

Thanks for the very quick reply, but I am still unclear what I need
to do in order to fix the code. After looking at the post I left
testing code that wasn't suppose to be include, I am very sorry for
this. All the code that uses the following

UINT32 test;
UINT32* testRef;

isn't part of my original problem with trying trying to pop data out
of array (void* data[CBQ_MAX_QUEUE_SIZE] ) and copy it into the input
argument (void* objectRef) of the function
PopCbq().

Since the project that I am working on is embedded there is no use of
dynamic allocation and everything is statically defined or locally
defined within a function. I am not sure if that helps any, but just
some back ground info.

Mark

Nov 14 '05 #3
On Mon, 25 Apr 2005 23:24:39 -0700, LordHog wrote:
Hello all,

I am having a little problem getting my (void *) assignment to work
correctly. Basically it is some code for a circular queue buffer I am
trying to implement, but getting the deferrencing of the (void *) type
is giving me problems... For brevity sake not all the functions are
defined, but the name gives a clue to what it does...
[------------Code Begins------------]
#define FAIL 0x01
#define SUCCESS 0x00
typedef unsigned long UINT32;

typedef struct {
void* data[CBQ_MAX_QUEUE_SIZE];
INT8 head,
tail;
} CIRCULAR_BUF_DESCRIPTOR;
STATUS_RESULT_TYPE PopCbq( CIRCULAR_BUF_DESCRIPTOR* QueueDescriptor,
void* objectRef )
You need to explain what it is this function is supposed to be doing, and
in particular what is the purpose of the objectRef argument
{
UINT32 test;
UINT32* testRef;

// Check for valid arguments
if ( QueueDescriptor == NULLPTR || objectRef == NULLPTR ) { return
FAIL; }

// Check to make sure the queue isn't empty first. if (
IsCbqEmpty(QueueDescriptor) ) { return FAIL; }

test = 0x5555;

testRef = &test;

*testRef = (void *)QueueDescriptor->data[QueueDescriptor->tail++];

// The following line is where I am having the problems... I can get
// the above line to work, but not this one... I am having
indirection
// problems, accoding to Visual C++ v6 *objectRef =
(void)QueueDescriptor->data[QueueDescriptor->tail++];

// *objectRef = ((void *)test);
It appears that you are trying to return in some way an entry that was in
the datastructure indicated by QueueDescriptor. There are various ways you
can go about this, e.g. return a pointer to the object in the
datastructure or make a copy of the object.

If you want to return a pointer then do that, the easiest way is to make
the return type void * and use some other nethod to return a result code.
You may just be able to return NULL to indicate failure. It is then the
responsibility of the caller to convert the void * pointer to a pointer of
the appropriate type and then use it. You could pass an argument of type
void ** and write a void * value through that, but it is again the
responsibility of the caller to convert the void * value to the correct
type.

If you want to make a copy of the object then you can have the caller
define the destination object and pass a pointer to that for objectRef. It
will also need to pass the size of the object as another argument because
PopCbq just has a void * pointer i.e. it knows nothing about the object
other than its start address in memory. So if you had

STATUS_RESULT_TYPE PopCbq( CIRCULAR_BUF_DESCRIPTOR* QueueDescriptor,
void* objectRef
size_t objectSize)

you might have something like

memcpy(objectRef, QueueDescriptor->data[QueueDescriptor->tail++],
objectsize);

return SUCCESS;
}
[------------Code Ends------------]
So I am not sure what I am doing wrong.. I have tried various
declarations, but none of them work. What am I doing wrong or might be a
better approach? This function is suppose to take care or different
types of data which the application later needs to cast it to the
appropriate data type. Thanks for any help...


You first need to be clear about what it is you are trying to do. And be
aware that there is no polymorphism in C, you can't access an object
through a void * pointer to it, you have to convert the pointer back to an
appropriate type in the code first. You CAN treat any object as an array
of unsigned char if you know its size which is in effect what the memcpy()
call above does.

Lawrence

Nov 14 '05 #4
Lo*****@hotmail.com wrote:

... my original problem with trying trying to pop data out
of array (void* data[CBQ_MAX_QUEUE_SIZE] ) and copy it into the input argument (void* objectRef) of the function
PopCbq().


C passes parameters by value. You are passing 'objectRef' as a
parameter to a function. This means that the calling function must
pass in a valid pointer to something, and the caller cannot see
any changes that the function makes to 'objectRef'.

If you are trying to return a value via a parameter, then the
parameter needs to be a pointer to what you are trying to
return.

Here's a simple example:
void get_int(int *p) { *p = 5; }
void foo(void) { int x; get_int(&x); }

Now to repeat the example but with (void *) as the type instead
of int:

STATUS_RESULT_TYPE PopCbq(
CIRCULAR_BUF_DESCRIPTOR *QueueDescriptor,
void **objectRef )
{
*objectRef = QueueDescriptor->data[QueueDescriptor->tail++];
return 0;
}

void foo()
{
CIRCULAR_BUF_DESCRIPTOR bar;
...............
void *ptr;
PopCbq(&bar, &ptr);
}

You can then go on to use 'ptr' for whatever purpose (including
converting it back to point to the type it was pointing to
originally).

Nov 14 '05 #5

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

Similar topics

1
by: Jenny | last post by:
Hi, Can I create an array of tags by assigning same name to these tags? For example, I have two <p> tags with the same name t1. But document.all.b.value=document.all.t.length does not...
1
by: Antonio D'Ottavio | last post by:
Good morning, I've a problem with a dropdownlist located inside any row of a datalist, I fill both datalist and dropdownlist at runtime, the problem is with the dropdownlist infact using the event...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
25
by: Sourav | last post by:
Suppose I have a code like this, #include <stdio.h> int *p; void foo(int); int main(void){ foo(3); printf("%p %d\n",p,*p);
10
by: Steve Pope | last post by:
The first of the following functions compiles, the second gives what I think is a spurious error: "cannot convert `const char' to `char *' in assignment". void foo(int m) { char *str; if (m...
2
by: askcq | last post by:
main() { void *poin; unsigned int value; value =1000; printf("%u\n",value); *(unsigned int *)poin=(unsigned int)value; printf("%u\n",*poin); }
9
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int...
3
by: Froefel | last post by:
Hi group I am creating a web application that uses a simple DAL as an ObjectDataSource. To retrieve data from the database, I use a DataReader object from which I then assign the various fields...
43
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.