Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning literals to pointers

Tags:

c++

casting

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?

like image 746
parth_07 Avatar asked Feb 27 '17 06:02

parth_07


People also ask

Can integer literals be assigned to pointers?

The null pointer is the only integer literal that may be assigned to a pointer.

What is a pointer literal?

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.

How do you assign a pointer to a variable?

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 .

Where are literals stored in c++?

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.).

How to access the characters of string literal in C++?

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.

How do you assign a variable to a 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.

How to create a pointer to a pointer in C++?

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

What is the pointer to the first element of an array?

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.


1 Answers

The syntax

int *p=(int []){7};

is supported in C99 as well C11, but not in C++.

Options in C++11:

  1. Use a plain old array.

    int p[] = {7};
    
  2. Use a std::array.

    std::array<int, 1> p = {7};
    
  3. Use a std::vector.

    std::vector<int> p(1, 7);
    
like image 74
R Sahu Avatar answered Oct 13 '22 22:10

R Sahu