0

Why can we do:

char* array = "String";

but not

int* array = 1;

To my understanding * means address, so I don't really understand why we can give a non-address value, like "String." to char* array.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • * doesn't mean address. Look up pointers in your book. – yizzlez Jun 07 '15 at 15:38
  • Yeah, I get that. It points to a memory address, hence why we can do code like: int a = 1; int* pointer = &a. &a is the address, and pointer points to that address. I misworded the question, so apologies for that, but I am still left confused about why char* array can accept a value that is not a address. – user2814533 Jun 07 '15 at 15:40
  • possible duplicate of [Declare and initialize pointer concisely (i. e. pointer to int)](http://stackoverflow.com/questions/24805673/declare-and-initialize-pointer-concisely-i-e-pointer-to-int) – Quentin Jun 07 '15 at 15:51

5 Answers5

2

char* array means that array is a variable that can hold the address of another object (such as another variable or constant).

If the program has "String", it means that there is actually an array of 7 characters that exists in memory somewhere and it holds the contents "String".

When you write array = "String"; then the variable array is made to hold the address of the letter 'S' in that string.

This is because C++ has a rule, sometimes called array-pointer decay which means that if you try to use an array (such as "String") in a context where a value is expected, then it gets automatically converted to a pointer to the first element of that array.

Without that rule you'd have to write array = &("String"[0]); . The rule was included in C originally to avoid having to write &....[0] all over the place when working with arrays, although in hindsight it seems to have generated more pain than pleasure.

Moving onto int* i = 1. You have said that i can hold the address of an int, but you have not provided any such address. Variables thare aren't arrays don't automatically get converted to their address. In fact 1 isn't even a variable. We call it a prvalue , it doesn't have any memory storage area associated with it, so it does not have an address. To point at an instance of a 1 you would have to make a variable, for example:

int j = 1; int* i = &j;
M.M
  • 138,810
  • 21
  • 208
  • 365
1

* does not mean adress. Its meaning is context sensitive, but most of the time it means pointer.

The reason for this, not to work is because "String" is an array of characters, or a pointer to an character. In contrast to this, 1 is a literal which is not a valid adress. You should write int array = 1 instead, and after that you could do int *brray = &array.

hellow
  • 12,430
  • 7
  • 56
  • 79
  • Got it, so can you just verify whether I have this right now? a int is a literal, a string is a pointer to the address of the first character in what is basically a char array. – user2814533 Jun 07 '15 at 15:45
  • 1
    @user2814533 A string literal has slightly unusual behavior in C/C++, but yes, if you use it as a pointer, it's a pointer to an array of chars. – Sneftel Jun 07 '15 at 15:59
0

"I don't really understand why we can give a non-address value, like "String." "

That's because a character string literal actually is const char[] array, which decays to a pointer when assigned to a char*, while 1 isn't one and you can't take it's address in any way.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    No it isn't. The type of a string literal is `const char[N]`. It's not an address. – jalf Jun 07 '15 at 15:52
0

* means a pointer, not an address. You can retrieve an address using the & operator.

char* array = "String";

Actually declares array as a pointer to a character, and the = sign after the declaration tells the compiler what value should the pointer posses. In this case it's an address of "String" in the string pool somewhere in the memory of the run program.

int* array = 1;

Doesn't put the address of 1 to array as you may expect. However, with a little adjustment

int* array = (int*)1;

... it could point to an integer at address 1, which is unfortunately unaccessible.

user35443
  • 6,309
  • 12
  • 52
  • 75
0

This assignment: char* array = "String";
Assign "String" to an available location in memory and returns the memory address of the first position of "String" in memory. "array" stores the address of "S".

This assignment: int* array = 1;
Doesn't work because you are trying to assign an integer to a pointer to integer. The types are different