C++ storing a string in a pointer.

  • Comp Sci
  • Thread starter Mr Virtual
  • Start date
  • Tags
    C++ String
In summary, when you assign a string to a pointer, the compiler reserves space in the executable's data segment and initializes the pointer to point to the first character of the string.
  • #1
Mr Virtual
218
4

Homework Statement



If I write:

char* Name="Computer";
cout<<Name[0]; //Output: C
cout<<Name[2]; // Output: M

what do the above lines mean? How can a pointer store a string? And why is it acting like an array? Either it is a simple concept that I have forgotten or it is a new concept that I have yet to study.


The Attempt at a Solution



Here, Name is a pointer of char type i.e. it can store the address of a char type variable. We cannot assign a character to a pointer. Then how is it still correct to assign a string to a pointer. What is happening here?
Please Help!

Thanks
Mr V
 
Physics news on Phys.org
  • #2
You're using a shorthand notation.

When the compiler sees quote marks, it actually reserves space in your executable's data segment for the stuff inside the quotes, and then initializes the pointer Name to point to the first character of it. There's no deeper magic going on.

- Warren
 
  • #3
As chroot said, behind the scenes the compiler is doing.
char temp[] = "Computer"
char *Name = temp[0];

Does this make it clearer?
 
  • #4
Ok, so the compiler reserves a temporary sapce for the string and makes the pointer point at the address of the first character. But what does it mean if I write

cout<<Name[1];

and it gives 'O' as output, if the string entered was "COMPUTER"?

Is this equivalent to: cout<<Temp[1];
So C++ actually says that if you assign a character to a pointer, then it is an error, but if you assign a string, you are not actually assigning it to the pointer but creating a space to store the string.

There is one more problem:
If Name is storing the address of the first character of the string, how come I get "COMPUTER" as output when I write:

cout<<Name;

It should show me the address of where 'C' is stored, but it gives me a string. Are all the pointer rules bent for this type of thing. Can you direct me to a site which provides all the rules for such a case and explains them.

Thanks for your answers.

Mr V
 
  • #5
When I write:

cout<<*Name; //Output: C

Which means it "is" storing the address of the first character entered. But why does it display the string in the case I have shown in my previous post above?

Mr V
 
  • #6
Because the 'C' rules for outputing a string are, start at the first character and continue until you get a '\0'.
Strings are just areas of memory with a 0 byte at the end.

If you want to output just the first letter you would do something like,
char c = Name[0];
cout << c;

or possibly
cout << (char)Name[0]
 
  • #7
Thanks. I have now understood the answer to my question.

Thanks again for your answers.

Mr V
 

FAQ: C++ storing a string in a pointer.

What is C++?

C++ is a high-level programming language that was developed in the 1980s as an extension of the C programming language. It is commonly used for developing system software, game engines, and other complex applications.

What is a pointer in C++?

A pointer in C++ is a variable that stores the memory address of another variable. It allows for indirect access to the data stored in that memory address.

How do you store a string in a pointer in C++?

To store a string in a pointer in C++, you first need to declare the pointer variable using the asterisk (*) symbol. Then, you can use the built-in function "strcpy" to copy the string into the memory location pointed to by the pointer.

Why would you store a string in a pointer in C++?

Storing a string in a pointer in C++ can be useful for dynamically allocating memory for the string, allowing for more flexibility in manipulating and accessing the data. It can also be used for passing strings between functions.

What are the potential drawbacks of storing a string in a pointer in C++?

One potential drawback is the risk of memory leaks if the memory allocated for the string is not properly released. Additionally, using pointers can be more complex and error-prone compared to directly storing the string in a variable, especially for beginners.

Similar threads

Replies
2
Views
1K
Replies
7
Views
2K
Replies
3
Views
2K
Replies
14
Views
4K
Back
Top