NSString and Pointer [duplicate] - objective-c

This question already has answers here:
Pointers on Objective-c
(2 answers)
Why dereferencing a NSString pointer is not necessary?
(1 answer)
Closed 9 years ago.
The following snippet prints the value 10,
int x = 10;
int *y = &x;
NSLog(#"Value pointed by y = %d",*y);
But incase of NSString pointer as below, why are we not prefixing the variable name with * to retrieve the value it points to.
NSString *country = #"USA";
NSLog(#"Value pointed by country: %#",country);
Let me know if I am missing something. Thanks.

You can think about this as of a purely syntactic convention - a way of keeping Objective-C a pure superset of the C language. The way the Objective-C designers decided to represent objects is with C pointers: all variables of object type, including NSString, must be declared as a pointer. In fact, it is an error to define a variable of an object type without an asterisk:
NSString country; // <<== ERROR: this will not compile

Objects are always addressed by using pointers. You can not dereference them. Therefore is is not necessary and would be wrong to dereference an object pointer.

Related

Why would we use a pointer in an NSInteger but not in an NSNumber? [duplicate]

This question already has answers here:
What's the difference between NSNumber and NSInteger?
(5 answers)
Closed 6 years ago.
I have an NSInteger that doesn't use a pointer and an NSNumber that does use a pointer. Can someone explain to me why this is the case? All my teacher said was the NSInteger is being used as a type alias but I'm not familiar with that either yet.
This question asked a direct question asking for the reason there was no pointer in NSInteger; not asking for all the differences.
Bear in mind that Objective-C is C. Thus:
An NSInteger is a scalar, a built-in C data type (an integer). [The actual size of this integer depends on the architecture, 32-bit vs. 64-bit. But it is still some form of C integer.]
An NSNumber is an object; Objective-C object references are represented as C pointers.

Objective-C Casting with Pointer [duplicate]

This question already has answers here:
Objective-C and Pointers
(3 answers)
why most of the objects we create in iphone are pointers
(1 answer)
Closed 9 years ago.
I am new to Objective-C and come from a Java background. I have just gone over casting in Objective-C but the book I am using failed to explain the use of the '*'/pointer when casting. Here is the example they gave me:
myFraction = (Fraction *) fraction;
Aren't pointers for specific objects so they have their own unique memory location? So then why must I use a pointer when simply referencing a class? In this case, Fraction.
Thanks I hope this makes sense and I know this is a simple question that I should know and understand but I could find nothing explaining this.
The * symbol has multiple meanings (beside multiplication :):
Dereference (follow) pointers. This code follows pointer stored in pointerToInt and then assigns a value to it.
(*pointerToInt) = 5;
Declares a pointer type. When you write int * it means “reference to an integer”.
int x = 5;
int * xPtr = &x
Now, objects are a kind of structures, but we only manipulate with them via pointers. Never directly. This basically means, that 99% of time when you see * (and it's not multiplication :) it is the second case: a part of type declaration:
NSString * = pointer to NSString structure (you can't use NSString alone)
Fraction * = pointer to Fraction structure and Fraction structure is described in Fraction class
So it's not “pointer to the Fraction class”, but rather “pointer to structure of Fraction class”.
I will go a little further and answer your future question about two **. You may see this usually with NSError arguments that are defined like methodWithError:(NSError **)errorPtr.
Short story: int is to int * as NSError * is to NSError **.
Long story: If we cannot manipulate with objects directly (without pointers to them), the single pointer becomes standard part of declaration. Now what if we want to make indirect access to the object? We use double pointer! First * is required for object, second is for indirection.
NSError *error = nil; // Empty.
NSError **errorPtr = &error; // Reference to our local `error` variable.
[data writeToURL:URL options:kNilOptions error:errorPtr];
// That method uses: (*errorPtr) = [NSError errorWith...];
NSLog(#"Error: %#", error); // Our local error is no longer empty.
I believe pointers are weird when you come from Java. They are a bit of legacy from C, but they are not used in any crazy way.
The * symbol is simply syntax that's used when referring to pointers.
Here, myFraction, and fraction are both variables that hold pointers (they aren't objects themselves – in fact you never have variables that hold Objective-C objects, objects must always be referred to with pointers).
The (Fraction*) syntax describes a cast to a pointer-to-a-Fraction of the expression on its right (in this case the fraction variable).
Remember that a pointer is just a variable that holds a memory location.
In Objective-C, when you have an object, what you really have is a pointer to an object, that is, a variable whose value is the memory address where the object really is.
Casting a pointer to a pointer of another type has no effect at runtime (at least for objects). In fact all your objects could be of type (void *). The casting helps the compiler to know what kind of object the pointer is pointing to, and generate errors or warnings.
If these two little paragraphs don't make much sense to you right now, consider reading some basic information or tutorials on pointers. Understanding pointers can be challenging for a beginner or from someone transitioning form the Java world.
...failed to explain the use of the '*'/pointer when casting...
Pointers have little to do with casting, other than being part of a type specifier. Consider:
Fraction is a type -- for the sake of argument, let's imagine that it's the name of a class, and that Fraction is a subclass of another class called Number.
Fraction * is a pointer to an instance of the Fraction class. In Objective-C, you always use pointers to refer to objects, so you'll see a lot of variables with types of the form ClassName *.
Casting is simply a matter of telling the compiler that it should treat a variable as a certain type. So, let's say you've got a variable number of type Number * and you know that the object it points to is actually a Fraction. However, you can't use any of the methods that are specific to Fraction because, as far as the compiler is concerned, number is just a Number *. You can use a type cast to tell the compiler: "I know what I'm doing, and number is definitely pointing to an instance of Fraction, so please treat number as a Fraction *." You do it like this:
Fraction *f = (Fraction *)number;
But again, the * doesn't have any special significance in the casting operation beyond the fact that Fraction * is the type to which you're casting number.

Different c array declaration [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
sizeof array clarification
I have 2 arrays declared
GLfloat gCubeVertexData[216] = { list of numbers};
and an array declared:
GLfloat *resultArray = malloc(sizeof(GLfloat) * [arrayOfVerticies count]);
for(int i = 0; i < [arrayOfVerticies count]; i++)
{
resultArray[i] = [[arrayOfVerticies objectAtIndex:i] floatValue];
}
why is it when I do sizeof(gCubeVertexData) I get 864 ( a GLflot is 4 bits so divided by 4 and you get 216)
and when I do sizeof(resultArray) I get 4? event though if I were to print out resultArray[100] I get the correct number, and there is a lot more than 4 numbers stored?
Because gCubeVertexData is an array, and resultArray is a pointer. In the case of an array, the compiler knows how many bytes it is required to allocate for the array, so it explicitly knows a size (in the case of variable-length arrays in C99, it can also be computed easily at runtime, perhaps by messing with the stack pointer).
However, in the case of malloc(), the compiler has no knowledge about the size of the memory pointed by the pointer (that size can be obtained in a non-standard and platform-dependent way only anyways...), so it just returns the size of the variable itself, which is a pointer in this case, so you'll get back sizeof(GLfloat *) in the end.
Because with sizeof(resultArray) you are getting the size of the pointer to the first element.
The type of resultArray is simply GLfloat *, i.e. "pointer to GLfloat, and your machine uses 4 characters to store a pointer. The size information associated with the pointer is not visible to the sizeof operator.
Therefore, sizeof resultArray == sizeof (GLfloat *), which is what you're seeing.
Look at the declaration of gCubeVertexData and resultArray. The first is an array with 216 elements, the latter is just a pointer. C (and thus C++ and Objective-C) allow to use pointers to access arrays, but that does not mean they have the same type.

The asterisk in Objective-C object instance declarations: by the Object or by the instance? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's your preferred pointer declaration style, and why?
In C, why is the asterisk before the variable name, rather than after the type?
What makes more sense - char* string or char *string?
When declaring a new instance of an object in Objective-C, does it make any difference where you put the asterisk?
Is this just a matter of personal preference?
NSString* string = #"";
vs.
NSString *string = #"";
It doesn't make a difference, but there are good reasons to put it in each place:
It makes sense to put it near the class, because that makes it feel like a type: NSString*, a pointer to a string. Sensible.
It makes sense to put it near the variable, because that's what's actually happening: * is dereference. When you dereference your pointer string, you get an NSString. *string is an NSString. Sensible.
You may want to go with the latter because that's the way the compiler is thinking, so: NSString* oneString, anotherString will not work, whereas NSString *oneString, *anotherString is correct.
It's simply a matter of preference. Putting the * next to the type emphasizes that it's part of the type, i.e. "pointer to an NSString". However, this is usually frowned upon, because it ignores the fact that the * associates with the nearest variable name, not the type name. For instance, the following doesn't work:
NSString* a = #"string1", b = #"string2
This is because a is a pointer, but b is not.
Putting the * next to the variable name is, in my opinion, more of a C/C++ convention, because it emphasizes that the * and the variable name together act kind of like a variable.
Personally, I put a space on both sides of the *.
Another question that asked the same thing is here:
Declaring pointers; asterisk on the left or right of the space between the type and name?
It doesnt make the difference wher you put that pointer symbol. If you declare multiple objects in single line, you do it like NSString *str1, *str2. So its more appropriate to put that asterisk close to object. I prefer it close to object instance.

What does the Asterisk * mean in Objective-C?

Is it true, that the Asterisk always means "Hey, that is a pointer!"
And an Pointer always holds an memory adress?
(Yes I know for the exception that a * is used for math operation)
For Example:
NSString* myString;
or
SomeClass* thatClass;
or
(*somePointerToAStruct).myStructComponent = 5;
I feel that there is more I need to know about the Asterirsk (*) than that I use it when defining an Variable that is a pointer to a class.
Because sometimes I already say in the declaration of an parameter that the Parameter variable is a pointer, and still I have to use the Asterisk in front of the Variable in order to access the value. That recently happened after I wanted to pass a pointer of an struct to a method in a way like [myObj myMethod:&myStruct], I could not access a component value from that structure even though my method declaration already said that there is a parameter (DemoStruct*)myVar which indeed should be already known as a pointer to that demostruct, still I had always to say: "Man, compiler. Listen! It IIISSS a pointer:" and write: (*myVar).myStructComponentX = 5;
I really really really do not understand why I have to say that twice. And only in this case.
When I use the Asterisk in context of an NSString* myString then I can just access myString however I like, without telling the compiler each time that it's a pointer. i.e. like using *myString = #"yep".
It just makes no sense to me.
an * is actually an operator to de-reference a pointer. The only time it means "hey i'm a pointer" is during variable declaration.
Foo* foo // declare foo, a pointer to a Foo object
&foo // the memory address of foo
*foo // de-reference the pointer - gives the Foo object (value)
mmattax well covered the distinction between declaration (as a pointer) and dereferencing.
However, as to your point about:
(*myVar).myStructComponentX = 5;
to access a member of an instance of a C struct (as this is) you can do what you did , or more commonly you use the -> notation:
myVar->myStructComponentX = 5;
Objective-C is a little confusing here because it recently (in ObjC 2.0) introduced property syntax, which is a short cut for:
int val = [myObject someIntProperty];
and can now be written as:
int val = myObject.someIntProperty;
This is Objective C (2.0) syntax for accessing a property which you have declared (not an actual member variable), whereas your example was accessing a member of a C struct.
Make sure you are clear on the difference.
As I said in my answer of your previous question, #"yep" is already a pointer, so there is no need of * before myString which is also a pointer. In this case, you assign pointers not values.