将char数组分配给指针 [英] Assigning char array to pointer

查看:130
本文介绍了将char数组分配给指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解C ++中的指针,但目前我对以下内容感到困惑:

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

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

我在这里很困惑,因为我认为这是将一维数组的地址分配给指针;但是,当我尝试对此进行编译时,出现错误:error: cannot convert char (*)[4]' to 'char*' in assignment p = &input_line;

看来我应该这样做:

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

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

这对我来说没有意义,因为您正在将char 数组分配给存储地址的char 指针.

解决方案

此行代码中的错误:

p = &input_line;

可以通过以下方法解决:

p = input_line;

那是因为您要分配阵列的内存方向. 它的值是一个指向char指针的指针.这就是为什么错误是 提高.记住,运算符&给您变量存储 方向.

指针存储对象的存储方向. 数组是按某种类型的对象序列,它们位于内存中连续保留的空间量中.

数组的每个索引都是由0到9之间的数字组成的数字.数组的每个元素都是一个对象,您可以获取其地址,例如 pointer 到对象的存储位置.在数组中,对象位于连续的内存位置.当您将 array 分配给 pointers 时,就是将指针分配给 arrays 的第一个元素,即array[0].

当您将 pointer 的值增加1时, pointer 将指向内存位置中的下一个对象.因此,数组和指针具有类似的行为.如果您将指针分配给数组,然后将指针值增加1,它将立即指向数组中的对象.

这不仅适用于char类型,还适用于C ++中的每种类型. 在此页面中,您可以获得有关指针和数组的更多信息. 您必须注意,指针和数组必须包含或指向相同的变量类型.

以下是此页面中的示例:

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

此页面中的示例的输出示例为:

使用数组显示地址:

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

使用指针显示地址:

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

正如您在输出示例中注意到的那样,两者都指向相同的内存位置,因此您可以从这两种方法中访问对象.

C ++ 11标准中,通常提到:

数组到指针的转换:

类型为"N T"的"array"或类型为"array"的左值或右值 可以将T的未知范围转换为指针类型的prvalue 到T".结果是指向array第一个元素的指针.

您可以查看这些页面以获取有关此主题的更多信息:

指向数组的C ++指针.

C ++指针和数组.

指向数组的C ++指针.

C ++ 11标准库扩展

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.

解决方案

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 a object. An array is a sequence of object by certain type that are located in consecutives reserved amount of space in memory.

Each index of an array is a number composed by 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 memory location. So, arrays and pointers have a 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 array.

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

这篇关于将char数组分配给指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆