tags:

views:

82

answers:

5

Consider the below 2 declarations.

* appears next to the datatype and not next to variable

char* ptr1, * ptr2, * ptr3; //all 3 are pointers

* appears next to the variable and not next to datatype

char *ptr1,*ptr2,*ptr3; //again all 3 are pointers

Is there any difference in intepretation between the 2 declarations. I know there is no difference in the variables.

What is the rationale behind introducing void pointers?

+5  A: 

The only difference is whitespace, they end up being pointers-to-chars. I personally use char * ptr1, using plenty of whitespace in my code.

Void pointers can point to any type of object and have no size. For example, this works (but isn't really a good idea):

int * ptr1;
float * ptr2;
(*ptr1) = 17;
ptr2 = (void*)ptr1;

(*ptr2) will now interpret the bytes of ptr1 as a float instead of an int, giving you a different result. You can change between types. There are some uses for void*s in pointer-to-function typedefs, as well.

In addition, if you have char * ptr1 and do ptr1++, you will increment the address ptr1 refers to by sizeof(char). That's not possible with a void*, it will give you a compiler error saying it doesn't know what size to increment by.

peachykeen
+1  A: 

Void pointers are pointers to some unknown data, they can be cast to any type and thus are not typesafe an they might crash your program, use boost::any if there is a need for a total variant.
There's no difference between declarations.

the_drow
+1  A: 
  1. The only difference is in whitespace which irrelevant. The * does not "appear next to the data type" or "next to the variable" -- you could even write char*a,*b;. The important thing is the precendence. * binds to the right, so to the variable name. This also means that char* a, b; is equivalent to char (*a), b; and will result in a pointer to char a and a char b, not two pointers. If you want two pointers you need to use char *a, *b;, as in your example.
  2. A void pointer is a pointer to memory without specifying the type. This is useful when writing a function that should accept various types of data. For example, the c library function memcpy() copies arbitrary data. It doesn't care about the data it is getting passed. As you don't know what data it will get passed when implementing that function you need to use void* for that, as otherwise you would need a memcpy() function for each data type (combination) you could pass. As this is a c library function and c doesn't support function overloading this would require a lot of memcpy() functions with different names.
bluebrother
+1  A: 

void pointer represents the absence of data type, and hence can be used to point to data of any type. However, since the data type is unknown, there is no way to directly derreference the pointer. To do so, casting the pointer to a certain data type is needed beforehand.

Situations where void pointer is applicable: Sometimes, to make a function accept data of different types, e.g. int, char, double, etc, one might use template. (Although overloaded function is an option, but the least efficient way). Another way for this purpose is to use void pointer.

void FunctionName(void* data, type param) { /* based on the second argument param, dereference the pointer data such that the data can be processed in the desired way. */ }

Just a reminder: Void pointer and null pointer should be distinguished as they do have different purposes of usage and functionality. The latter does have a pointer type, but does not pointing to any valid address of any data.

Rui
A: 

I heard a joke, that says that if you ask a C++ programmer, they will tell you to put it on the left, and a C programmer on the right.

Ultimately, since * binds to the variable, not the type name, then it's undoubtedly a better idea to put it on the right to make the code clearer. However, since in C++ there' very little reason to deal with raw pointers, I never have an issue.

DeadMG
"Undoubtedly"? So you're saying C++ programmers do something that is "undoubtedly" wrong?The reason why some put it on the left is that it's more intuitive. It is part of the type specification. you're creating an int pointer, not an int. Depending on your perspective, either option is the most obviously correct.
jalf
"Undoubtedly" != significant. I always put it on the left personally, even though it's a better idea not to, which was the point of the final statement.
DeadMG