What's wrong with this code: arrays?

Discussion in 'C' started by gormster, Dec 28, 2007.

  1. gormster

    gormster New Member

    Joined:
    Dec 28, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    - (void) sendMessage:(int) type data:(char[]) data length:(int)length
    {
    	++length;
    	char message[length];
    	message[0] = type;
    	printf("%X",message[0]);
    }
    
    I know this is in Obj-C but the important part is pure C. The weird bit at the top is just Obj-C's way of making an instance method. Basically, length and type are both integers.

    So, I pass 0x90 to 'type', and the printf gives me FFFFFF90. If I printf message[1] or [2], I get 7 and FFFFFF84, respectively. They should, of course, be empty.

    So, what the hell is going on? I feel I have made some kind of rookie mistake.
     
  2. gormster

    gormster New Member

    Joined:
    Dec 28, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    PS: I should mention that if I replace the message[0] = type with message[0] = 0x90, it works fine.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice