C/C++ on Linux/Unix

Moderators: Lundin
Number of threads: 336
Number of posts: 663

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
void *p problem; Posted by shizhihong on 20 Jan 2010 at 1:10 AM

hi, I have one question about void *p;

void *p;
struct block *bh;
struct disk *docs;

bh=(struct block *)p;
docs=(struct disk *) ( (unsigned long)p+sizeof(struct block));

my question is that which address p points to now?

Does p point to the address (p+sizeof(struct block)) ?

please help me,

Thanks a million


Report
Re: void *p problem; Posted by anthrax11 on 20 Jan 2010 at 11:59 AM
p doesn't point to anything, because it is uninitialized.
Maybe you have a typo somewhere?
Report
Re: void *p problem; Posted by shizhihong on 20 Jan 2010 at 12:51 PM

int A(void *p,struct word_cache *wc,int index)
{
struct block *bh;
struct disk *docs;

....
.....
....

bh=(struct block *)p; //cast the void * to struct block
docs=(struct disk *) ( (unsigned long)p+sizeof(struct block));
//calculate the memory location of the struct disk:
//then cast it to struct disk.

....
.....

}

we pass an parameter void *buf to the first parameter of A();
So, in this case does p point to different address (+sizeof(struct block))
or it points to the original address ?

please help me, thanks


Report
Re: void *p problem; Posted by anthrax11 on 23 Jan 2010 at 1:05 PM
Sorry, I still can't understand what you mean.

The value of p is what you pass to the A function.
p+sizeof(struct block) is a combined address that points to whatever is sizeof(struct block) bytes after what p points to, but this addition operation doesn't change the value of p itself.
Report
Re: void *p problem; Posted by shizhihong on 23 Jan 2010 at 4:50 PM
Thank you.
I've already understood it.

thanks a million



 

Recent Jobs