|
|
#1 |
|
* Confusion in C
To figure out pointers, I thought of the * in C as having three proper usages:
1 - It may be used for multiplication, like here: Code:
product = factor1 * factor2; Code:
runningProduct *= factor; Code:
char* charPtr; int* intPtr; float* floatPtr; double* doublePtr; void* voidPtr; Code:
typedef struct {
int a;
int b;
} MyStruct;
MyStruct* myStructPtr;
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. 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)[]; This couldn't be rewritten as: Code:
char* ptr[]; If I try to write it as: Code:
char(char* ptr)[]; 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; But if I want an array like this: Code:
int arr[]; 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/1...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...
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed. Like it on Facebook! Last edited by ArtOfWarfare; Jan 7, 2013 at 08:40 PM. |
|
|
|
0
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 01:07 AM.







Threaded Mode