user2991252
user2991252

Reputation: 788

Pointers and arrays - c++

I cannot figure out why the following works in c++

 int main() {
  function("hello world");
     return 0;
 }

 void function(char *ch) {
  cout << ch;
 }

How come the pointer points to the whole character-string? I thougt It would be pointed to the first element in the array.

Upvotes: 0

Views: 99

Answers (5)

Theolodis
Theolodis

Reputation: 5102

"hello world" is an array of characters that you could declare as:

char* test = "hello world";

what behaves similar to:

char test[] = {"hello world"};

arrays in C can be interpreted as pointers, and pointer can point to arrays.

I would recommend you some literature about C pointer.

Upvotes: -2

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16017

Apart from the problem that a string literal in C++ is const:

What you see is the normal way arrays in C, and by association in C++, are passed and handled. In most cases an array "decays" to a pointer to its first element when it is passed or used in an expression. So yes, you are right: ch is a pointer to the first char. And all C/C++ routines around char arrays, a.k.a. C strings, rely on the string being 0 terminated. Somewhere in the valid memory area starting at the adress given, a byte with the value zero must indicate that the string logically ends here. For example operator<< will stop copying characters from that memory to cout when it encounters a zero byte. Functions like strcat() just overwrite that zero byte and append a new one where the new, longer string now "ends".

The background is that C/C++ arrays do not carry any "ancillary information". There is not any data associated with them beyond the bare sequence of their elements in memory.

As an aside, for types that do not have "invalid" values (like 0 for a char) which could serve as an end marker, there is no way a function can find the end of an array passed to it, because the function gets only a pointer to the first element. All such functions need a second parameter indicating the array's length. This primitive array handling (which is, of course, extremely fast and trivially translatable in any machine code) is very different from languages with a safer and more abstract type system like Ada or C#, where an array carries information about its length.

Upvotes: 1

Gil Elad
Gil Elad

Reputation: 409

char* is a type. Its value is a memory address - a unit of machine memory. The value contained in this byte of memory is a character. There is more memory beyond this address, which contains more characters, sequentially.

Telling cout to output char* (as opposed to just char) tells it to output not just the direct memory address it points to, but everything beyond it as well, up until the value \0

Upvotes: 2

Mike Seymour
Mike Seymour

Reputation: 254431

How come the pointer points to the whole character-string?

Because the string is an array - each character is in the memory location immediately after the previous one. So a pointer to the first character can also be treated as a pointer to the whole string.

I thougt It would be pointed to the first element in the array.

It does. But you can apply pointer arithmetic to it (e.g. ch[2]) to access the other elements. This is what << does when you give it a pointer to a character: it assumes that it points to a zero-terminated array, and prints all the characters it finds until it reaches the terminator.

Note that, in modern C++, this shouldn't compile - the string literal is constant, so it should only be convertible to const char *, not char *. Older compilers will allow the conversion, but should give a warning that it was deprecated. You should always enable, and fix, compiler warnings.

Upvotes: 1

HelloWorld123456789
HelloWorld123456789

Reputation: 5359

You're right. The pointer points to the first element in the array. But then again, since ch is of type char * and you try to cout ch, it prints till it finds a \0 in memory.

Upvotes: 1

Related Questions