Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

c++ - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...