Pointer to array of character in C++

Hello,

I want to declare a pointer to an array of characters in C++. How can I do it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
#include <iostream>
int main()
{
    char letter[100];

    //char *sentence[] = letter; ERROR

    //char* sentence[] = &letter; ERROR

    //char* sentence[] = *letter; ERROR

    return 0;
}

Create a pointer to the first element

char* sentence = &letter[0];
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
   char letter[100] = { "Hello World!" };

   char* sentence = letter;

   std::cout << sentence << '\n';
}
Create a pointer to the first element

char* sentence = &letter[0];

The name of an array by itself is treated as a pointer to the first element, no need for all the syntax sugar.
Thank you for your answers,

If we don't initialize letter[100], In the following example compiler allocated memory for the sentence?

How can I check if memory is allocated?

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
   char letter[100] ;

   char* sentence = letter;

   std::cout << sentence << '\n';
}
Yes, array decays to pointer implicitly.

Most commonly seen infunction parameters:
1
2
3
4
5
6
7
void func( int arr[] ); // syntactic sugar, an alias for:
// void func( int* arr );

int main() {
  int sample [42] {};
  func( sample ); // only address of first element passes to func
}


Note:
1
2
   char letter[100] = { "Hello World!" };
   std::cout << letter;

There is no ostream related operator<< that would operate on arrays, but there is one that takes const char*.


[edit]
If we don't initialize letter[100], In the following example compiler allocated memory for the sentence?

How can I check if memory is allocated?

The char letter[100] ; allocates memory for continuous block of 100 chars. The value of each char is uninitialized and therefore undefined.

The char* sentence = letter; creates a valid pointer to allocated memory. The pointer remains valid until the letter goes out of scope.

The std::cout << letter copies consecutive character values to output until it encounters null character. We have no idea where in memory starting from address of letter[0] there is null, nor how many unprintable values are before it.


The convention with pointers is that address value nullptr is considered "does not point to memory" and all other addresses have to be assumed valid. It is the responsibility of programmer to ensure that the pointer is truly valid.

Note:
1
2
char letter[100] ;
char* beyond = letter+100;

The 'beyond' points after the last element in the array. Out of range. It does not point to allocated memory, but can be used to test whether we have reached that far (when iterating the array).
Last edited on
If we don't initialize letter[100], In the following example compiler allocated memory for the sentence?

Then you have to deal with whatever garbage values are in the uninitialized C string. Always initialize your C string, even if to an empty state. char letter[100] = { };

For that matter your char pointer should be initialized, preferably to the same size or larger, as well. On the heap.

char* sentence = new char[100];

Of course now you have to use the C library string functions to muck around with the two C strings.

And remember to delete the C string on the heap when finished.

I prefer dealing with C++ strings in C++ programs. Much less messy and prone to problems than C strings.
you don't need an explicit pointer to do C on it.
sprintf(letter, "%s", "hello world"); //fine, works on the array even if sprintf supposedly wants a char*, because of array name to pointer syntax shortcut.
Topic archived. No new replies allowed.