Help me - C++
-
Differentiate between: (elaborate with diagrams) a. const int *ptr; b. int const *ptr;
-
Differentiate between: (elaborate with diagrams) a. const int *ptr; b. int const *ptr;
Did you try
cdecl
? const int * p; int const int * p; Diagrams are left as exercise... -
Differentiate between: (elaborate with diagrams) a. const int *ptr; b. int const *ptr;
-
Differentiate between: (elaborate with diagrams) a. const int *ptr; b. int const *ptr;
So lazy you couldn't even put your homework into your own words? Just google it. But first, refer to the material your professor provided in class.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
-
Differentiate between: (elaborate with diagrams) a. const int *ptr; b. int const *ptr;
They are equivalent. The int value is constant, cannot be modified. Now that you decalre a pointer to this type, this pointer can be set to any constant int value, but the pointer itself may be moved around freely among several constant integer values. The difference comes when you move 'const' to the right of the asterisk: int *const ptr = &xxx; Now the xxx value may change, but ptr will always point to xxx. You are not allowed to move ptr to &yyy. You can consider *ptr as another way of writing xxx. Or ptr is another way of writing &xxx. You can't move xxx to refer to another variable either, so *ptr and xxx, or ptr and &xxx work the same way. You rarely need constant pointers; the address it is initialized with will serve the same purpose. But if the address expression is complex, and it is used many times, setting up a constant pointer may save both typing and improve readability (as long as a more descriptive name than "ptr" is chosen :-)).
-
So lazy you couldn't even put your homework into your own words? Just google it. But first, refer to the material your professor provided in class.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
It sure may be homework, but then again: If different phrasings are both accepted by the compiler, it is legitimate to ask if they are identical, even if it is not homework :-). If "const int *ptr" is identical to "int const *ptr", is it then identical to "int *const ptr" as well? If you can shift the "const" keyword one position, why not two? There are several cases (in various languages) of modifiers that can be written in any order (when there are more than one). Knowing when order/position is significant and when it isn't (and which orders/positions are illegal) can be quite confusing until you have built expertise in the language!
-
They are equivalent. The int value is constant, cannot be modified. Now that you decalre a pointer to this type, this pointer can be set to any constant int value, but the pointer itself may be moved around freely among several constant integer values. The difference comes when you move 'const' to the right of the asterisk: int *const ptr = &xxx; Now the xxx value may change, but ptr will always point to xxx. You are not allowed to move ptr to &yyy. You can consider *ptr as another way of writing xxx. Or ptr is another way of writing &xxx. You can't move xxx to refer to another variable either, so *ptr and xxx, or ptr and &xxx work the same way. You rarely need constant pointers; the address it is initialized with will serve the same purpose. But if the address expression is complex, and it is used many times, setting up a constant pointer may save both typing and improve readability (as long as a more descriptive name than "ptr" is chosen :-)).