Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Assignment makes pointer from integer without a cast
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Assignment makes pointer from integer without a cast



You should definitely read the free ObjC.pdf that Apple provides, as it should help clear up some confusion here.

But basically, Objective-C is a struct superset of C, without any ambiguity between primitive types and ObjC classes. Objects in ObjC are always created on the heap, rather than the stack, and as such they always require a pointer. Thus, you can never declare a string like: NSString string; but instead you must always declare it as NSString* string; since it is a pointer to an NSString object in memory.

(This is true even for short-lived temporary objects, which at first confused me greatly. To create a temporary object for the method you're in, you can simple do: MyObject* object = [[[MyObject alloc] init] autorelease]; in most cases. The -autorelease method is what makes it a temporary objects for you, even though it's created on the heap. Read more about memory management to learn about this. But I've digressed, that's a Cocoa topic more than an Objective-C topic.)

Steven
Software Engineer
Big Nerd Ranch, Inc.
http://www.bignerdranch.com/

On Fri, Mar 5, 2010 at 11:09 AM, Marx Bievor <email@hidden> wrote:
Hi Steve,

thank you. You were right, I used int* instead of int.. The warning has gone. Thank you!

But that makes me wonder whether I understood object* vs object correctly. Apparently not. Could you please give me a brief explanation what the difference is? Is *used for objects, while without *, is it something like a primitive data type in java?

thank you
-M
Am 05.03.2010 um 17:01 schrieb Steven Degutis:

Hi Marx,

This warning pops up when you're assigning, as the warning says, a value of one type to a variable of another type. Having not yet seen your header file, I can't be 100% certain of this, but the warning strongly indicates that your "age" instance-variable (or ivar) is in fact *not* typed as "int", but as some kind of pointer, perhaps as int* for example. But it's hard to say without seeing your header file (I'm sure you understand why we can't just take your word for it, and why we need to see the code verbatim).

-Steven


On Fri, Mar 5, 2010 at 10:53 AM, Marx Bievor <email@hidden> wrote:
Hi there,

I'm relatively new to the objC language, that's why I would be very happy if you could help me. 

The compiler gives me the following build warning (the code compiles and I can run my app just fine, but I'm curious to know which line of code causes this behavior): "Assignment makes pointer from integer without a cast". The error occurs in the line "age = newAge"... I declared age as an int in the .h file.

CODE:

#import "Person.h"

@implementation Person
@synthesize lastName, firstName, age;

-(id)initWithFirstName:(NSString *)fName name:(NSString *)lName age:(int)newAge;
{
if (self = [super init]) {
lastName = lName;
firstName = fName;
age = newAge; // ERROR occurs here!
}
    return self;
}

Any suggestion would be appreciated. Thanks in advance.
-M

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden



--
Steven Degutis
http://www.thoughtfultree.com/
http://www.degutis.org/




--
Steven Degutis
http://www.thoughtfultree.com/
http://www.degutis.org/
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Assignment makes pointer from integer without a cast (From: Marx Bievor <email@hidden>)
 >Re: Assignment makes pointer from integer without a cast (From: Steven Degutis <email@hidden>)
 >Re: Assignment makes pointer from integer without a cast (From: Marx Bievor <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.