pointer-conversion

passing const pointer by reference

旧街凉风 提交于 2019-12-20 12:05:37
问题 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 float*&' from expression of type 'float*' but when I try to pass without by reference in foo, it is compiling fine. I think it should show same behavior whether I pass by reference or not. Thanks, 回答1: Because it isn't type-safe. Consider: const float f = 2.0; int foo

C function pointer type compatibility

雨燕双飞 提交于 2019-12-13 17:00:38
问题 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 (all data), or void pointers. Recently, I discovered that it might not be that safe, according to this: https://stackoverflow.com/a/14044244/3079266 Basically, as I understood it, if the types of the arguments are compatible, that means the function

passing const pointer by reference

北战南征 提交于 2019-12-03 02:29:17
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 float*&' from expression of type 'float*' but when I try to pass without by reference in foo, it is compiling fine. I think it should show same behavior whether I pass by reference or not. Thanks, Because it isn't type-safe. Consider: const float f = 2.0; int foo(const float* &a) { a = &f; return 0; } int main() { float* a; foo(a); *a = 7.0; return 0; } Any non- const

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

不打扰是莪最后的温柔 提交于 2019-11-29 22:21:50
问题 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 different way. Is this code legal? #include <stdio.h> #include <stdlib.h> struct base { double some; char space_for_subclasses[]; }; struct derived { double some; int value; }; int main(void) { struct base *b = malloc(sizeof(struct derived)); b->some

Conversion from Derived** to Base**

丶灬走出姿态 提交于 2019-11-27 09:26:20
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 line by line: Car car; Car* carPtr = &car; Car** carPtrPtr = &carPtr; //MyComment: Until now there is no problem! Vehicle** vehiclePtrPtr = carPtrPtr; // This is an error in C++ //MyComment: Here compiler gives me an error! And I try to understand why. //MyComment: Let us consider that it was allowed. So what?? Let's go ahead! NuclearSubmarine sub;

Conversion from Derived** to Base**

混江龙づ霸主 提交于 2019-11-26 17:50:23
问题 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 line by line: Car car; Car* carPtr = &car; Car** carPtrPtr = &carPtr; //MyComment: Until now there is no problem! Vehicle** vehiclePtrPtr = carPtrPtr; // This is an error in C++ //MyComment: Here compiler gives me an error! And I try to understand why.