473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Pointer Arithmetics and void *

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
Nov 13 '05 #1
6 4153
Terence <fi****@hotmail.com> scribbled the following
on comp.lang.c:
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.

--
/-- 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
Terence wrote:
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;

--
Best regards,
Andrey Tarasevich

Nov 13 '05 #3
"Terence" <fi****@hotmail.com> wrote...
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
Nov 13 '05 #4
Terence wrote:
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

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Nov 13 '05 #5
Terence wrote:

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;
...

--
Er*********@sun.com
Nov 13 '05 #6
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Terence | last post by:
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...
21
by: MOvetsky | last post by:
Is the following code ISO C++ standard compliant? If yes, is it guaranteed that it will not crash on compliant platforms? If yes, will it print "Pointers are equal" on any compliant platform? Will...
27
by: Riaan Cillié | last post by:
Hi I'm trying to learn C, but I am struggling with using scanf and a struct. I want to define a structure and declare a variable of that type in int main. This has to be passed to a function and...
15
by: ranjeet.gupta | last post by:
Dear All, Which is correct ? Let suppose we allocate the memory for the 100 bytes, int *PtrMemoryBlock = (int *)malloc(100); If I do
18
by: Jacek Dziedzic | last post by:
Hi! I'm trying to squeeze a few clock cycles from a tight loop that profiling shows to be a bottleneck in my program. I'm at a point where the only thing that matters is execution speed, not...
4
by: Todd R. Jacobs | last post by:
Hi group, I had this with VS2003 Managed C++ pro-to-types: =================================== TimeHandler.h class TimeHandler
8
by: stefano.verna | last post by:
I would be grateful to anybody who can explain what on earth is going wrong. I have this tiny little piece of code: void sendOperation16 (int fd, int16_t o1, int16_t o2) { unsigned char response...
11
by: Army1987 | last post by:
Is this code legal (according to the strictest possible sane interpretation of the standard, regardless of wheter it does work on all implementations in the known universe)? #include <stdio.h>...
1
by: Francesco | last post by:
Hi to all, to make it short: I'm trying to build a small class that I conditionally include in some other class just for debugging purposes. This class should register itself in a container on...
6
by: lithiumcat | last post by:
Hi, maybe you remember me, some time ago I asked about how to store an integer value into a void*, and I learned that doing pointer arithmetic yeilding a pointer outside of an object (except the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.