Setting Integer value in Objective c

I recently started programming in iOS. I am looking at a code snippet that declares the following variables:

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

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

+4
source share
2 answers

I suggest you familiarize yourself with the basics of programming languages, in particular C programming with pointers. Objective-C is a superset of C and follows many similar rules.

But to your question: 0x before the literal values ​​in the code (0x00) indicates that the value is interpreted as hexadecimal, not decimal. But 0x00 (hex) is the same as 0 (dec).

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

int is a primitive type in Obj-C and C that defines an integer, effectively initializing the variable. In C, you must initialize the variables, otherwise they could point to a random part of the memory. So consider this code:

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

In C, the variable 'a' is not initialized, and therefore it is usually not safe to assume that it will be initialized to 0. Always initialize your variable. If you made printf or NSLog the variable "a", you will see that it prints a huge amount, and this does not make sense (sometimes it depends on the compiler)

The same can be said for BOOL. Although setting 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 initialize a pointer to point to any memory address. The actual memory address is usually determined by the stack / heap, and you need not worry about it. But you need to make sure that the pointer does not indicate the wrong location (known as the 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 when you immediately allocate memory for an object, so in this case you do not need to worry about initializing a pointer to nil:

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

As I said, I recommend that you familiarize yourself with basic C programming, selections, pointers, data types, initialization, etc., as soon as you understand this, and then go to Objective-C, which then builds it on it with object oriented material.
Good luck.

+3
source

0X00 just 0 in hexadecimal notation. Thus,

 int rc = 0X00; 

coincides with

 int rc = 0; 

Same thing for BOOL variables, where 0 same as NO . Using 0X00 is odd - it would be wiser to use 0 or NO , where necessary, and use nil for pointers.

+4
source

Source: https://habr.com/ru/post/1494893/


All Articles