Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the & symbol mean in Objective-C?

What does the & symbol mean in Objective-C? I am currently looking at data constucts and am getting really confused by it.

I have looked around the web for it but have not found an answer at all. I know this is possibly a basic Objective-C concept, but I just can't get my head around it.

For example:

int *pIntData = (int *)&incomingPacket[0];

What is the code doing with incoming packet here?

like image 976
Adam Libonatti-Roche Avatar asked Sep 04 '09 09:09

Adam Libonatti-Roche


People also ask

What does the fox say Meaning?

Speaking of the meaning of the song, Vegard characterizes it as coming from "a genuine wonder of what the fox says, because we didn't know". Although interpreted by some commentators as a reference to the furry fandom, the brothers have stated they did not know about its existence when producing "The Fox".

What does a real fox say?

One of the most common fox vocalizations is a raspy bark. Scientists believe foxes use this barking sound to identify themselves and communicate with other foxes. Another eerie fox vocalization is a type of high-pitched howl that's almost like a scream.

How can I find a song by the sound?

On your phone, touch and hold the Home button or say "Hey Google." Ask "What's this song?" Play a song or hum, whistle, or sing the melody of a song. Hum, whistle, or sing: Google Assistant will identify potential matches for the song.


2 Answers

& is the C address-of unary operator. It returns the memory address of its operand.

In your example, it will return the address of the first element of the incomingPacket array, which is then cast to an int* (pointer to int)

like image 54
Paul Dixon Avatar answered Sep 27 '22 19:09

Paul Dixon


Same thing it means in C.

int *pIntData = (int *)&incomingPacket[0];

Basically this says that the address of the beginning of incomingPacket (&incomingPacket[0]) is a pointer to an int (int *). The local variable pIntData is defined as a pointer to an int, and is set to that value.

Thus:

*pIntData will equal to the first int at the beginning of incomingPacket.
pIntData[0] is the same thing.
pIntData[5] will be the 6th int into the incomingPacket.

Why do this? If you know the data you are being streamed is an array of ints, then this makes it easier to iterate through the ints.

This statement, If I am not mistaken, could also have been written as:

int *pIntData = (int *) incomingPacket;
like image 33
mahboudz Avatar answered Sep 27 '22 20:09

mahboudz