C Pointers. Confuse ?

When I started learning programming with C++, the learning is stopped when the chapter is reached to pointers. Pointers are often a bit of a stumbling block for people who want to learn C/C++. I read some articles about pointers, but most of them can’t explain the confusing point of pointers for me. In this article, I won’t explain basic and advance usage of pointers. I will explain what I was confused about pointers and what is need to know to clear on pointers. 

What is a Pointer

We can assume pointer is just like other datatypes, but it holds memory addresses rather than values. So, a pointer is a variable that contains a memory address. Please carefully look at the codes below.

int d = 13;
int *e = &d;
printf("d value is %i \n", d); //13
printf("&d value is %p \n", &d); //0xbfffdca8 memory address
printf("e value is %p \n", e); //0xbfffdca8 memory address
printf("*p value is %i \n", *e); //13

Line 1 initiate the value 13 to d.

Line 2, the memory address of d(&d) something will look like 0xbfffdca8 is assigned to *e that is integer pointer type. Still there is no problem. Using pointer is like using normal datatypes. Then, I printed the variables d, e and *e.

Confuse ??

Confusing start here. We assign the address value &d to pointer *e ( can assume like this, int *e = 0xbfffdca8 ), but when we accessing and print *e ,the output is value of d (13) instead of the address. Although we can assume pointers are like normal data types, we also have to assume initialising and accessing ways in pointers are different from normal datatypes.

Although we assigned the memory address to *e, the whole *e is not holding the memory address. Pointer name e is holding the address and ‘*‘ sign is pointing to real value on that address. So *e is printed 13 and e printed 0xbfffdca8.

That is what I was confused in recent years. If you have the same problems on pointers, I hope you are now ok. Then you can now find and practice pointer exercises. Here is one of the good tutorials about pointers. 

http://www.thegeekstuff.com/2011/12/c-pointers-fundamentals/

http://www.thegeekstuff.com/2012/01/advanced-c-pointers/

Literals in Objective-C


 int x = 13;
 double y = 10/3;

In above C code example, primitive values 13 and 10/3 are literals. As Objective-C is a superset of C, valid codes in C are also valid in Objective-C. But in iOS application development environment, object types provide from iOS SDK are being use for literal values instead of primitive data types.

NSString *greeting = @"Hello";

In the above code, @”Hello” is an String type Object literal. Without using @ sign and will be something like this


NSString *greeting = [NSString stringWithUTF8String:"Hello"];

So, the use of that @ sign for object literals are more clear and easy to readable. I think everyone use that literals instead of traditional initialising data type objects.

Here are some examples of object literals. They can also use in Numeric Types, Collection and in Dictionary.

Data Types : Traditional 

NSNumber *boolYES = [NSNumber numberWithBool:YES];
NSNumber *boolNO  = [NSNumber numberWithBool:NO];
NSNumber *charX = [NSNumber numberWithChar:'X'];
NSNumber *fortySevenInt = [NSNumber numberWithInt:47];
NSNumber *fortySevenUnsigned = [NSNumber numberWithUnsignedInt:47U];
NSNumber *fortySevenLong = [NSNumber numberWithLong:47L];
NSNumber *goldenRatioFloat = [NSNumber numberWithFloat:1.61803F];
NSNumber *goldenRatioDouble = [NSNumber numberWithDouble:1.61803];
Data Types : Literals
NSNumber *boolYES = @YES;
NSNumber *boolNO  = @NO;
NSNumber *charX = @'X';
NSNumber *fortySevenInt = @47;
NSNumber *fortySevenUnsigned = @47U;
NSNumber *fortySevenLong = @47L;
NSNumber *goldenRatioFloat = @1.61803F;
NSNumber *goldenRatioDouble = @1.61803;
Collection(Array) : Traditional
NSArray *instruments = [NSArray arrayWithObjects: @"Ocarina", @"Flute", @"Harp", nil];
Collection(Array) : Literals 
NSArray *instruments = @[ @"Ocarina", @"Flute", @"Harp" ];
NSMutableArray *instrumentsMutable = [ @[ @"Ocarina", @"Flute", @"Harp" ] mutableCopy];

Dictionary : Traditional

NSArray *keys   = [NSArray arrayWithObjects:@"Character", @"Weapon", @"Hitpoints", nil];
NSArray *objects = [NSArray arrayWithObjects:@"Zelda", @"Sword", [NSNumber numberWithInt:50], nil];
NSDictionary *stats = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

Dictionary : Literals

NSDictionary *stats = @{ @"Character" : @"Zelda",
                         @"Weapon" : @"Sword",
                         @"Hitpoints" : @50 };