简体   繁体   English

如何获取C ++ Char数据类型的地址

[英]How to get address of C++ Char Datatype

In the lesson I'm going through it uses this to record the address of a char value... 在本课程中,我将使用它来记录char值的地址...

char givenChar;
std::cout<<"character = \n";
std::cin>>givenChar;

std::cout<< "address character = " << (void *) &givenChar<<"\n\n";

But it does not explain at all what is happening here to get address character = 0x7ffd812a9257 . 但是,它根本没有解释这里得到address character = 0x7ffd812a9257

what is the (void *) called and what is it doing? (void *)叫什么,它在做什么?

In C++ operators work based on the types of their operands. 在C ++中,运算符根据其操作数的类型进行工作。 Here the behavior of the << operator depends on [the type of] its right hand side operand. <<操作符的行为在此取决于其右侧操作数的类型。

For example, if you want to print out an integer with cout << 1 , the string 1 will be printed. 例如,如果要打印出cout << 1的整数,则将打印字符串1 If on the other hand the operand is a pointer then the output will be in hexadecimal and will have a 0x prefix. 另一方面,如果操作数是指针,则输出将为十六进制,并且前缀为0x

If the operand is a char pointer ( &givenChar ), the behavior is also different, the operator will print the characters starting from that address until the first zero byte. 如果操作数是char指针( &givenChar ),则行为也不同,操作员将打印从该地址开始直到第一个零字节的字符。

If you want to print the address of a character, you need to have a void pointer to achieve that. 如果要打印字符的地址,则需要一个空指针来实现。 To have a void pointer, you need to cast the char *: (void *) givenChar . 要具有空指针,您需要(void *) givenChar char *:( (void *) givenChar

To stuff something into an output-stream ( std::ostream ) like std::cout , the stream insertion operator << is used: 要将东西塞入输出流( std::ostream ),如std::cout ,将使用流插入运算符<<

std::cout << "Hello, World!";

The stream insertion operator that is called for string literals like "Hello, World" looks like "Hello, World"这样的字符串文字被调用的流插入运算符看起来像

std::ostream& operator<<(std::ostream& os, const char* s);

As you can see, the 2nd parameter is a pointer to const char . 如您所见,第二个参数是指向const char的指针。 Now if you would write 现在如果你写

char givenChar;
std::cout << &givenChar;

the address-of operator & would give you the address of givenChar . 运算符的地址&会给你给定的givenChar地址。 The type of this address is char* which is convertible into a char const* . 此地址的类型为char* ,可以转换为char const* So the above mentioned function 所以上面提到的功能

std::ostream& operator<<(std::ostream& os, const char* s);

would be called (like operator<<(std::cout, &givenChar) ) which would interpret the memory at the location of the address of givenChar as a zero-terminated string. 将会被调用(例如operator<<(std::cout, &givenChar) ),它将给定givenChar地址处的内存解释为一个以零结尾的字符串。 Eg. 例如。 it would read from the memory until it finds a '\\0' . 它会从内存中读取数据,直到找到'\\0'为止。 But at the address of givenChar is only space for *one* char which most likely is not zero. 但是在给定givenChar的地址上只有* one * char空间,最有可能不是零。 This would result in garbage inserted into std::cout (=printed) and eventually lead to an access violation. 这将导致将垃圾插入std::cout (= printed)中,并最终导致访问冲突。

So instead you use 所以你用

char givenChar;
std::cout << (void*) &givenChar;

(void*) is a cast. (void*)是演员表。 It converts the char* produced by applying the address-of operator & to the char givenChar into a pointer to void . 其转换的char*通过将操作者地址的产生&char givenChar成指针void For a void* the operator void*运算符

ostream& operator<<(void* val);

gets called which will only insert the numeric value of the given address into the stream instead of trying to print a string that might exist at the address. 被调用,它将仅将给定地址的数值插入流中,而不是尝试打印该地址处可能存在的字符串。

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

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