3

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.
Nimitz14
  • 2,138
  • 5
  • 23
  • 39

4 Answers4

4

Regarding your first question, a string literal (like e.g. "A+") is an (read-only) array of characters, and as all arrays they can decay to pointers to their first element, i.e. 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 (i.e. inside a function) then variable will be shared between invocations of the function, i.e. 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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • But shouldn't then letters[0] then just be 'A' and not the full "A+", since a pointer would just look at the first element of { 'A','+','\0'} ? I know this doesn't happen, but I do not understand why not. – Nimitz14 Nov 18 '15 at 10:36
  • @Nimitz14 `letters` is an array *of pointers* to `char`, not an array of `char`. That makes all the different. `letters[0]` is a *pointer* to the string literal `"A+"`. It's true that the pointer is a pointer to the first character, but its a pointer to the whole string. To get the first letter of the first element you need to do `letters[0][0]`. I think you need to find a book or tutorial on pointers and arrays, how they work and how to use them. – Some programmer dude Nov 18 '15 at 10:42
3

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.

Community
  • 1
  • 1
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • This code must be inside a function (due to the for loop), so `static` has a different meaning. – interjay Nov 18 '15 at 10:15
  • But shouldn't then letters[0] then just be 'A' and not the full "A+", since a pointer would just look at the first element of { 'A','+','\0'} ? I know this doesn't happen, but I do not understand why not. – Nimitz14 Nov 18 '15 at 10:37
  • @Nimitz14: It is same as with C string. How to you declare string in C? char * str = "test" right? It is similar here. You have array of strings just – Giorgi Moniava Nov 18 '15 at 10:40
2

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)

rcgldr
  • 27,407
  • 3
  • 36
  • 61
2
  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

marcinj
  • 48,511
  • 9
  • 79
  • 100