Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Integer value in Objective c

I have recently started programming in iOS.. I am going through a code snippet that declares the following variables:

int rc = 0X00;
sqlite3_stmt *pStmt = 0X00;
FMStatement *stat = 0X00;
BOOL abc = 0X00;

what does this mean?? I read somewhere that setting 0X00 in a reference variable means setting it to NULL (in C). But what does setting a BOOL type variable and an int type variable to 0X00 mean??

like image 443
Shradha Avatar asked Aug 02 '13 05:08

Shradha


People also ask

How do you declare an int value in Objective-C?

Variable Definition in Objective-Cextern int d = 3, f = 5; // declaration of d and f. int d = 3, f = 5; // definition and initializing d and f. byte z = 22; // definition and initializes z. char x = 'x'; // the variable x has the value 'x'.

How do you declare a string in Objective-C?

- (NSString *)substringFromIndex:(NSUInteger)anIndex; Returns a new string containing the characters of the receiver from the one at a given index to the end. You can find a complete list of Objective-C NSString related methods in NSString Class Reference.

What is Objective-C in iOS?

Objective-C is the primary programming language you use when writing software for OS X and iOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime.


2 Answers

I suggest you read up about the basics of programming languages, specifically, C programing with pointers. Objective-C is a superset of C and follows many similar rules.

But to your question: The 0x in front of the literal values in the code (0x00) specifies that the value is interpreted as hexadecimal rather than decimal. But 0x00(hex) is the same as 0(dec).

int rc = 0x00; //same as int rc = 0;

int is a primitive type in both Obj-C and C that specifies an integer, effectively you are initializing the variable. In the C language you must initialize variables otherwise they could be pointing at a random piece of memory. Therefore, examine this code:

int a;
int b = 0;
//a is NOT equal to b!

In C, the variable 'a' has not be initialized and therefore its not typically safe to assume that it will be initialized to 0. Always initialize your variable. If you did a printf, or an NSLog of the variable 'a' you will see that it prints some huge number and it doesnt make sense (sometimes this is compiler dependent)

The same can be said for a BOOL. Although setting a BOOL to 0 is the same as setting it to false;

BOOL flag = 0; //The same as saying BOOL flag = false;

Now for the final part of your code:

FMStatement *stat = 0X00;

Often in Objective-C if you are dealing with pointers and objects you need to initialise the pointer to point at some memory address. The actual memory address is usually determined by the stack/heap and you don't need to worry about that. But you do need to ensure that the pointer isn't pointing to the wrong location (known as a garbage pointer). To do this, we simply set our pointer to nil. eg:

FMStatement *stat = nil; //This pointer is now safe. Although memory still hasnt been allocated for it yet

This is usually taken care of for you though when you immediately allocate the memory for an object, therefore in this case you don't need to worry about initializing the pointer to nil:

FMStatement *stat = [[FMStatement alloc]init];

Like I said, I recommend you read about basic C programming, allocations, pointers, datatypes, initialising etc, once you have a grasp of this, then move to Objective-C which then builds ontop of it with Object-Oriented stuff.
Good luck.

like image 170
Ospho Avatar answered Sep 28 '22 18:09

Ospho


0X00 is simply 0 in hexadecimal notation. So,

int rc = 0X00;

is the same as

int rc = 0;

Same for BOOL variables, where 0 is the same as NO. Using 0X00 is odd -- it'd make more sense to use 0 or NO where appropriate, and use nil for the pointers.

like image 40
Caleb Avatar answered Sep 28 '22 18:09

Caleb