Thread: pointer initialisation

  1. #1
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522

    pointer initialisation

    damm this is doing my head in, I know it's probably a simple answer i should know myself but i cant figure it out

    I want to iniialise a pointer to an array of structures

    this is what i've tried

    Code:
    struct mystruct array[10], *array_ptr;
    
    array_ptr = array; /* this dont work */
    
    array_ptr = &array;  /* or this */
    
    array_ptr = array[0]  /* you guessed it */
    help before its to late and i jump of my roof
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  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
    The first should have worked
    Code:
    struct mystruct {
        int a;
    };
    int main ( ) {
        struct mystruct array[10], *array_ptr;
        array_ptr = array; /* this dont work - it does here */
        array_ptr = &array;  /* or this - because it's a pointer to the whole array */
        array_ptr = array[0];  /* you guessed it - this is an element of the array */
        array_ptr = &array[0]; /* but this is a pointer to the first element - So this is OK */
        return 0;
    }
    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 C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Thaks salem the first method works for me now.
    must be my compilers time of the month.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM