Why can't I create an int array using 'const int *' the same as 'const char *'?

Why can I create a string or character array this way:

#include <iostream>
int main() {
  const char *string = "Hello, World!";
  std::cout << string[1] << std::endl;
}

      

? and it outputs the second element correctly, while I cannot create an array of integer type without index notation [ ]

? What is the difference between char and one: const int* intArray={3,54,12,53};

.

+3


source to share


4 answers


Why: Because string literals are special. A string literal is stored in binary as a constant part of the program itself, but const char *string = "Hello, World!";

simply treats the literal as an anonymous array stored elsewhere, which then stores a pointer to string

.

There is no equivalent special behavior for other types, but you can get the same basic solution by creating a named static constant and using that to initialize the pointer, for example.

int main() {
  static const int intstatic[] = {3,54,12,53};
  const int *intptr = intstatic;
  std::cout << intptr[1] << std::endl;
}

      



The effect of an array static const

is to allocate the same constant space that a string literal will use (although unlike string literals, it is less likely that the compiler will identify duplicate arrays and concatenate memory), but as a named variable and not anonymous. The string register could be done in the same way:

int main() {
  static const char hellostatic[] = "Hello, World!";
  const char *string = hellostatic;
  std::cout << string[1] << std::endl;
}

      

but using a literal directly makes things a little cleaner.

+4


source


Such a feature exists in C and is called a compound literal .

for example

#include <stdio.h>

int main(void) 
{
    const int *intArray = ( int[] ){ 3, 54, 12, 53 };

    printf( "%d\n", intArray[1] );

    return 0;
}

      

However, C ++ does not support this feature since C.



There is a difference compared to string literals. String literals have a constant storage duration regardless of where they appear, while complex literals have either a static storage duration or an automatic storage time depending on where they appear.

In C ++, what's close to this function is std::initializer_list

. for example

#include <iostream>
#include <initializer_list>

int main() 
{
    const auto &myArray = { 3, 54, 12, 53 };

    std::cout << myArray.begin()[1] << std::endl;

    return 0;
}

      

+1


source


You can almost. There are several things at work.

{1,2,3}

and "abc"

- they are not the same thing. In fact, if you want to make a comparison, "abc"

compare with {'a', 'b', 'c', '\0'}

. Both are valid array initializers:

char foo[] = "abc";
char bar[] = {'a', 'b', 'c', '\0'};

      

However, only is "abc"

also a valid expression for pointer initialization in C ++.

In C (and as an extension in some C ++ compilers, including Clang and GCC), you can cast compound literals to an array type, for example:

static const int* array = (const int[]){1, 2, 3};

      

However, this is almost never correct. It works globally and as a function argument, but if you try to initialize an auto-store variable with it (i.e. a variable inside a function) you get a pointer to an expiring location, so you can't use it for what -or useful.

+1


source


String literals come from the C language. Any string declared with double quotes in the code is automatically converted to const char [].

So this:

const char str[6] = "hello";

      

Just like:

const char str[6] = { 'h', 'e', 'l', 'l', 'o', '\0' };

      

0


source







All Articles