This question already has answers here:
Why C++ compiler allows assigning 0 to pointers and no other number [duplicate]
(3 answers)
Why are NULL pointers defined differently in C and C++?
(3 answers)
Closed 5 years ago.
The following code compiles in C but not in C++:
int *ptr = 25; //why not in C++?
Error
prog.cpp: In function ‘int main()’:
prog.cpp:6:11: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
int *ptr = 25;
But this compiles in both C and C++:
int *ptr = 0; //compiles in both
Why does assigning 0 work fine and other numbers does not work?
Because you can't implicitly convert from int to int* in C++, however 0 can, due to historical reasons, since it is often used as the value of NULL.
If you want to do it in C++, you need to explicitly cast the number to a pointer:
int *ptr = reinterpret_cast<int*>(25);
Related
This question already has answers here:
How do I declare a 2d array in C++ using new?
(29 answers)
Expression does not evaluate to a constant
(1 answer)
Closed 7 months ago.
const int32_t kernel_sizes[1] = {8};
void Create(int id)
{
float *kernel = new float[1][1][kernel_sizes[id]];
}
Create(0);
When I try to create this array, there is a error. The error is:
Array size is not a constant expression implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function. What is the reason for this error and how to solve this error?
This question already has answers here:
Cannot convert from XXX** to const XXX** [duplicate]
(3 answers)
How to convert "pointer to pointer type" to const?
(2 answers)
Closed 4 years ago.
E.g. the following will fail to compile:
auto foo = [](const char**) {};
char* ptr = nullptr;
foo(&ptr);
Visual studio says that there's a conversion that loses qualifiers. Clang says cannot initialize a parameter of type 'const char **' with an rvalue of type 'char **". Perhaps my brain just isn't working today, but why is this dis-allowed? char* can implicitly convert to const char* after all
Oh wait, yes my brain isn't working today. foo can assign a const char* to the pointer, which is clearly bad.
This question already has answers here:
Address of array VS pointer-to-pointer : Not the same?
(2 answers)
Closed 6 years ago.
Consider the below code snippet:
int x[] ={10,20,30,40,50};
int *p;
int **q;
p = x; /* Obviously p now holds base address of array. So no problem here */
q = &x; /* Error. Why? */
I am getting this error in gcc compiler.
error: cannot convert ‘int (*)[5]’ to ‘int**’ in assignment q = &x;
An array variable can be considered as constant pointer to first element of the array. Then why cant I assign the address of the constant pointer i.e) &x to pointer of pointer variable q ?
An array decays to a pointer in certain contexts, like assignment, or passing it to a function.
The address-of operator, &, does not decay the array into a pointer. It's as simple as that. Instead, you get a pointer to the array. In this case:
int (*q)[5];
q= &x; // Works just fine
This question already has answers here:
Pointer to class data member "::*"
(18 answers)
Closed 7 years ago.
I cannot figure out how this compiles. It seems like it should not, and if I use a value other than NULL in the constructor it will not.
#include <stdio.h>
class MyClass{
private:
int *first;
public:
MyClass();
};
MyClass::MyClass(){
int whatever = 42;
//int* MyClass::*first = &whatever;//This does not compile
int* MyClass::*first = NULL;//This compiles
}
int main(){
MyClass doSomething;
return 1;
}
It seems that generally the type Class::member = value syntax is used for static vars, which this is not.
Also, there is an asterisk before the member name, which confuses things even more.
If I switch the lines to the one that is commented out, the compiler complains, as expected.
error: cannot convert ‘int*’ to ‘int* MyClass::*’ in initialization
While I did expect an error, I have no idea what the type int* MyClass::* is. Or how it could be used.
This is a pointer to data member. You cannot initialize it with an ordinary pointer, that is why the commented out expression does not compile.
This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 8 years ago.
I recently wrote below simple program but compiler shows warning.
#include <iostream>
int main()
{
int a();
std::cout<<a;
return 0;
}
[Warning] the address of 'int a()' will always evaluate as 'true' [-Waddress]
What is the meaning of the above warning? Why value of a is 1 not 0?
It might look like a definition of a as an int, but:
int a();
declares a function a taking no parameters and return int.
Use:
int a{};
instead.
std::cout<<a;
calls operator<<() with bool which is always nonzero, hence true.
int a(); declares a function, not a variable. If you want a to be a zero-initialised variable, then you'll need one of
int a{}; // C++11 or later
int a = int();
int a(0);
int a = 0;
<< doesn't have an overload that can directly take a function; so it looks for a suitable conversion sequence to a type that it is overloaded for, and finds:
int() -> int(*)() -> bool
that is, using the standard function-to-pointer and pointer-to-boolean conversions. The function pointer won't be null, since a declared function must exist and have an address; so the boolean value will be true.