pointer casting question

S

sinbad

is there anything wrong with the following program. specifically
i am asking about casting pointer to an integer pointer (double
pointer)
to a void pointer and casting it back to double pointer.

1 void func(void *x)
2 {
3 int *y;
4 y = (*(int **)x);
5 y++;
6 *((int**)x) = y;
7
8 return;
9 }
10
11 int main(void)
12 {
13 int z = 10;
14 int *y = &z;
15
16 func(&y);
17
18 return 0;
19 }
~
 
V

viza

Hi

is there anything wrong with the following program. specifically i am
asking about casting pointer to an integer pointer (double pointer)
to a void pointer and casting it back to double pointer.

Please just say pointer pointer. Calling it a double pointer will make
people think you mean a pointer to a double precision floating point
number, (double*).
1 void func(void *x)
2 {
3 int *y;
4 y = (*(int **)x);
5 y++;
6 *((int**)x) = y;
7
8 return;
9 }

That is fine. void* can hold an int**.

The line numbers are a nuisance though, imho.

viza
 
J

James Kuyper

sinbad said:
is there anything wrong with the following program. specifically
i am asking about casting pointer to an integer pointer (double
pointer)
to a void pointer and casting it back to double pointer.

1 void func(void *x)
2 {
3 int *y;
4 y = (*(int **)x);
5 y++;
6 *((int**)x) = y;

Lines 3-6 could be replaced by:

(*(int**)x)++;

I presume that this is a simplified version of something that will be
much more complicated, in which line 5 would be replaced by something a
lot more complicated. In that case lines 3, 4, and 6 are all quite
reasonable.
7
8 return;
9 }
10
11 int main(void)
12 {
13 int z = 10;
14 int *y = &z;
15
16 func(&y);
17
18 return 0;
19 }
~

This looks good to me; but I often miss minor details that could make a
big difference.
 
A

Andrey Tarasevich

sinbad said:
is there anything wrong with the following program. specifically
i am asking about casting pointer to an integer pointer (double
pointer)
to a void pointer and casting it back to double pointer.

1 void func(void *x)
2 {
3 int *y;
4 y = (*(int **)x);
5 y++;
6 *((int**)x) = y;
7
8 return;
9 }
10
11 int main(void)
12 {
13 int z = 10;
14 int *y = &z;
15
16 func(&y);
17
18 return 0;
19 }


There's nothing formally wrong with this code.

The way you do a simple increment inside 'func' looks a bit weird to my
taste though. You could just do

++*(int **) x;

or, for better readability,

int **pp = x;
++*pp;

The second approach requires no explicit cast, although some compilers
might issue a warning, which can usually be suppressed with an explicit cast

int **pp = (int **) x;
++*pp;
 
C

CBFalconer

sinbad said:
is there anything wrong with the following program. specifically
i am asking about casting pointer to an integer pointer (double
pointer) to a void pointer and casting it back to double pointer.

1 void func(void *x)
2 {
3 int *y;
4 y = (*(int **)x);
5 y++;
6 *((int**)x) = y;
7
8 return;
9 }
10
11 int main(void)
12 {
13 int z = 10;
14 int *y = &z;
15
16 func(&y);
17
18 return 0;
19 }

I see no doubles. However, a working rule for casts is "don't". So:

void func(void *x) {
int *y;

y = x;
y++;
x = y;
} /* can eliminate return for a void function */

because the compiler knows how to convert a void* to a something*,
and a something* to a void*.
 
A

Andrey Tarasevich

CBFalconer said:
I see no doubles. However, a working rule for casts is "don't". So:

void func(void *x) {
int *y;

y = x;
y++;
x = y;
} /* can eliminate return for a void function */

because the compiler knows how to convert a void* to a something*,
and a something* to a void*.

Good advice, but bad code. Your code only modifies an automatic variable
- the value of the parameter 'x', which, of course, will have absolutely
no effect on the caller. This was not the OP's intent.
 
C

CBFalconer

Andrey said:
Good advice, but bad code. Your code only modifies an automatic
variable - the value of the parameter 'x', which, of course, will
have absolutely no effect on the caller. This was not the OP's
intent.

Well, that is one advantage of c.l.c. :) Bad code tends to get
corrected immediately. Thanks.

The way to avoid such foolish errors is to return the value wanted
with the function.

void *func(void *x) {
int *y = x;

y++;
return y;
}
 
O

ozbear

Good advice, but bad code. Your code only modifies an automatic variable
- the value of the parameter 'x', which, of course, will have absolutely
no effect on the caller. This was not the OP's intent.

I am uncertain about what the OP's intent actually was/is.
If it were to ultimately modify /z/ in the main program from
10 to 11 then the corrected versions don't do that.

Oz
 

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,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top