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?
Related
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);
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:
How do I use arrays in C++?
(5 answers)
Why can't I assign an array variable directly to another array variable with the '=' operator?
(5 answers)
Closed 8 years ago.
Hello I am beginner in c++ , can someone explain to me this
char a[]="Hello";
char b[]=a; // is not legal
whereas,
char a[]="Hello";
char* b=a; // is legal
If a array cannot be copied or assigned to another array , why is it so that it is possible to be passed as a parameter , where a copy of the value passed is always made in the method
void copy(char[] a){....}
char[] a="Hello";
copy(a);
It isn't copying the array; it's turning it to a pointer. If you modify it, you'll see for yourself:
void f(int x[]) { x[0]=7; }
...
int tst[] = {1,2,3};
f(tst); // tst[0] now equals 7
If you need to copy an array, use std::copy:
int a1[] = {1,2,3};
int a2[3];
std::copy(std::begin(a1), std::end(a1), std::begin(a2));
If you find yourself doing that, you might want to use an std::array.
The array is silently (implicitly) converted to a pointer in the function declaration, and the pointer is copied. Of course the copied pointer points to the same location as the original, so you can modify the data in the original array via the copied pointer in your function.
This question already has answers here:
Why are references not reseatable in C++
(17 answers)
Closed 9 years ago.
Why references can not be reinitialized in C++ while pointers can be reinitialized?
int x=5;
int y=6;
int *p1;
p1 = &x;
p1 = &y; //re-initializing the pointer but same can not be done with references
int &r1 =x;//can be initialized only once
There's no obvious syntax. You can't use the normal = syntax; that sets the value the underlying pointer of the reference. Perhaps you could think up a syntax like this:
&my_reference = new_value;
But that's kind of strange and awkward.
This question already has answers here:
Do the parentheses after the type name make a difference with new?
(7 answers)
Closed 9 years ago.
What is the difference between the following 2 two initializations?
class Pod {
public:
int a, b;
};
Pod *p1 = new Pod;
Pod *p2 = new Pod();
In the first case the object is left uninitialized, while in the second case the object is guaranteed to be value-initialized, which in this case as the type is POD it means zero-initialized