简体   繁体   中英

Pointers in CPP, confusion regarding an example program

Working my way through accelerated 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[] = (...) . First of all, I always thought a char was a single character delimited by '. 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; . 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? 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 . The variable letters is an array of constant pointers (the pointer in the array can't be changed) to characters that are constant.

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 . 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.

1) Here

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

letters is actually array of const pointers to const characters. Hence the "".

2) Like said, above variable is array of pointers. 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.

3) static has various uses in 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[]

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

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://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*.

  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.

3.

I know what static is in java, is the meaning similar in 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++.

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

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