简体   繁体   中英

char, pointer, cast and string questions

/*1*/ const char *const letter = 'A';

/*2*/ const char *const letter = "Stack Overflow";

Why is 1 invalid but 2 valid?

letter is a pointer that needs to be assigned an address. Are quoted strings addresses? I'm assuming this is why #2 is valid and that single quoted strings are not considered addresses?

Also, what is the difference between these two casting types?:

static_cast<> and () .

And lastly, if var is a char variable, why does cout << &var << come out garbled? Why must I cast it to void*?

Thanks you for your patience with beginner questions.

Because 'A' is not a pointer, it's a char , 65 or 41 16 if the underlying character set is ASCII.

"Stack" , on the other hand, is string, basically the character array {'S', 't', 'a', 'c', 'k', '\\0'} , which degrades to a pointer to its first character.

Your "difference between static_cast and () " has been answered here , far better than I could.

The reason why you get rubbish with a char var = 'x'; cout << &var ... char var = 'x'; cout << &var ... is because &var is a char * which means it's being treated as a string - in that case, cout outputs characters up to the final nul character \\0 which isn't there, or is there beyond the character. The following code shows this:

#include <iostream>
int main() {
    //int q1 = 0;
    char xx = 'x';
    //int q2 = 0;
    std::cout << &xx << std::endl;
    return 0;
}

outputting:

x~Í"

When you uncomment the q lines, it works, because it's putting zeros around the character, outputting x on its own). Keep in mind this is not kosher C, it's only working because of the way my stack is organised. Don't use this is real code.

First one is just a char type that has the value A .. Also there is no single quoted strings but just single quoted char..

While the second is an array of characters and thus it works..

char letter [] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char letter [] = "Hello"; 

Both specify the same means of initialization for letter .

对于您的第二个问题,此链接将非常有用。

In this case, 'A' is just a value (probably '65'). Whenever C sees something in single quotes, it assumes it's dealing with a single character. Thus, you're essentially saying that the pointer 'letter' is going to point to whatever is at memory address 65. . . probably not what you're going for, so the compiler flags it as an error.

On the other hand, when you assign a string ("A"), the compiler does some magic behind the scenes to make it work. It takes that string and puts it somewhere into your executable, then every time you use that particular string, it substitutes the memory address of the string it knows about for the literal you've put into the program.

Also, static_cast(value) is preferred over (type)value because static_cast<>() tends to be a little more restrictive in what it'll do (so more errors are caught at compile-time). It's also more verbose (for example, there's const_cast(value) which will convert a value from a const (immutable) to something that can be modified by your code.

HTH

You can see a char as an integer type (as int but shorter), so when you write char l = 'A'; , you just sign the numerical ASCII value of A in the variable l .

When you write char* letters = "foo" , the resulting thing would be similar to the following

char letters[4];
letters[0] = 'f';
...
letters[3] = '\0';

One way I usually follow is to use the typeid operator to understand the types I am dealing with.

Thoug the type_info::name returns an implementation defined string, it is usually many times quiet self explanatory to understand what we are dealing with. The output below is shown when compiled with gcc

#include <iostream>
#include <typeinfo>
using namespace std;

int main(){
   cout << typeid('A').name() << endl;                      // prints 'c' meaning char
   cout << typeid("Stack Overflow").name() << endl;         // prints 'A15_c' meaning
                                                            // array of 15 characters
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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