Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning char array to pointer

I am trying to understand pointers in C++, but I am currently confused with the following:

char input_line[] = "hi?";
char* p;
p = &input_line;

while (*p)
{       
    cout << *p << endl;
    *p++;
}

I must be confused here because I would think this says assign the address of a 1d array to the pointer; however, when I try to compile this, I get an error: error: cannot convert char (*)[4]' to 'char*' in assignment p = &input_line;

It looks like I should be doing this:

const char input[] = "test?";
int quest_count = 0;
const char *i = input;

while(*i){
    cout << *i << endl;
    *i++;
}   

This doesn't make sense to me because you are assigning a char array to a char pointer which stores an address.

like image 513
4m4d3u5 Avatar asked Jun 12 '26 10:06

4m4d3u5


2 Answers

The error in this line of code:

p = &input_line;

Can be resolved by changing it with:

p = input_line;

That's because your're assigning the memory direction of the array. Its value is a pointer to a char pointer. That's why the error is raised. Remember, the operator & gives to you the variables memory direction.

A pointer stores a memory direction of an object. An array is a sequence of objects by a certain type that are located in consecutives reserved amount of space in memory.

Each index of an array is a number composed of the digits from 0 to 9. Each element of an array is an object that you can take the address of, like a pointer to an object memory location. In an array the objects are located in consecutive memory locations. When you assign an array to a pointer, you're assigning the pointer to the arrays first element, it's array[0].

When you increase by 1 the pointer value, the pointer will point to the next object in a memory location. So, arrays and pointers have similar behavior. If you assign to a pointer an array and then you increase by 1 the pointer value, it will point now to the object in the array.

This image is taken from the page below mentioned.

This is not only for char type, it's for every type in C++. In this page you can get more information about pointers and array. You must note that the pointer and the array must contain or point to the same variable type.

Here's an example from this page:

int* ptr;
int a[5];
ptr = &a[2];  // &a[2] is the address of third element of a[5].

An output example from the example in this page is:

Displaying address using arrays:

&arr[0] = 0x7fff5fbff880
&arr[1] = 0x7fff5fbff884
&arr[2] = 0x7fff5fbff888
&arr[3] = 0x7fff5fbff88c
&arr[4] = 0x7fff5fbff890

Displaying address using pointers:

 ptr + 0 = 0x7fff5fbff880
 ptr + 1 = 0x7fff5fbff884
 ptr + 2 = 0x7fff5fbff888
 ptr + 3 = 0x7fff5fbff88c
 ptr + 4 = 0x7fff5fbff890

As you can note in the output example, both are pointing to the same memory location, so you can access the objects from both methods.

Formally, in the C++ 11 standard is mentioned that:

Array-to-pointer conversion:


An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The result is a pointer to the first element of the array.

You can see those pages for more information about this theme:

C++ Pointer to an Array.

C++ Pointers and Arrays.

C++ Pointer to an Array.

C++11 Standard Library Extensions

like image 56
CryogenicNeo Avatar answered Jun 14 '26 01:06

CryogenicNeo


From the C++11 standard (emphasis mine):

An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The result is a pointer to the first element of the array.

This conversion, conversion of an array to a pointer to the first element of the array, is commonly know as "an array decays to a pointer to the first element of the array".

This conversion applies to all types, not just char.

int array_1[10] = {};
int* ptr1 = array_1;     // ptr1 is a pointer to the first element of array_1

double array_2[10] = {};
double* ptr2 = array_2;  // ptr2 is a pointer to the first element of array_2
like image 20
R Sahu Avatar answered Jun 13 '26 23:06

R Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!