简体   繁体   English

char数组及其指针如何在c ++中完全正常工作?

[英]How do char arrays and their pointers work in c++ exactly?

I am a beginner student in c++ and there is one thing I cannot understand when working with character arrays: So, I know that pointers are essentially variables that "point" to the memory address of another variable, and that the name of an array(Ex: int a[20]) is a constant pointer to the values in that array. 我是c ++的初学者,在使用字符数组时有一件事我无法理解:所以,我知道指针本质上是指向另一个变量的内存地址的变量,以及数组的名称(例如:int a [20])是指向该数组中值的常量指针。 When working with different numeric types(int, float etc.) if we output through a message the name of that array it shows the address of the first element, but if we do the same with a char type, it doesn't show the address, but the value of the variable. 当使用不同的数字类型(int,float等)时,如果我们通过消息输出该数组的名称,它将显示第一个元素的地址,但如果我们对char类型执行相同操作,则它不会显示地址,但变量的值。 Example: 例:

#include <iostream>

using namespace std;

int main()
{int a[]={1,2,3,4,5};
cout<<a<<endl; //through output, it shows the memory address of the first 
element of the array;
char b[]={"Mountain"};
cout<<b; //It outputs the word "Mountain"
    return 0;
}

Is the pointer from a char array automatically converted to its value when you output it? 输出时,来自char数组的指针是否自动转换为其值?

There is no difference. 没有区别。 char pointers aren't somehow magically different than int pointers. char指针与int指针在某种程度上并不神奇。 So what's going on, than? 那是怎么回事呢?

std::cout << (or the older printf() ) have overloads for char* . std::cout << (或旧的printf() )具有char*重载。 Meaning that the functions behave differently if the input is a char* : the pointer is iterated until a '\\0' character is reached (see null terminated string ). 这意味着如果输入是char* ,函数的行为会有所不同:指针会被迭代,直到达到'\\0'字符(请参见null终止字符串 )。


char b[]={"Mountain"};

b does not contain b 不含

  • {'M', 'o', 'u', 'n', 't', 'a', 'i', 'n'}

but instead 但反而

  • {'M', 'o', 'u', 'n', 't', 'a', 'i', 'n', '\\0'} <- '\\0' {'M', 'o', 'u', 'n', 't', 'a', 'i', 'n', '\\0'} < - '\\0'

making the iterating and stopping possible. 使迭代和停止成为可能。

This also explains why the array size of b is 1 larger than the number of characters inside the word. 这也解释了为什么b的数组大小比单词内的字符数大1

在此输入图像描述


To add, you should not use these char pointers. 要添加,你应该使用这些字符指针。 They are dangerous and are long replaced by modern utilites like std::string . 它们很危险,很久以来就被现代的实用工具所取代,比如std::string


now int a[]={1,2,3,4,5}; now int a[]={1,2,3,4,5}; is OK but std::array<int, 5> a = {1,2,3,4,5}; 没问题,但是std::array<int, 5> a = {1,2,3,4,5}; is even better. 甚至更好。

  • the types are unique ( std::array<int, 4> != std::array<int, 5> ) 类型是唯一的( std::array<int, 4> != std::array<int, 5>
  • it has a .size() function. 它有一个.size()函数。
  • you can therefore pass it to other functions without having to add a size argument 因此,您可以将其传递给其他函数,而无需添加大小参数
  • it's as fast as a normal array 它和普通数组一样快

std::array can be used by including <array> . std::array可以通过包含<array>来使用。


If you ever go for something like int* a = new int[5]; 如果你去过像int* a = new int[5]; than stop right there and instead use std::vector 而不是停在那里,而是使用std::vector


Fianally never ever say use namespace std; Fianally永远不会说use namespace std; ( here why ) 这里为什么

It all depends on how you interpret the parameters. 这一切都取决于你如何解释参数。 cout << operator will consider (sometype*) as an address, but particularly char* as a string. cout <<运算符会将(sometype *)视为一个地址,但特别是char *作为字符串。

If you write a function taking your own parameters, you can interpret what ever the way you like. 如果您使用自己的参数编写函数,则可以解释您喜欢的方式。

In this problem, if you want to get the address, you can do it so 在这个问题中,如果你想获得地址,你可以这样做

std::cout << static_cast<const void*>(b);

In C, strings are represented as a pointer to char . 在C中,字符串表示为char的指针。 for this reason, when you pass a char* to an ostream (such as std::cout ) in C++, it will interpret it as a null-terminated string and print that string's content rather than the address. 因此,当您在C ++中将char*传递给ostream (例如std::cout )时,它会将其解释为以null结尾的字符串并打印该字符串的内容而不是地址。 If you want to print the address, you'll have to cast that pointer to a different kind: 如果要打印地址,则必须将该指针强制转换为其他类型:

std::cout << (void*)b;

cout is an output stream. cout是输出流。 When we use output streams, and pass a char* , it treats it as a null terminated string (ie it prints all the characters till it find '\\0') in the string. 当我们使用输出流并传递一个char* ,它将它视为一个空终止字符串(即它在字符串中打印所有字符,直到找到'\\ 0')。 For any other pointer type, the address is printed. 对于任何其他指针类型,将打印地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM