void pointer

H

harshal

Hi all,
i have 2 structures say struct a, and struct b

i am in a function now in this function i want to store these 2
pointers
in global memory and store that address in void pointer.
void abc(void *temp) {
struct a * aptr;
struct b * bptr;

// get these pointers filed from temp
aptr = (struct a *) temp;
temp = temp + (sizeof(struct a *));
bptr = temp;
// i am confused about this.
}

void collect(struct collector *e) {
struct a *aptr = NULL;
struct b *bptr = NULL;
void * temp = NULL;
// suppose these two pointers are filled with appropriate values by
iterating a list aptr and bptr
// are ok now
temp = malloc(2 * sizeof(void *));
temp = aptr;
temp = temp + sizeof(struct a *);
temp = bptr;
//then decrement temp again to get original value
temp = temp - sizeof(struct a *);
//and pass this pointer to other function i dont want to send them
as an parameter
abc(temp);
}

is this right??
i forgot everything about void pointers but i know that we can not do
arithmetic operations on void ptr and if we do it will be like char
ptr and we can not dereference it also.
and please guide me through how abc will collect these two pointers in
local variables also

Thanks
 
M

Mark Bluemel

harshal said:
Hi all,
i have 2 structures say struct a, and struct b

void collect(struct collector *e) {
struct a *aptr = NULL;
struct b *bptr = NULL;
void * temp = NULL;
// suppose these two pointers are filled with appropriate values by
iterating a list aptr and bptr

That illustrates why you shouldn't use '//' comments in newsgroup
postings.
// are ok now
temp = malloc(2 * sizeof(void *));
temp = aptr;

Surely you mean "*temp = aptr;" ?
temp = temp + sizeof(struct a *);
temp = bptr;

and again "*temp = bptr;"
//then decrement temp again to get original value
temp = temp - sizeof(struct a *);
//and pass this pointer to other function i dont want to send them
as an parameter
abc(temp);
}

You don't half make things more complicated than they need to be...

What's wrong with :-

void abc(void *list_of_pointers[]) {
struct a * aptr = list_of_pointers[0];
struct b * bptr = list_of_po;
/* use the pointers ... */
}

void collect(struct collector *e) {
struct a *aptr = NULL;
struct b *bptr = NULL;
/* suppose these two pointers are filled
* with appropriate values by iterating
* a list, so aptr and bptr are ok now
*/
void * temp = malloc(2 * sizeof(void *));
temp[0] = aptr;
temp[1] = bptr;
abc(temp);
}
 
M

Mark Bluemel

Mark said:
harshal said:
Hi all,
i have 2 structures say struct a, and struct b

void collect(struct collector *e) {
struct a *aptr = NULL;
struct b *bptr = NULL;
void * temp = NULL;
// suppose these two pointers are filled with appropriate values by
iterating a list aptr and bptr

That illustrates why you shouldn't use '//' comments in newsgroup
postings.
// are ok now
temp = malloc(2 * sizeof(void *));
temp = aptr;

Surely you mean "*temp = aptr;" ?
temp = temp + sizeof(struct a *);
temp = bptr;

and again "*temp = bptr;"
//then decrement temp again to get original value
temp = temp - sizeof(struct a *);
//and pass this pointer to other function i dont want to send them
as an parameter
abc(temp);
}

You don't half make things more complicated than they need to be...

What's wrong with :-

void abc(void *list_of_pointers[]) {
struct a * aptr = list_of_pointers[0]; [Corrected below]
struct b * bptr = list_of_pointers[1];
/* use the pointers ... */
}

void collect(struct collector *e) {
struct a *aptr = NULL;
struct b *bptr = NULL;
/* suppose these two pointers are filled
* with appropriate values by iterating
* a list, so aptr and bptr are ok now
*/ [Corrected below]
void ** temp = malloc(2 * sizeof(void *));
temp[0] = aptr;
temp[1] = bptr;
abc(temp);
}
 
A

Army1987

harshal said:
Hi all,
i have 2 structures say struct a, and struct b

i am in a function now in this function i want to store these 2
pointers
in global memory and store that address in void pointer.
void abc(void *temp) {
struct a * aptr;
struct b * bptr;

// get these pointers filed from temp
aptr = (struct a *) temp;
temp = temp + (sizeof(struct a *));
bptr = temp;
aptr and bptr are automatic variables of abc, and will die as soon as it
returns. That is not what you wanted to do, I guess.
// i am confused about this.
}

void collect(struct collector *e) {
struct a *aptr = NULL;
struct b *bptr = NULL;
void * temp = NULL;
// suppose these two pointers are filled with appropriate values by
iterating a list aptr and bptr
Long lines are likely to be wrapped in Usenet, breaking // comments. Use
them only on very short lines (or better, don't use them at all, for
compatibility with C89 which didn't have them).
// are ok now
temp = malloc(2 * sizeof(void *));
temp = aptr;
This immediately throws away the memory you allocated.
temp = temp + sizeof(struct a *);
temp = bptr;
If you wanted to store two pointers in temp, just declare it as void **, so
that you can do:
struct a *aptr;
struct b *bptr;
void **temp;
/* fill aptr and bptr */
temp = malloc(2 * sizeof *temp);
if (temp == NULL) {
/* memory allocation failed, do something */
}
temp[0] = aptr;
temp[1] = bptr;
//then decrement temp again to get original value temp = temp -
sizeof(struct a *);
//and pass this pointer to other function i dont want to send them
as an parameter
abc(temp);
}

is this right??
i forgot everything about void pointers but i know that we can not do
arithmetic operations on void ptr and if we do it will be like char ptr
and we can not dereference it also. and please guide me through how abc
will collect these two pointers in local variables also

declare abc as taking a void **:
void abc(void **temp)
{
struct a *aptr = temp[0];
struct b *bptr = temp[1];
/* do what you need with them */
}
 
K

Keith Thompson

Mark Bluemel said:
That illustrates why you shouldn't use '//' comments in newsgroup
postings.


Surely you mean "*temp = aptr;" ?

Surely not. temp is of type void*; what do you expect *temp to be?

[snip]
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top