586
General Discussion / Re: if self.isCoder(): post() #Programming Thread
« on: September 26, 2015, 04:50:27 am »
Regarding const in C++ you need to keep in mind how it modifies pointer types:
const int* is a pointer-to-const-int, that is you can change what memory address the variable points to but you can't change the value of what is being pointed at.
int* const is a const-pointer-to-int, that is you can change the value of what is being pointed at but not what address the variable points to.
const int* const naturally is a const-pointer-to-const-int which prevents both changing the address being pointed to and the value of what is being pointed at.
My way of remembering this is that the const modifier modifies what is closest to the const. You also have references-to-const (for example: const Person& p) which is used when you want to avoid copying a large data structure into a parameter but also don't want that structure to be modified in the function.
As for two-dimensional arrays in C, basically you have one-dimensional arrays which are of type T*. Then you want a one-dimensional array of one-dimensional arrays, so you want T**. As for your specific example you want
const int* is a pointer-to-const-int, that is you can change what memory address the variable points to but you can't change the value of what is being pointed at.
int* const is a const-pointer-to-int, that is you can change the value of what is being pointed at but not what address the variable points to.
const int* const naturally is a const-pointer-to-const-int which prevents both changing the address being pointed to and the value of what is being pointed at.
My way of remembering this is that the const modifier modifies what is closest to the const. You also have references-to-const (for example: const Person& p) which is used when you want to avoid copying a large data structure into a parameter but also don't want that structure to be modified in the function.
As for two-dimensional arrays in C, basically you have one-dimensional arrays which are of type T*. Then you want a one-dimensional array of one-dimensional arrays, so you want T**. As for your specific example you want
Code: [Select]
float** A = (float**)malloc(4*sizeof(float*));because A is an array of pointers to float as I stated.[/code]