Factsheet
Since Iโve started exploring C, Iโve realized that many programming languages rely on libraries built using C โbindings.โ I know C is fast and simple, so why donโt people just stick to using and improving C instead of creating new languages every couple of years?
I am currently working on an assignment in C where we are required to make a stack by simply using pointers.
I know the line: int *ptr = &val; declares ptr to be a "pointer to"(which is my interpretation of what asterisk * means in C) the "address of" the integer variable val.
When I want to create a double pointer, or a pointer to a pointer, I do so like:
int **ptr_ptr = &ptr; By setting ptr_ptr to a "pointer to" the address of pointer ptr.
When we use the asterisk anywhere other than in a declaration, it is usually referred to as dereferencing that pointer (I think), and grabbing the value that the pointer actually points to. This goes against my intuition that an asterisk means "pointer to".
Could anybody explain the proper meaning of the asterisk in C? Is it just that it means different things depending on how it is used (i.e. in a declaration versus anywhere else)?
Thanks!
As a european who learned english from speaking to and watching youtube videos of british and australian people online I always wondered why americans were so offended by a word that doesn't mean anything more "offensive" than calling someone a dick or an asshole. Especially considering how normalized the word is in australian or even scottish english.
Is there any particular reason for it?
In this case, double means a variable of type double.
double* means a pointer to a double variable.
double** means a pointer to a pointer to a double variable.
In the case of the function you posted, it is used to create a sort of two-dimensional array of doubles. That is, a pointer to an array of double pointers, and each of those pointers points to an array of pointers.
Here's a more general answer: the spiral rule. For this particular example:
+-----------+
|+--------+ |
|| +---+ | |
|| ^ | | |
double** foo; | | |
^ |^ | | |
| |+------+ | |
| +---------+ |
+--------------+
Read this as "\"foo\" is a pointer to a pointer to a double."
In this case, each pointer is semantically an array, so this represents a 2D array of type double.