Thread: Help please

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    Unhappy Help please

    I am beginner to c. I have to make snake game to run through microcontroller and FPGA based demo card. I have one code. I have to add some features. but i have troubles with understanding the code.

    here are some declarations

    typedef unsigned int snake_t;
    snake_t *snake;

    unsigned int snake_struct_length;
    unsigned int snake_head_i;
    unsigned int snake_tail_i;
    #define SNAKE_EXTEND_STEP 5
    unsigned int snake_extend;

    and then there is a line like that

    snake[snake_head_i] = new_snake_head;

    i didn't get "snake[snake_head_i]" part. because snake is a pointer. so what does it mean? Could someone explain me?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Pointers and arrays share the same subscript notation.

    With a pointer, writing ptr[index] generates the code for *(ptr+index)

    So a pointer to a number of ints say gives you access to each int in that block of integers, simply by incrementing i in ptr[i] (much like you would do with an array of ints).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2
    thank you for your quick reply

Popular pages Recent additions subscribe to a feed