2

I am a beginner student in c++ and there is one thing I cannot understand when working with character arrays: So, I know that pointers are essentially variables that "point" to the memory address of another variable, and that the name of an array(Ex: int a[20]) is a constant pointer to the values in that array. When working with different numeric types(int, float etc.) if we output through a message the name of that array it shows the address of the first element, but if we do the same with a char type, it doesn't show the address, but the value of the variable. Example:

#include <iostream>

using namespace std;

int main()
{int a[]={1,2,3,4,5};
cout<<a<<endl; //through output, it shows the memory address of the first 
element of the array;
char b[]={"Mountain"};
cout<<b; //It outputs the word "Mountain"
    return 0;
}

Is the pointer from a char array automatically converted to its value when you output it?

machine_1
  • 4,266
  • 2
  • 21
  • 42
  • 6
    OT: As a quote: " beginner student in c++" you shouldn't study these types of arrays. Study `std::vector`, `std::array` and `std::string` instead. If you are going to write real C++ you ain't gonna need the old C-style array anyway. – Support Ukraine Apr 16 '19 at 12:13
  • The question as presented in the title is way too broad. If you want to ask why printing a char array works differently from printing an int array, a good title for your question would be "why does printing a char array work differently from printing an int array?" – n. m. could be an AI Apr 16 '19 at 12:34

4 Answers4

4

There is no difference. char pointers aren't somehow magically different than int pointers. So what's going on, than?

std::cout << (or the older printf()) have overloads for char*. Meaning that the functions behave differently if the input is a char*: the pointer is iterated until a '\0' character is reached (see null terminated string).


char b[]={"Mountain"};

b does not contain

  • {'M', 'o', 'u', 'n', 't', 'a', 'i', 'n'}

but instead

  • {'M', 'o', 'u', 'n', 't', 'a', 'i', 'n', '\0'} <- '\0'

making the iterating and stopping possible.

This also explains why the array size of b is 1 larger than the number of characters inside the word.

enter image description here


To add, you should not use these char pointers. They are dangerous and are long replaced by modern utilites like std::string.


now int a[]={1,2,3,4,5}; is OK but std::array<int, 5> a = {1,2,3,4,5}; is even better.

  • the types are unique (std::array<int, 4> != std::array<int, 5>)
  • it has a .size() function.
  • you can therefore pass it to other functions without having to add a size argument
  • it's as fast as a normal array

std::array can be used by including <array>.


If you ever go for something like int* a = new int[5]; than stop right there and instead use std::vector


Fianally never ever say use namespace std; (here why)

Stack Danny
  • 7,754
  • 2
  • 26
  • 55
  • Thank you for taking your time to respond to my question. I know it sounded silly, but I guess curiosity sometimes mixes with dumb! Could you please link me an alternative to different functions for strings that I can use that were previously available in C? (Like strtok,strcpy,strstr etc.) – mountainkingRune Apr 16 '19 at 14:53
  • for `strcpy` you simply say `std::string a = std::string b;` - [`std::istringstream`](https://en.cppreference.com/w/cpp/io/basic_istringstream) allows you to do alot of the other things (mostly not 1 line, though) like explained on [`strtok`](https://stackoverflow.com/questions/289347/using-strtok-with-a-stdstring) and also `strstr`. The best string manipulation gives you [`std::regex`](https://en.cppreference.com/w/cpp/regex) (which relies on `std::string`). In the end you can also always call `.c_str()` on a `std::string` to get the raw C-Style string. – Stack Danny Apr 16 '19 at 16:53
  • for simple replacing, finding `std::string` has `.replace()` and `.find()`. This *could* even be chained as in [this](https://stackoverflow.com/questions/27158812/does-this-code-from-the-c-programming-language-4th-edition-section-36-3-6-ha) post the questions example displays that. Though with care. – Stack Danny Apr 17 '19 at 10:46
  • @mountainkingRune Your question is still listed as unanswered. Doesn't Dannys answer answer your question? If it does, please accept the answer. – Ted Lyngmo May 15 '19 at 06:47
1

It all depends on how you interpret the parameters. cout << operator will consider (sometype*) as an address, but particularly char* as a string.

If you write a function taking your own parameters, you can interpret what ever the way you like.

In this problem, if you want to get the address, you can do it so

std::cout << static_cast<const void*>(b);
Tony
  • 632
  • 1
  • 4
  • 18
0

In C, strings are represented as a pointer to char. for this reason, when you pass a char* to an ostream (such as std::cout) in C++, it will interpret it as a null-terminated string and print that string's content rather than the address. If you want to print the address, you'll have to cast that pointer to a different kind:

std::cout << (void*)b;
Blaze
  • 16,736
  • 2
  • 25
  • 44
0

cout is an output stream. When we use output streams, and pass a char*, it treats it as a null terminated string (i.e. it prints all the characters till it find '\0') in the string. For any other pointer type, the address is printed.