I know there are millions of similar question on this site, but none points to the topic I am having difficulty in understanding.
I would like to assign an integral literal to an integer pointer like this:
int *p=(int []){7} //casting the literal to array so that it would return the address of first element just like string literal.
But it gives the error:
a parenthesized type followed by an initializer list is a non-standard explicit type conversion syntax.
And this code works perfectly in C.
If this type of conversion is illegal in C++ , is there any other way to achieve the same thing?
The null pointer is the only integer literal that may be assigned to a pointer.
Pointer literal (C++11) The only pointer literal is the nullptr keyword that is a prvalue of type std::nullptr_t . A prvalue of this type is a null pointer constant that can be converted to any pointer type, pointer-to-member type, or bool type. Parent topic: Literals.
You need to initialize a pointer by assigning it a valid address. This is normally done via the address-of operator ( & ). The address-of operator ( & ) operates on a variable, and returns the address of the variable. For example, if number is an int variable, &number returns the address of the variable number .
Often, the literals are placed into the read-only section of the executable. Some tools may also place the literals into a separate data file. This data file may be used to program the data into read-only memory devices (ROM, PROM, Flash, etc.).
As pointers and arrays behave in the same way in expressions, ptr can be used to access the characters of string literal. For example: char x = *(ptr+3); char y = ptr[3]; Here, both x and y contain k stored at 1803 (1800+3). Pointers to pointers. In C++, we can create a pointer to a pointer that in turn may point to data or other pointer.
Define a pointer variable Assigning the address of a variable to a pointer using unary operator (&) which returns the address of that variable. Accessing the value stored in the address using unary operator (*) which returns the value of the variable located at the address specified by its operand.
In C++, we can create a pointer to a pointer that in turn may point to data or other pointer. The syntax simply requires the unary operator (*) for each level of indirection while declaring the pointer. char a; char *b; char ** c; a = ’g’; b = &a; c = &b; Here b points to a char that stores ‘g’ and c points to the pointer b. Void Pointers
Except when it is the operand of the sizeof or unary * operators, or is a string literal being used to initialize an array in a declaration, an expression of type "N-element array of T " will be converted ("decay") to an expression of type "pointer to T ", and the value of the expression will be the address of the first element of the array.
The syntax
int *p=(int []){7};
is supported in C99 as well C11, but not in C++.
Options in C++11:
Use a plain old array.
int p[] = {7};
Use a std::array
.
std::array<int, 1> p = {7};
Use a std::vector
.
std::vector<int> p(1, 7);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With