Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old Jan 7, 2013, 07:29 PM   #1
ArtOfWarfare
macrumors 68040
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
* 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;
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/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.
ArtOfWarfare is offline   0 Reply With Quote

 
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 01:07 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC