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
2
votes
2 answers

Address location and value at address location in C

int PileInts[1024]; char *Pile = (char *)PileInts; What do these two lines of code do? I am thinking that the char *Pile = (char *)PileInts; line creates a character named *Pile that gives the value of the address at PileInts. Am I right? Could I…
user12116614
2
votes
1 answer

Is it safe to cast pointer to integer, increment that integer, and cast back?

Suppose I have a valid pointer p0: T a[10]; T* p0 = &a[0]; I know that I can safely round-trip-cast it like this: reinterpret_cast(reinterpret_cast(p0)) == p0; But is it safe to do the following? T* p1 =…
yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
2
votes
1 answer

C++ implicit conversion of pointer type

Consider this case: int *ptr; int offset; ptr = ; offset = 10; Assume that offset is 32-bit variable. ptr has type int*, the target architecture is 64-bit (so ptr is 8-byte variable), offset has type int. What conversion will be…
Alexander_KH
  • 189
  • 12
2
votes
1 answer

C function pointer type compatibility

Writing a library that works with function callbacks, I've frequently type-casted (and called) function pointers to types with the same calling convention and same signatures, but with one exception: they had parameters pointing to different types…
user3079266
2
votes
1 answer

Why is it not allowed to cast Derived T::* to Base T::*?

Background: Many functional languages support algebraic data types, which can to a degree be emulated with virtual functions and inheritance. The most obvious solution involves a heap allocation, since the derived types come in different sizes.…
2
votes
1 answer

Conversion of uchar array to uint8 type pointer

I want to convert a array of uchar to uint8 pointer. As both are of 8 bits and value ranges from 0 to 255 so I do not think it should cause and issue. uchar list[100]; I have to pass above list to a function which accepts pointer to uint8t. Can i…
Prannoy Mittal
  • 1,525
  • 5
  • 21
  • 32
1
vote
1 answer

Protobuf-2.6.1 compile error on Solaris 10 SPARC 64

When trying to compile Protobuf-2.6.1 on Solaris 10 SPARC 64, I get: ./google/protobuf/stubs/once.h: In function `void google::protobuf::GoogleOnceInit(google::protobuf::ProtobufOnceType*, void (*)())': ./google/protobuf/stubs/once.h:125: error:…
翟桂锋
  • 158
  • 1
  • 7
0
votes
1 answer

Procedurally bind struct fields to command line flag values using reflect

I have a several configuration structures that I want to automatically parse into accepted command line flags (to allow a user to override them via CLI). Given that the structures are likely to evolve over time, and one of the structures is an…
John Hoffman
  • 5
  • 1
  • 2
0
votes
1 answer

Should static_pointer_cast calls be std:: qualified, or relied upon ADL?

static_pointer_cast resides in std namespace. Yet it takes pointers that are also in std namespace. So both std:: qualified calls and unqualified calls are accepted. Which one is the right, more idiomatic way to call static_pointer_cast?
Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
0
votes
1 answer

C++ Socket recv() always returns 0

This does work: char blockSize[4]; int r = recv(socket, blockSize, 4, 0); This does not work. It always returns zero, why? size_t blockSize = 0; int r = recv(socket, (char*)blockSize, 4, 0); It's not a major problem, I know how to convert the char…
Created
  • 87
  • 1
  • 13
0
votes
1 answer

does implicit pointer conversion occur during assignment?

For e.g. int x = 3; float * ptr = (float*)&x; // here compiler does not implicitly do conversion, but we have to manually convert to float* so my question is, why here we don't need to manually convert it. Base_Class * ptr = Derived_Class…
sparsh goyal
  • 89
  • 1
  • 7
0
votes
1 answer

Proper handling of 128..255 chars in C

I need to process some Win-1251-encoded text (8-bit encoding, uses some of 128..255 for Cyrillic). As far as I can tell, C was created with 7-bit ASCII in mind, no explicit support for single-byte chars above 127. So I have several questions: Which…
Alex
  • 1,165
  • 2
  • 9
  • 27
0
votes
2 answers

Can Anyone explain why I'm getting 1 here?

it's a pointer arithematic question //code #include using namespace std; int main() { int a=20,b=50,*p,*q; p=&a; q=&b; int c=p-q; cout<
0
votes
1 answer

C++ polymorphism function taking void * and other pointer type as argument: is it considered ambiguous?

C++ polymorphism functions taking void * and other pointer type as their arguments: is it considered ambiguous? I am worried that since any pointer can be cast to void*, will the 2nd call of bar below executes void bar(void*) instead of my expected…
-1
votes
1 answer

Bug casting from bool* to void* to int*

Often in C++, one has a parameter void* user_data that one can use to pass an arbitrary type. I used this to pass an array of booleans. However, I had a bug where I cast from bool* --> void* --> int* and I got weird results. Here is an…
thedoctar
  • 8,943
  • 3
  • 20
  • 31