1
#include <iostream>
#include <cstring>
using namespace std;

int main(){
    char first_name[20] {};
    cout << "Enter Your First Name : ";
    cin >> first_name;
    cout << "Hi "<< &first_name <<", Welcome to C++ Programming.";
    return 0;
}

if I write &first_name it is giving the memory location of first element correctly, but unable to find location of other elements (eg. &first_name[0] etc.)

  • 1
    `&first_name[0]` might not be working because `ostream` has a separate `<<` for `char *` specifically. Try `static_cast(&first_name[0])`. (Also, don't spam tag. I don't see any need to include a version here but if you need to, only include one. And I don't even see any C++/CLI here). – mediocrevegetable1 Jun 13 '21 at 13:37
  • Why not simply `for (int i = 0; first_name[i]; i++) std::cout << (char)first_name[i] << '\n';` to output each letter in `first_name` separately? Also see [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) – David C. Rankin Jun 13 '21 at 14:12
  • Hello Sir, actually my concern is to find the memory location of each element,.....i know that using namespace std is not better way of approaching,... – K R Darshan Jun 13 '21 at 16:55

3 Answers3

0

Just use the c++ string instead

#include <iostream>
#include <cstring>
using namespace std;
int main(){
    string first_name;
    cout << "Enter Your First Name : ";
    cin >> first_name;
    cout << "Hi "<< first_name.c_str() <<", Welcome to C++ Programming.";
    return 0;
}

Enter Your First Name : Tess
Hi Tess, Welcome to C++ Programming.

Oceania Water
  • 267
  • 2
  • 11
  • :( sorry this does not answer his question. He is asking for address of a char array element. –  Jun 13 '21 at 14:58
0

Let's explain what happens when you do this:

&first_name[0]

According to operator precedence:

  • We start by fetching the first character in the character array using the subscript operator (we get a char)
  • We then get the address of that char using the address-of operator (we get char*)

Now the way things are set up in C++ (ostream in particular) is that a char* is generally considered a null terminated character array which will be read, one sizeof(char) at a time, until a \0 character is encountered. That's why

std::cout << &first_name[1];

would print something like oo if foo was given to stdin (foo would look like foo\0 in memory).

Now if you wanted to get the actual address of each character you could do one of two things:

  • Use math (you know that sizeof(char) is always 1)
  • Cast the char* to something else (eg void*) so that it is no longer considered a char array by ostream or whatever other function that works the same. To be clear char* is still an address (and is in fact the result you're looking for), it's just displayed differently because ostream is overloaded differently for char* where it is set up to print whatever is at that location and everything that follows until a null char.
mnhaouas
  • 101
  • 5
  • Simply said if you have `[]` in array and you feed it into `std::iostream`, it is treated as offset from start of an array. –  Jun 13 '21 at 14:56
0

OK, so @mnhaouas answer explained why it does not work but there are ways around it.

First, old and dirty C style, which should be avoided for production code.

std::cout << (void *) &first_name[4] << "\n";

Again this should be used for debugging or playing around. C++ is way stricter about converting pointers, but this is still possible

Other and better way to do it is:

cout << "Hi "<< std::static_cast<void*> (&first_name[3]) << "\n";

Again it is not recommended to use first one.