* Confusion in C - MacRumors Forums
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
macrumors 603
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:
product = factor1 * factor2;
Or here:
runningProduct *= factor;
2 - It may be used to declare a pointer type variable, like these:
char* charPtr;
int* intPtr;
float* floatPtr;
double* doublePtr;
void* voidPtr;
Or here:
typedef struct {
    int a;
    int b;
} MyStruct;

MyStruct* myStructPtr;
3 - It may be used to dereference pointers, like here:
*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.
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:
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:
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

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:

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.


Somehow, I completely forgot about the rabbit hole that is C function pointers:

Here's an explanation of how to read them aloud:

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.
Including the ones paired with other Macs on your network.

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

Similar Threads
thread Thread Starter Forum Replies Last Post
Java confusion DianeK Mac Basics and Help 2 Jun 21, 2013 08:47 PM
Hotspot Confusion skysailing iPhone 5 Dec 23, 2012 08:38 PM
Two monitor confusion kenglade Mac Basics and Help 6 Dec 12, 2012 08:39 AM
10.7 and 10.8 confusion 2056 OS X 5 Dec 9, 2012 12:48 AM
Total Confusion mntr iPhone 2 Nov 28, 2012 11:28 PM

Forum Jump

All times are GMT -5. The time now is 04:26 AM.

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

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC