Questions tagged [pointer-conversion]

Pointer conversion is a term mostly related to the C and C++ languages, for when a pointer to one type is converted to a pointer of another type.

Pointer conversion is a term mostly related to the C and C++ languages, for when a pointer to one type is converted to a pointer of another type. It could also mean a conversion between pointers with different qualifiers (const, volatile etc), conversions to/from void* or conversions to/from null pointers.

The rules of pointer conversion (C11 6.3.2.3) are slightly different between C and C++. For example, C allows implicit conversions to/from a void pointer and a pointer to another type, while C++ does not allow it. The languages also have different rules for null pointers.

Pointer conversions are also related to function pointers. Function pointers cannot get converted to/from object pointer types, but may in some cases be converted to other function pointer types or to null pointers.

Tag usage:
Any question using shall also be tagged with the relevant language, likely either or . Since these two languages have different pointer conversion rules, both tags should not be used at once, unless the question is explicitly about the difference in pointer conversion rules between the two languages.

33 questions
55
votes
10 answers

Do all pointers have the same size in C++?

Recently, I came across the following statement: It's quite common for all pointers to have the same size, but it's technically possible for pointer types to have different sizes. But then I came across this which states that: While pointers are…
Jason
  • 36,170
  • 5
  • 26
  • 60
19
votes
1 answer

passing const pointer by reference

I am confused that why following code is not able to compile int foo(const float* &a) { return 0; } int main() { float* a; foo(a); return 0; } Compiler give error as: error: invalid initialization of reference of type 'const…
ravi
  • 3,304
  • 6
  • 25
  • 27
17
votes
2 answers

Proper type for a dynamic array of const strings

I was always under the impression that const char **x was the correct type to use for a dynamically allocated array of const strings, like so: #include int main() { const char **arr = malloc(10 * sizeof(const char *)); const char…
Andrew Sun
  • 4,101
  • 6
  • 36
  • 53
15
votes
3 answers

Conversion from Derived** to Base**

I was reading this and unfortunately could not understand in depth why the compiler does not allow conversion from Derived** to Base**. Also I have seen this which gives no more info than the parashift.com's link. EDIT: Let us analyze this code…
Narek
  • 38,779
  • 79
  • 233
  • 389
7
votes
1 answer

Is casting to (void**) well-defined?

Suppose A is a struct and I have a function to allocate memory f(size_t s, void **x) I call f to allocate memory as follows. struct A* p; f(sizeof(struct A), (void**)&p); I wonder if (void**)&p here is a well-defined casting. I know that in C, it…
6
votes
1 answer

Passing a Swift protocol to an Objective-C pointer

Using XCode 10.1 / Swift 4.2. I'm trying to assign an object that conforms to a Swift protocol to an Objective-C pointer. The following code is a minimal example that compiles and works as expected, but it gives me the following warnings: If…
6
votes
1 answer

Pointer/integer arithmetic (un)defined behaviour

I have the following function template: template HeldAs* duplicate(MostDerived *original, HeldAs *held) { // error checking omitted for brevity MostDerived *copy = new MostDerived(*original); std::uintptr_t…
4
votes
1 answer

Pointer-to-Pointer-to-Const Conversion

I'm reading a book called C++ Gotchas which explains the conversions between const pointers and I'm having some trouble understanding the following rules: Two pointer types T1 and T2 are similar if there exists a type T and integer n > 0 such…
user1086635
  • 1,536
  • 2
  • 15
  • 21
4
votes
2 answers

Is it legal to implement inheritance in C by casting pointers between one struct that is a subset of another rather than first member?

Now I know I can implement inheritance by casting the pointer to a struct to the type of the first member of this struct. However, purely as a learning experience, I started wondering whether it is possible to implement inheritance in a slightly…
user4385532
3
votes
3 answers

Might casting void* into unsigned long int cause undefined behaviour even where sizeof(void*)==sizeof(unsigned long int)

size_t size_int = sizeof(unsigned long int); size_t size_ptr = sizeof(void*); printf("sizeof(unsigned long int): %zu\n", size_int); printf("sizeof(void*): %zu\n", size_ptr); if(size_int == size_ptr) { int a = 0; void * ptr_a = &a; …
ahk
  • 43
  • 5
3
votes
2 answers

Is (void*) ptr == ptr always true?

I cut this question out of my last question because i thought this was rather an individual question. So i found the passages for pointer conversion in the standard from which the ones regarding my question are: 6.3.2.3 Pointers 1 A pointer to…
Yastanub
  • 1,227
  • 8
  • 19
3
votes
1 answer

C++: is reinterpret_cast the best choice in these scenarios?

This has been bugging me for a very long time: how to do pointer conversion from anything to char * to dump binary to disk. In C, you don't even think about it. double d = 3.14; char *cp = (char *)&d; // do what u would do to dump to disk However,…
TerryTsao
  • 565
  • 7
  • 16
3
votes
1 answer

Accessing first field of struct in a union of structs

I have three structs that share the first type and name of the first field: struct TYPEA { char *name; int x,y; /*or whatever*/ }; struct TYPEB { char *name; float a[30]; /*or whatever*/ }; struct TYPEC { char *name; void *w,*z; /*or…
afiori
  • 465
  • 3
  • 15
3
votes
1 answer

C++ conversion: have pointer to object member, calculate pointer to object

C++ has static_cast to convert base_class_pointer to derived_class_pointer. It is very similar operation to convert object_data_member_pointer to object_pointer. I wrote the function ConvertDataMemberPtrToObjectPtr using unsafe C type conversion.…
2
votes
2 answers

Aliasing and pointer-interconvertability

Given the following code: struct Tag {}; struct X { // Tag t; // if not commented it shouldn't be pointer-interconvertible int k; }; int fn(const X& x, int& p) { int i = x.k; p = 2; return i + x.k; } The generated code is: fn(X…
wimalopaan
  • 4,838
  • 1
  • 21
  • 39
1
2 3