To figure out pointers, I thought of the * in C as having three proper usages:
1 - It may be used for multiplication, like here:
Or here:
2 - It may be used to declare a pointer type variable, like these:
Or here:
3 - It may be used to dereference pointers, like here:
To make it quite clear which of the three ways I'm using the * character, I have a different way of putting spaces around it in each case.
When using it the first way, for multiplication, there is a space before and after it, " * ".
When using it the second way, to declare a pointer type, I put a space after but not before it, "* ".
When using it the third way, to dereference a pointer variable, I put a space before but not after it, " *".
Am I correct so far in thinking of the * as being used in three different ways? I believe I am, as I've been writing functional code with all that preceded for the last 2 years now.
I've decided that I'd like to try my hand at making a C IDE for iOS, partially because I'm not content with them, and partially because I suspect that in doing so, I'll master the C language.
So, having said that, I've run into this funky line in C...
This line declares a pointer, named ptr, to an array of characters.
I'm not sure what to make of the parenthesizes, though. They're separating the * from both char and [], making it seem like my pointer variable is declared as neither a char nor a [], if that makes any sense.
This couldn't be rewritten as:
Because that would make an array of char pointers rather than a single pointer to a char array.
If I try to write it as:
Except, well, no, because that doesn't compile.
While I'm on this topic... why are brackets places after the identifier of the variable instead of after the type? When I write something like
Can be easily read as "an int pointer named ptr"... note that the symbols/keywords come in the same order as the nature English.
But if I want an array like this:
it'd be most naturally read in English as "an int array named arr", but if I construe my English to have the same order as the symbols/keywords, I end up with "an int variable named arr which is actually an array".
I don't care about the array complaint as much as the pointer confusion... which actually confuses me.
Edit:
Somehow, I completely forgot about the rabbit hole that is C function pointers:
http://stackoverflow.com/questions/13247473/figuring-out-c-declarations
Here's an explanation of how to read them aloud:
http://c-faq.com/decl/spiral.anderson.html
So... now I'm feeling a bit more lost than before...
I'm going to go sleep on this now, I guess...
1 - It may be used for multiplication, like here:
Code:
product = factor1 * factor2;
Or here:
Code:
runningProduct *= factor;
2 - It may be used to declare a pointer type variable, like these:
Code:
char* charPtr;
int* intPtr;
float* floatPtr;
double* doublePtr;
void* voidPtr;
Or here:
Code:
typedef struct {
int a;
int b;
} MyStruct;
MyStruct* myStructPtr;
3 - It may be used to dereference pointers, like here:
Code:
*intPtr = 3; // Sets the value at the address intPtr points to to 3.
int localInt = *intPtr; // localInt now holds the value at the address intPtr point to - which we know to be 3 in this example.
To make it quite clear which of the three ways I'm using the * character, I have a different way of putting spaces around it in each case.
When using it the first way, for multiplication, there is a space before and after it, " * ".
When using it the second way, to declare a pointer type, I put a space after but not before it, "* ".
When using it the third way, to dereference a pointer variable, I put a space before but not after it, " *".
Am I correct so far in thinking of the * as being used in three different ways? I believe I am, as I've been writing functional code with all that preceded for the last 2 years now.
I've decided that I'd like to try my hand at making a C IDE for iOS, partially because I'm not content with them, and partially because I suspect that in doing so, I'll master the C language.
So, having said that, I've run into this funky line in C...
This line declares a pointer, named ptr, to an array of characters.
Code:
char(* ptr)[];
I'm not sure what to make of the parenthesizes, though. They're separating the * from both char and [], making it seem like my pointer variable is declared as neither a char nor a [], if that makes any sense.
This couldn't be rewritten as:
Code:
char* ptr[];
Because that would make an array of char pointers rather than a single pointer to a char array.
If I try to write it as:
Code:
char(char* ptr)[];
Except, well, no, because that doesn't compile.
While I'm on this topic... why are brackets places after the identifier of the variable instead of after the type? When I write something like
Code:
int* ptr;
Can be easily read as "an int pointer named ptr"... note that the symbols/keywords come in the same order as the nature English.
But if I want an array like this:
Code:
int arr[];
it'd be most naturally read in English as "an int array named arr", but if I construe my English to have the same order as the symbols/keywords, I end up with "an int variable named arr which is actually an array".
I don't care about the array complaint as much as the pointer confusion... which actually confuses me.
Edit:
Somehow, I completely forgot about the rabbit hole that is C function pointers:
http://stackoverflow.com/questions/13247473/figuring-out-c-declarations
Here's an explanation of how to read them aloud:
http://c-faq.com/decl/spiral.anderson.html
So... now I'm feeling a bit more lost than before...
I'm going to go sleep on this now, I guess...
Last edited: