PDA

View Full Version : pointers help.




larswik
Sep 3, 2010, 03:13 PM
In Main I sent an argument message to an instance variable in the object myVar. Also in Main I created a variable called 'newValue'. I am trying to set the variable 'newValue' using a pointer to 'value' located in the myVar object.
I get the error here.
newValue = *value;

I am getting an error saying "Value undeclared"?

Here is MAIN
#import <Foundation/Foundation.h>
#import"varstore.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
varstore * myVar = [[varstore alloc] init];
int newValue;

[myVar setValue:5];
NSLog(@"value = %i",[myVar value]);

newValue = *value;

NSLog(@"new Value = %i", newValue);

[myVar release];
[pool drain];
return 0;
}
Here is the Class @interface
#import <Foundation/Foundation.h>


@interface varstore : NSObject
{
int value;
}
@property int value;
-(void) setValue: (int) num;
@end
Here is the Class @implementation
#import "varstore.h"

@implementation varstore
@synthesize value;

-(void) setValue: (int) num
{
value = num;
}

@end

Any help is always appreciated :D



lee1210
Sep 3, 2010, 03:17 PM
newValue = [myVar value];

No pointers really involved. You never declared int *value, so the message is pretty clear.

-Lee

larswik
Sep 3, 2010, 03:27 PM
Ahhh... Thanks Lee. I am learning about pointers so I was trying to set up a simple example of how I can set a value to a variable in MAIN by pointing to an instance variable in an object.

Is it possible to show me where I went wrong and how I can use a pointer to get the value from the object? Or is my understanding of pointers wrong?

-Lars

lee1210
Sep 3, 2010, 03:38 PM
This is a little unusual (pointing to an ivar), but:
Write getValueAddress that returns an int *. the only code would be:
return &value;

then declare an int * in main, and assign the result of getValueAddress to it. You can then manipulate value in myVar by way of this pointer.

-Lee

larswik
Sep 3, 2010, 03:52 PM
I kinda get that... I thought that by creating @property and @synthesize in the class it would automatically synthesize my setters and getters for the instance variable. I would then avoid having to write and define a method in the class to set and retrieve the value of the instance variable.

It would seem that the use of pointers are a little more complicated then I first thought.

O someone posted this link. I think I should read the whole thing.
http://www.cplusplus.com/doc/tutorial/pointers/

-Lars

chown33
Sep 3, 2010, 03:58 PM
The syntax for direct access to instance variables from main() is this:
newValue = myVar->value;

It will cause a warning (or error) about an @protected instance variable.

Now that you know how to do this, DO NOT DO THIS.

Direct access to instance variables from outside a class is a bad design. The class's encapsulation is "leaky", because internal details of the implementation are exposed in ways that can compromise integrity, prevent subclassing, or cause unsightly hair growth.
http://www.jargondb.org/glossary/hair
http://www.jargondb.org/glossary/hairy
http://www.jargondb.org/glossary/hirsute

Every externally visible instance variable represents a contract to provide exactly that instance variable in perpetuity, with exactly the same name, type, semantics, etc. If the contract is ever broken or changed in any way, then other things that rely on the variable can break.

lee1210
Sep 3, 2010, 03:59 PM
Synthesize does behave as advertised. The generated methods return values, not pointers to the ivars, as values are a much more commonly used means of accessing an ivar. Getting a pointer to an ivar breaks encapsulation. A setter can be altered to meet new requirements, etc. in one place. Once a pointer is out there, all bets are off.

-Lee

chown33
Sep 3, 2010, 04:02 PM
I kinda get that... I thought that by creating @property and @synthesize in the class it would automatically synthesize my setters and getters for the instance variable. I would then avoid having to write and define a method in the class to set and retrieve the value of the instance variable.

It does. The problem is you're not using property-accessor syntax, and you're not asking "How do I access the property?".

Property access notation uses dot, as in:
newValue = myVar.value; // uses the getter
myVar.value = 24; // uses the setter


You don't have to define a setValue method, either. That is done automatically when you declared 'value' as a property, and then synthesized it. You might want to look at the examples of basic properties again.

larswik
Sep 3, 2010, 04:23 PM
You guys are shedding light on this again for me. Thanks!

Now chown33... These are the same, are they not?

[myVar setValue:5];
myVar.value = 5;

If that is the case, in the first example [myVar setValue:5]; I am sending a message to the method of the 'myVar' object to set the instance variable 'value' to 5. in the second example myVar.value = 5; I am in bypassing the method of the myVar object and setting the instance variable 'value' to 5 directly. If that is correct, I could only do that by @synthesizing my setters and getters?

lee1210
Sep 3, 2010, 04:27 PM
The dot is exactly the same as the message pass:
http://developer.apple.com/mac/library/documentation/cocoa/conceptual/objectivec/Articles/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW17

Just syntactic sugar. You CAN use the dot for your own methods, but it was introduced along with properties with synthesized setters and getters. That's the real intent.

-Lee

larswik
Sep 3, 2010, 05:22 PM
I see, thanks. Real quick... back to the pointer. I deleted the Class and tried this in main instead with a reference to variable ted.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

int ted;
int fred;

ted = 25;
fred = &ted;

NSLog(@"fred is pointed to %i", fred);
[pool drain];
return 0;
}
it exicutes but with a strange number which I believe is the address where the value is stored in memory. "

2010-09-03 15:15:00.577 pointer_test1[15806:10b] fred is pointed to -1073744172

I was expecting the value of 25 to be displayed. I do get a warning "Assignment makes integer from pointer without a cast". My guess is I am pointing to the address and not the value soted in the address?

lee1210
Sep 3, 2010, 05:33 PM
fred is not a pointer to an int. It's just an int:
int *fred;

Will get the type you need. Then in your print you can display the memory address stored with %p, then pass *fred to get the value stored there.

-Lee

chown33
Sep 3, 2010, 06:03 PM
I was expecting the value of 25 to be displayed. I do get a warning "Assignment makes integer from pointer without a cast". My guess is I am pointing to the address and not the value soted in the address?

This expectation is simply wrong. Also, "pointing to the address" makes little or no sense, given the expectation and what you described you wanted to do.

Given these things, I strongly suggest reading the entire article previously linked:
http://www.cplusplus.com/doc/tutorial/pointers/

If that doesn't make sense because you don't understand how memory is organized, then read this article:
http://masters-of-the-void.com/book5.htm

All this "real quick" stuff is completely wrong, because you're missing the fundamental points: what an address is, how an address can be stored in a variable (a pointer), how to declare a variable as a pointer, how to dereference a pointer, etc. When it's this far wrong, it's time to restart from the fundamentals and stop trying to do a quick fix.

I should also point out there are two distinct things: knowing what a pointer is, and knowing how to declare it and dereference it in the language. The first is a fundamental concept. The second is a matter of syntax. If you don't have a firm grasp of the concept, the syntax is just magic-speak.

larswik
Sep 3, 2010, 06:14 PM
chown33 - I was reading one of those that you listed. I will look over the other one and 'RESET'. I thought I was close in understanding. I don't what to move on in the book until I have a firm understanding of what is happening. My brain is almost 40 and seems not to be able to absorb things as easily now as when I was 20 :)

I appreciate your guys help and understanding with my slow learning curve.

-Lars