Asked 7 years ago
7 Jul 2017
Views 3517
jaman

jaman posted

comparison between pointer and integer ('int *' and int) in iOS objective c

initialisation

     int *selectedType;

trying to save selected picker view row to selectedType integer

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
     
         selectedType =   &row;
[self  showdata]
    }
-(void)Showdata{

        if(  selectedPaymentType == 1 || selectedPaymentType == NULL){
//other code goes here 
}
}


on selecting the picker view . application crash

i can see warning at code .
comparison between pointer and integer ('int *' and int)



Phpworker

Phpworker
answered Apr 24 '23 00:00

In iOS Objective-C, understanding the difference between pointers and integers (int) is crucial.

A pointer is a variable that holds the memory address of another variable. Pointers are widely used in Objective-C to refer to objects and other data structures. In Objective-C, an int * pointer is a pointer to an integer variable.

On the other hand, an int is a primitive data type that stores an integer value.

When you compare a pointer and an integer in Objective-C, you are comparing the memory address (pointer) with the integer value. This is not a recommended practice and can lead to unexpected behavior. For example, the following comparison may not work as expected:



int *myPointer = // some integer pointer value
int myInt = // some integer value

if (myPointer == myInt) {
    // This comparison may not work as expected
}

To compare an integer and a pointer in Objective-C, you should compare the pointer with the memory address of the integer variable using the address-of operator (&). Here's an example:



int *myPointer = // some integer pointer value
int myInt = // some integer value

if (myPointer == &myInt) {
    // This comparison is correct
}

In summary, pointers and integers are different data types in iOS Objective-C. Comparing them directly is not recommended, and if you need to compare an integer and a pointer, you should compare the pointer with the memory address of the integer variable.
Post Answer