445,852 Members | 2,219 Online Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

# Pointer Arithmetics and void *

 P: n/a I need some clarification with pointer arithmetics on void *. Example 1: ======== char s; 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; // 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; 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 Nov 13 '05 #1
6 Replies

 P: n/a Terence scribbled the following on comp.lang.c: I need some clarification with pointer arithmetics on void *. Example 1: ======== char s; 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; // 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; 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. -- /-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "The truth is out there, man! Way out there!" - Professor Ashfield Nov 13 '05 #2

 P: n/a Terence wrote: I need some clarification with pointer arithmetics on void *. Example 1: ======== char s; 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; // 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; 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; -- Best regards, Andrey Tarasevich Nov 13 '05 #3

 P: n/a "Terence" wrote... I need some clarification with pointer arithmetics on void *. Example 1: ======== char s; 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; // 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; 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 Nov 13 '05 #4

 P: n/a Terence wrote: I need some clarification with pointer arithmetics on void *. Example 1: ======== char s; 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; // 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; 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 -- Artie Gold -- Austin, Texas Oh, for the good old days of regular old SPAM. Nov 13 '05 #5

 P: n/a Terence wrote: I need some clarification with pointer arithmetics on void *. Example 1: ======== char s; 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; // 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; 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; ptr = i + 1; ... -- Er*********@sun.com Nov 13 '05 #6

 P: n/a Terence wrote: 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] -- Martin Ambuhl Nov 13 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion. 