简体   繁体   English

CPP中的指针,关于示例程序的混淆

[英]Pointers in CPP, confusion regarding an example program

Working my way through accelerated c++. 通过加速c ++工作。 There's an example where there are multiple things that I do not understand. 有一个例子,有很多我不理解的东西。

double grade = 88;

static const double numbers[] = { 97,94,90,87,84,80,77,74,70,60,0 };

static const char* const letters[] = { "A+","A","A-","B+","B","B-","C+","C","C-","D","F" };

static const size_t ngrades = sizeof(numbers) / sizeof(*numbers);

for (size_t i = 0;i < ngrades;++i) {
    if (grade >= numbers[i]) {
        cout << letters[i];
        break;
    }
}
  1. I don't understand what's going on with static const char* const letters[] = (...) . 我不明白static const char* const letters[] = (...) First of all, I always thought a char was a single character delimited by '. 首先,我一直认为char是由'分隔的单个字符。 Single or more characters delimited by " are for me a string. 由“对我来说是一个字符串”的单个或多个字符分隔。
  2. The way I've understood pointers is that they're a value that represents the address of an object, although this would be initialized as int* p=&x; 我理解指针的方式是它们是一个表示对象地址的值,尽管这可以初始化为int* p=&x; . They have the advantage of being able to be used like an iterator (kind of). 它们的优点是能够像迭代器一样使用(种类)。 But I really do not get what is going on here, we declare a pointer letters that gets assigned to it an array of values (not addresses), what does that mean? 但我真的不知道这里发生了什么,我们声明一个指针字母,它被分配给一个值数组(而不是地址),这是什么意思? What would be a reason for doing this? 这样做的原因是什么?
  3. I know what static is in java, is the meaning similar in CPP? 我知道java中的static是什么,CPP中的含义是否相似? The author writes it means that the compiler will initialize the static values only once. 作者写道,这意味着编译器只会初始化一次静态值。 But isn't that done with every variable within a certain scope? 但是,在某个范围内的每个变量都没有完成吗? I've noticed in debug that I seem to skip over the values after having executed it the first time. 我在调试中注意到,我第一次执行它后似乎跳过了值。 But that would imply that even after my program is finished running these static values are still saved? 但这意味着即使在我的程序运行完毕后,这些静态值仍然会被保存? That doesn't seem logical to me. 这对我来说似乎不合逻辑。

Regarding your first question, a string literal (like eg "A+" ) is an (read-only) array of characters, and as all arrays they can decay to pointers to their first element, ie a pointer to char . 关于你的第一个问题,字符串文字(例如"A+" )是一个(只读)字符数组,并且作为所有数组,它们可以衰减到指向其第一个元素的指针,即指向char的指针。 The variable letters is an array of constant pointers (the pointer in the array can't be changed) to characters that are constant. 变量letters是一个常量指针数组(数组中的指针不能更改)到常量字符。

For the third questions, what static means is different depending on which scope you declare the variable in. It's a linkage specifier when used in the global scope, and means that the variable (or function) will not be exported from the translation unit . 对于第三个问题, static方法的不同取决于您声明变量的范围。它是在全局范围中使用时的链接说明符,意味着变量(或函数)不会从转换单元导出。 If use for a variable in local scope (ie inside a function) then variable will be shared between invocations of the function, ie all calls to the function will have the same variable with the the value of it being kept between calls. 如果在本地范围内(即在函数内部)使用变量,那么变量将在函数的调用之间共享,即对函数的所有调用将具有相同的变量,并且在调用之间保持它的值。 Declaring a class-member as static means that it's shared between all object instances of the class. 将类成员声明为static意味着它在类的所有对象实例之间共享。

1) Here 1)在这里

static const char* const letters[] = (...)

letters is actually array of const pointers to const characters. letters实际上是const字符的const指针数组。 Hence the "". 因此,“”。

2) Like said, above variable is array of pointers. 2)如上所述,上面的变量是指针数组。 So each element in the array holds address to the string literal defined in the array. 因此,数组中的每个元素都将地址保存到数组中定义的字符串文字。 So letters[0] holds address of memory where "A+" is stored. 因此, letters[0]保存存储“A +”的存储器的地址。

3) static has various uses in C++. 3) static在C ++中有各种用途。 In your case if its declared inside function, its value is preserved between successive calls to that function.. More details . 在你的情况下,如果它声明了内部函数,它的值将在对该函数的连续调用之间保留。更多细节

static const char* const letters[] static const char * const letters []

This is an array of pointers to characters. 这是一个指向字符的数组。 In this case the initializer list sets each pointer to the first character of each of the strings specified in the initializer list. 在这种情况下,初始化列表将每个指针设置为初始化列表中指定的每个字符串的第一个字符。

const ... const const ... const

The pointers and the characters the pointers point to are constants. 指针和指针指向的字符是常量。

static 静态的

If declared inside a function, similar to a global, but with local scope. 如果在函数内部声明,类似于全局,但具有局部范围。

Links: 链接:

http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx

http://en.wikipedia.org/wiki/Static_(keyword) http://en.wikipedia.org/wiki/Static_(keyword)

  1. Those are not characters but strings, so your understanding is correct. 那些不是字符而是字符串,所以你的理解是正确的。 Letters here are not stored as char but in the form of const char*. 这里的字母不存储为char,而是以const char *的形式存储。

  2. we declare a pointer letters that gets assigned to it an array of values (not addresses) 我们声明一个指针字母,它被分配给一个值数组(不是地址)

those are not pointer letters but literal strings, "a" is a literal, its type is const char[], which decays to const char* - which means its a poitner. 那些不是指针字母而是文字字符串,“a”是文字,它的类型是const char [],它衰变为const char * - 这意味着它是一个poitner。

3. 3。

I know what static is in java, is the meaning similar in CPP 我知道java中的static是什么,CPP中的含义是相似的

in general yes, but there are differences - like you cant use static inside functions in java while you can in c++, also you can have global static variables in c++. 一般来说是的,但是有区别 - 就像你不能在java中使用静态内部函数而你可以在c ++中,你也可以在c ++中使用全局静态变量。

The author writes it means that the compiler will initialize the static values only once. 作者写道,这意味着编译器只会初始化一次静态值。 But isn't that done with every variable within a certain scope? 但是,在某个范围内的每个变量都没有完成吗?

non static variables will be created on stack and default initialized (if no explicit initialization is done) on each function run. 非静态变量将在堆栈上创建,并在每个函数运行时默认初始化(如果没有进行显式初始化)。 static variables on the other hand will be initialized on the first run only. 另一方面,静态变量将仅在第一次运行时初始化。

But that would imply that even after my program is finished running these static values are still saved? 但这意味着即使在我的程序运行完毕后,这些静态值仍然会被保存?

thats not true, after program is done ,they are freed - your process is dead then 这不是真的,在程序完成后,它们被释放 - 你的过程就已经死了

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

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