Pointer Arithmetics and void *

T

Terence

I need some clarification with pointer arithmetics on void *.

Example 1:
========
char s[100];
char *ptr = s;
ptr += 1;
// I assume ptr is increased by 1 byte, pointing to the 2nd element in the array, right?

Example 2:
=======
int i[100]; // suppose the size of an int is 4 bytes
int *ptr = i;
ptr += 1; // so is ptr increased by 4 bytes in here?

Example 3:
=======
int i[100];
void *ptr = i;
ptr += 1; // So how many bytes is the ptr increased by here?
// Will it point to the 2nd element of the array.

If I know the byte-size of elements in the array, but I don't know the actual type.
How can I make the void * pointer points to the next element of the array?


Thanks,

Terence
 
A

Artie Gold

Terence said:
I need some clarification with pointer arithmetics on void *.

Example 1:
========
char s[100];
char *ptr = s;
ptr += 1;
// I assume ptr is increased by 1 byte, pointing to the 2nd element in the array, right?

You are correct.
Example 2:
=======
int i[100]; // suppose the size of an int is 4 bytes
int *ptr = i;
ptr += 1; // so is ptr increased by 4 bytes in here?

Again, you are correct.
Example 3:
=======
int i[100];
void *ptr = i;
ptr += 1; // So how many bytes is the ptr increased by here?
// Will it point to the 2nd element of the array.

Sorry, pointer arithmetic on void * is undefined.
If I know the byte-size of elements in the array, but I don't know the actual type.
How can I make the void * pointer points to the next element of the array?

You'll have to cast to and from char * to do this.

HTH,
--ag
 
J

Joona I Palaste

Terence <[email protected]> scribbled the following
I need some clarification with pointer arithmetics on void *.
Example 1:
========
char s[100];
char *ptr = s;
ptr += 1;
// I assume ptr is increased by 1 byte, pointing to the 2nd element in the array, right?
Yes.

Example 2:
=======
int i[100]; // suppose the size of an int is 4 bytes
int *ptr = i;
ptr += 1; // so is ptr increased by 4 bytes in here?

It's increased by sizeof(int) bytes. With your supposition, your
hypothesis is correct.
Example 3:
=======
int i[100];
void *ptr = i;
ptr += 1; // So how many bytes is the ptr increased by here?
// Will it point to the 2nd element of the array.

This code won't work. You cannot perform pointer arithmetics on
unadorned void pointers. You won't get this past the compiler.
If I know the byte-size of elements in the array, but I don't know the actual type.
How can I make the void * pointer points to the next element of the array?

You can't, unless you assign it to a variable of a concrete pointer
type, and increment that variable instead.
 
V

Victor Bazarov

Terence said:
I need some clarification with pointer arithmetics on void *.

Example 1:
========
char s[100];
char *ptr = s;
ptr += 1;
// I assume ptr is increased by 1 byte, pointing to the 2nd element in the
array, right?

Right
Example 2:
=======
int i[100]; // suppose the size of an int is 4 bytes
int *ptr = i;
ptr += 1; // so is ptr increased by 4 bytes in here?

Yes, and it points to the second element of the array, as well.
Example 3:
=======
int i[100];
void *ptr = i;
ptr += 1; // So how many bytes is the ptr increased by here?

+= cannot be used with a void*. The pointer is required to be
to a completely defined object type. 'void' is not an object
type.
// Will it point to the 2nd element of the array.

The program is ill-formed, it won't compile.
If I know the byte-size of elements in the array, but I don't know the actual type.
How can I make the void * pointer points to the next element of the array?

You cannot. Use char*.

Victor
 
A

Andrey Tarasevich

Terence said:
I need some clarification with pointer arithmetics on void *.

Example 1:
========
char s[100];
char *ptr = s;
ptr += 1;
// I assume ptr is increased by 1 byte, pointing to the 2nd element in the array, right?
Yes.

Example 2:
=======
int i[100]; // suppose the size of an int is 4 bytes
int *ptr = i;
ptr += 1; // so is ptr increased by 4 bytes in here?
Yes.

Example 3:
=======
int i[100];
void *ptr = i;
ptr += 1; // So how many bytes is the ptr increased by here?
// Will it point to the 2nd element of the array.

No. This code will not even compile. Operator '+=' cannot be applied to
pointer of type 'void*'.
If I know the byte-size of elements in the array, but I don't know the actual type.
How can I make the void * pointer points to the next element of the array?

ptr = (unsigned char*) ptr + byte_size;
 
E

Eric Sosman

Terence said:
I need some clarification with pointer arithmetics on void *.

Example 1:
========
char s[100];
char *ptr = s;
ptr += 1;
// I assume ptr is increased by 1 byte, pointing to the 2nd element in the array, right?
Yes.

Example 2:
=======
int i[100]; // suppose the size of an int is 4 bytes
int *ptr = i;
ptr += 1; // so is ptr increased by 4 bytes in here?
Yes.

Example 3:
=======
int i[100];
void *ptr = i;
ptr += 1; // So how many bytes is the ptr increased by here?
// Will it point to the 2nd element of the array.

No; arithmetic is not permitted on pointers to "incomplete
types," and `void' is an incomplete type. The reason is easy
to see: As you've noticed above, incrementing a pointer moves
it forward past all the bytes of the pointed-to object so it
now points to the next object (if such exists). But you don't
know the size of an incomplete object, so you don't know how
many bytes to skip over.

gcc (and perhaps other compilers) permit arithmetic on
`void*', more or less by pretending that it's equivalent to
`char*' -- but this is a non-conforming extension to the C
language, and even gcc will reject it if invoked in a
Standard-conforming mode.
If I know the byte-size of elements in the array, but I don't know the actual type.
How can I make the void * pointer points to the next element of the array?

ptr = (char*)ptr + sizeof(int);
ptr = (int*)ptr + 1;
ptr = &i[1];
ptr = i + 1;
...
 
M

Martin Ambuhl

Terence said:
I need some clarification with pointer arithmetics on void *.

There is no such thing. The size of a 'void' is unknowable (since there is
no such thing), so no pointer arithmetic makes sense on a 'void *'.

[Examples snipped, since the context is nonsense]
 

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
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top