0
#include <iostream>
#include <string>
using namespace std;

int main() {
    string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
    cars[0] = "Opel";
    cout << cars;
    return 0;
}

Why is it returning 0x7ffffcf9a010 when I output it?

Tustin2121
  • 2,023
  • 2
  • 24
  • 37
  • 4
    `cout` doesn't know how to print arrays. Use a loop to print each string individually. – HolyBlackCat Oct 29 '21 at 17:26
  • 2
    it is displaying the memory address of the first member of the array – newdeep ji Oct 29 '21 at 17:26
  • 1
    What is you purpose when you assign `cars[0] = "Opel";`? What do you _expect_ when outputting `cars`? – Ted Lyngmo Oct 29 '21 at 17:27
  • 1
    @TedLyngmo well while learning through w3 schools, i came through this code and when i tried to edit it a bit, i wanted to know the output but it outputted some pointer variable so i was little confused but yeah, now i get it why it was happening, thanks in advance :) – Some_Weirdo Nov 01 '21 at 18:23
  • @HolyBlackCat , oh, fine! – Some_Weirdo Nov 01 '21 at 18:24

3 Answers3

3

Yes, it will output that, the strange number you see is the address of the starting of the first element of the array, cars is implicitly converted to a pointer. By itself, it's an array rather than a pointer.

You want to do this,

#include <iostream>
#include <string>
using namespace std;

int main() {
    string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
    cars[0] = "Opel";
    //cout << cars[0]; // To print the first element
    for(int i = 0; i < 4; i++)
    {
        // To print all the elements one by one with a new line in between each element
        cout<<cars[i] << '\n';
    }

    return 0;
}
Ted Lyngmo
  • 60,763
  • 5
  • 37
  • 77
Anuraag Barde
  • 369
  • 3
  • 9
  • 10
    `cars` is *implicitly converted* to a pointer. By itself it's an array rather than a pointer. – HolyBlackCat Oct 29 '21 at 17:28
  • 4
    "*`cars` is the pointer to the first string in the array*" - no, it is not a pointer. It is an array, period. It would be more accurate to say that it *decays* into a pointer to the 1st string. Not quite the same thing as *being* a pointer. – Remy Lebeau Oct 29 '21 at 17:30
  • 1
    Yes, sorry for the in-accurate wordings, I have corrected accordingly. – Anuraag Barde Oct 29 '21 at 17:36
  • yeah, so it means that when i output using cout , it will return a pointer variable always and i have to use loops instead of using cout to get a list. – Some_Weirdo Nov 01 '21 at 18:27
  • Yes, you will have to do that. You can also do cout << cars[0]; if you want to specifically print a specific index element – Anuraag Barde Nov 02 '21 at 20:46
0

Output the name of raw array will return the first address of string array, which is the first element address too. Here's an example with std::string and int raw array.

#include<iostream>

int main() {
    std::string str[] = {"This", "is", "a", "string"};
    std::cout << str << std::endl;    //output 0xc3585ff5b0

    int arr[] = {1, 2, 3, 4, 5};
    std::cout << arr << std::endl;    //output 0xc3585ff590
    return 0;
}
iEcho-42
  • 77
  • 5
0

It outputs the hexadecimal address of the first element(0 index) of the array. Since array is a data structure that's why it outputs its address.

To correct the code you need to do this:

#include <iostream>
#include <string>


int main() {
    std::string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
    cars[0] = "Opel";
    
    char string1[100]={"C-style string."};
    std::cout << *cars<<"\n";
    
    
    std::cout<<string1; // does not show hexadecimal address but why?
    return 0;
}

Now this code will output 'Opel' as you required. The little asterisk next to the cars is known as the indirection operator. It is used to access the value the pointer is pointing to. Your confusion might be coming from the char array which can be outputted in one go using std::cout but the char array is an exception to the rule, the string array is not the same. Note(it is better use std::array and std::vector for modern C++ programming instead of the old fashioned array. )