Thread: Obj-C/C mix - return makes integer from pointer without a cast

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    16

    Question Obj-C/C mix - return makes integer from pointer without a cast

    Hi, this is my first post here, and I'm sure I'll be back for more later!

    I'm learning Obj-C, and stumbled on some C code that I find useful, but as I'm a totally new to this, I find it a little bit hard understanding about pointers and types.

    The idea is to call the makeChar-method from the main code, get it processed, and then returned to the main. Basically myChar gets filled with 0 and 1's.
    The next step is to send along some parameters for different situations, so that's why it's a method.

    It's a mix of C and Obj-C code, but I hope it's common enough for you geniuses to see what's wrong here.
    The code is kinda pseudo, since the real code is just longer, and gets the same errors.
    Inside the method, I can read myChar[5] with no problem, but I can't get it out of there.

    Code:
    int myChar = [self makeChar];
    int char5 = myChar[5]; // Error: subscripted value is neither array nor pointer
    
    -(unsigned char)makeChar {
     unsigned char *myChar = (unsigned char *)malloc(50); 
     myChar[5] = 1;
     return myChar; // Warning: return makes integer from pointer without a cast
    }
    I've searched for the errors for several hours, but couldn't find an answer to what I'm doing wrong here.

    Thank you in advance for any help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You defined myChar in your first snippet as an int. So that's all you get: an int. No bunches of things. One single solitary int. Therefore asking for the [5]th one isn't going to get you anywhere.

    Why not have makeChar return a pointer, since that's what myChar is, and then assign it to something that makes sense on the other end?

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Thanks! I'm trying to change the code to what I think you mean as below, but I'm not quite there... Got rid of those warnings, got a new one.

    Apparently I don't want an int, the value of myChar[5] will always be 0 or 1, so I could use a char or a bool, they both use 1 byte right?
    It also seems wasteful to use 1 byte for each position, when I could store 8 bits, but packing it down would probably be my next issue. First I want this working..

    Code:
    unsigned char myChar = [self makeChar]; 
    unsigned char char5 = myChar[5]; // Error: subscripted value is neither array nor pointer
    
    -(unsigned char)makeChar {
     unsigned char *myChar = (unsigned char *)malloc(50); 
     myChar[5] = 1;
     return *myChar; 
    }
    It's really hard coming from being an "expert" at PHP non-OOP to this, I find the concept of pointers really hard. Any links to easy to grasp text I should read would be helpful.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Even PHP has a difference between arrays and not-arrays. If you want more than one thing, then you have to declare the variable to hold more than one thing. In this case, since you're doing the dynamic memory allocation yourself with malloc, then you just need a pointer to say "this is where that piece of memory lives, youbetcha".
    Code:
    unsigned char* myChar = [self makeChar];
    (Oh, and that's also your easy-to-grasp tutorial on pointers. Pointers say "this is where that piece of memory lives, youbetcha".)

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Heh, thanks for the tutorial.
    I have long wondered if there's a difference where you put the *.

    Are these two the same?
    Code:
    unsigned char* myChar = [self makeChar];
    unsigned char *myChar = [self makeChar];
    I read tutorials and got the feeling that they seem to jump back and forth and I think I tried both and got the same result, but I'm not sure...

    However, now I got this error message instead on the row you corrected. Do I need to initialize myChar somewhere else or what do I do?
    Warning: initialization makes pointer from integer without a cast

    Thanks for keeping me awake

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Spaces are irrelevant, assuming you don't split a token or merge two tokens. So no "un sig ned", and you need the space between unsigned and char, but that's it. (And between self and makeChar, too.)

    As to the other, you need to return myChar, not *myChar. (Return the pointer, not the thing pointed to.)

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by BlueGooGames View Post
    Heh, thanks for the tutorial.
    I have long wondered if there's a difference where you put the *.

    Are these two the same?
    Code:
    unsigned char* myChar = [self makeChar];
    unsigned char *myChar = [self makeChar];
    I prefer them in the middle:
    Code:
    unsigned char * myChar = [self makeChar];
    but that's just me.
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Ok, so to return a pointer, i kinda figured out i need to set the return type to (id) instead of (unsigned char), is that correct?

    But of course this gives me another error. I like this a lot, so I slowly can learn what everything is for. So the error message sounds like (id) is not a compatible pointer type for the pointer I'm using. What pointer am I using, and what is compatible?

    I feel like I'm fixing one end and breaking another..

    Code:
    -(id)makeChar {
    	unsigned char *myChar = (unsigned char *)malloc(50);
    	myChar[5] = 5;
    	return myChar; Warning: return from incompatible pointer type
    }
    I'm also curious on what this does:
    Code:
    (unsigned char *)malloc(50);
    Why is there a star after char? Is it a pointer to a pointer, or what is it?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by BlueGooGames View Post
    I'm also curious on what this does:
    Code:
    (unsigned char *)malloc(50);
    Why is there a star after char? Is it a pointer to a pointer, or what is it?
    To take the second part first, that's just an error. Well, it's right, but it's wrong. It should be
    Code:
    malloc(50);
    The bit in parentheses is just a cast, to say "hey I really want it to be this type", where "this type" is "unsigned char *". But it's bad form to have it in there, because you're not supposed to, really. (malloc returns "void *", which is convertible to any other pointer by default without you having to do anything.) People put the cast in to shut the compiler up, but if the compiler is complaining that's because you're doing something wrong.

    The other thing: "id" is of type Object *, which isn't at all like unsigned char *.

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    I don't think the compiler (Xcode) complains with casting before malloc, but it doesn't help to do it like this either:
    Code:
    unsigned char *myChar = malloc(50);
    I still get this on return:
    Warning: return from incompatible pointer type

    How should I do this to be able to get a value to work with? I'm really trying to understand pointers and C, but a piece of code with correct types and casts would really save my day.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you ever fix the (id) at the front?

  12. #12
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    In the start of this thread, it was (unsigned char), but later I changed to (id) because I thought that would make it return a pointer. But it isn't?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    id is a pointer to an NSObject, which is most assuredly not what you have. You have a pointer to an unsigned char. So that is what you are returning: a pointer to unsigned char, or (unsigned char *).

  14. #14
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Thanks, I finally got it. Your help is very much appreciated! I'll keep that in mind for my credits of my amazing game..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Replies: 4
    Last Post: 03-10-2009, 10:29 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM