Skip to main content
Filter by
Sorted by
Tagged with
1 vote
3 answers
189 views

What is the correct form of a type-cast which should be used for non-typesafe binary data operations involving pointer casts which cause data to be reinterpreted as a different type? For example, if I ...
user2138149's user avatar
  • 18.7k
5 votes
3 answers
392 views

Generally pointers on x86-64 are defined to be 8 bytes. However, if you are certain that you have data that will only ever be in the first 4GB of the address space, then a 32-bit value is technically ...
Joseph Garvin's user avatar
3 votes
2 answers
200 views

Say you have an array of floats representing raw data, and several types representing 2D shapes that only have floats as members, like this: #include <iostream> #include <array> struct ...
greenlagoon's user avatar
4 votes
1 answer
80 views

I have a struct called Node whose first member is Node* p_next I have a pointer to the first node called p_head I want to cast &p_head which is of type Node** to a Node*, and store it in ...
timmy george's user avatar
1 vote
0 answers
80 views

Another question here lead me to a new problem. In C++ Builder I need to assign a reader to the TFDBatchMove::Reader property in runtime. The TFDBatchMove component is dropped on the form but then I ...
Max Kielland's user avatar
  • 5,871
2 votes
2 answers
442 views

I have the following code from a serial message encoder: uint32_t bits; *((float *) &bits) = some_external_variable; pFrame->data[0x2C] = ((uint8_t)(bits >> 0x18) & 0xFFu); pFrame->...
fschwaiger's user avatar
-2 votes
1 answer
147 views

This article lists all the different categories of pointers. I have tested the explicit conversion of different types of pointers to const void* in the following snippet (live): #include <print> ...
digito_evo's user avatar
  • 3,735
4 votes
1 answer
235 views

I am learning C++ using the book "C++ Primer" by Stanley. In particular, the section about "pointer conversions" says: a pointer to any nonconst type can be converted to void* ...
user avatar
7 votes
1 answer
244 views

While answering a question here, I made the following example: #include <stdio.h> #include <math.h> int main (void) { float_t a = -248.75; printf("%f\n", a); unsigned ...
Lundin's user avatar
  • 220k
0 votes
1 answer
970 views

#define HNULL (void*)0 BYTE *VRAM_OPTION_I_BIT = HNULL; WORD *VRAM_OPTION_I_WORD = HNULL; void Func1(void) { VRAM_OPTION_I_BIT = (BYTE *)(PLC_Data + VRAM_OPTION_I_BIT_ADDR); ...
Blackbriar07's user avatar
7 votes
1 answer
239 views

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**)&...
user18676624's user avatar
0 votes
1 answer
937 views

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 ...
John Hoffman's user avatar
0 votes
1 answer
292 views

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 ...
Alex Guteniev's user avatar
58 votes
10 answers
11k views

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 ...
Richard's user avatar
  • 47k
0 votes
1 answer
156 views

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 ...
editsons's user avatar
0 votes
1 answer
73 views

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 ...
sparsh goyal's user avatar
0 votes
1 answer
200 views

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 ...
Alex's user avatar
  • 1,175
0 votes
1 answer
408 views

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* --> ...
thedoctar's user avatar
  • 9,114
3 votes
3 answers
768 views

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(...
ahk's user avatar
  • 43
0 votes
2 answers
130 views

it's a pointer arithematic question //code #include<iostream> using namespace std; int main() { int a=20,b=50,*p,*q; p=&a; q=&b; ...
Ritik Kumar's user avatar
2 votes
2 answers
318 views

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; ...
wimalopaan's user avatar
  • 5,552
2 votes
2 answers
274 views

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 ...
user avatar
3 votes
2 answers
413 views

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 ...
Yastanub's user avatar
  • 1,247
3 votes
1 answer
505 views

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 *)&...
TerryTsao's user avatar
  • 788
6 votes
1 answer
918 views

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, ...
agirault's user avatar
  • 3,086
-3 votes
1 answer
76 views

can the Pointer have value?? so In which case is it used int num=100; int* iptr=NULL; iptr=reinterpret_cast<int*>(num); printf("%d \n",num); printf("%d \n",num); result 100 100
Tree's user avatar
  • 7
3 votes
1 answer
2k views

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*/ };...
afiori's user avatar
  • 669
4 votes
2 answers
1k views

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 ...
user avatar
17 votes
2 answers
603 views

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 <stdlib.h> int main() { const char **...
Andrew Sun's user avatar
  • 4,271
1 vote
1 answer
418 views

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 (*)())...
翟桂锋's user avatar
  • 158
0 votes
1 answer
1k views

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 ...
Robin Hsu's user avatar
  • 4,574
2 votes
1 answer
608 views

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<T*>(reinterpret_cast<uintptr_t>(p0)) == p0; But is ...
yuri kilochek's user avatar
2 votes
1 answer
1k views

Consider this case: int *ptr; int offset; ptr = <some_address>; offset = 10; Assume that offset is 32-bit variable. ptr has type int*, the target architecture is 64-bit (so ptr is 8-byte ...
Alexander_KH's user avatar
2 votes
1 answer
2k views

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: ...
user avatar
2 votes
1 answer
132 views

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, ...
Mitsukurina owstoni's user avatar
6 votes
1 answer
542 views

I have the following function template: template <class MostDerived, class HeldAs> HeldAs* duplicate(MostDerived *original, HeldAs *held) { // error checking omitted for brevity MostDerived ...
Angew is no longer proud of SO's user avatar
-2 votes
2 answers
359 views

I'm at a loss to understand how typecasting works with pointers double x = 0.7; int *ptr = (int *)&x; What is happening with *(byte )&x ? &x means the address of variable x. Then what ...
Dubby's user avatar
  • 1,144
2 votes
1 answer
1k views

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 ...
Prannoy Mittal's user avatar
3 votes
1 answer
143 views

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 ...
stan5's user avatar
  • 81
-2 votes
1 answer
1k views

uint32_t r,g,b; r = (uint32_t)145; g = (uint32_t)131; b = (uint32_t)139; uint32_t rgb = ((uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b); float rgbf = *reinterpret_cast<float*&...
RAM JI GUPTA's user avatar
20 votes
1 answer
17k views

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: ...
ravi's user avatar
  • 3,444
4 votes
1 answer
341 views

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 ...
user1086635's user avatar
  • 1,544
16 votes
3 answers
3k views

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 ...
Narek's user avatar
  • 40.2k