Thread: Pointers to structures - Beginner question

  1. #1
    Registered User RobJ's Avatar
    Join Date
    Apr 2006
    Location
    Bath, England
    Posts
    16

    Pointers to structures - Beginner question

    Hi all, I'm just starting to get to grips with C programming and am having a little trouble using pointers and structures. I'm working through a simple exercise for practice and have the following structure:
    Code:
    typedef struct array2d_info {
    int *contents; //Pointer to array contents
    int columns; //Number of columns in array
    int rows; //Number of rows in array
    }
    I then create a pointer to one of these structures and want to test if the pointer is actually pointing to anything, so I have the following code:
    Code:
    Array2D *a
    if (a == NULL) {
    printf("NULL");
    } else {
    printf("Non-NULL");
    }
    As far as I can see this code should print "NULL", since the pointer hasn't been initialised to point to anything. However it prints "Non-NULL" every time. How would I test to see if a pointer is actually pointing to something?

    I've tried using "if (*a == NULL)" instead but I get a compiler error about "Invalid structure operations".

    I'm sure it's a simple mistake but I'm all out of ideas. Thanks for any help.
    Last edited by RobJ; 04-08-2006 at 04:26 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to initialise a to NULL, otherwise it contains some garbage value.
    Code:
    Array2D *a = NULL; /* initialise a */
    
    /* further down the road */
    
    if (a == NULL) {
        printf("NULL");
    } else {
        printf("Non-NULL");
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User RobJ's Avatar
    Join Date
    Apr 2006
    Location
    Bath, England
    Posts
    16
    Oh okay, thanks for the very speedy reply.

    Unfortunately that doesn't completely solve my problem. To expand on the exercise I have a function with a signature like this:
    Code:
    int array2d_create(Array2D *array, int columns, int rows)
    This method basically uses malloc() and calloc() to allocate memory for the array object, and returns a pointer to the array through the "array" parameter. I then have functions later on for manipulating the array, such as:
    Code:
    int array2d_get(Array2D *array, int column, int row)
    and
    Code:
    int array2d_set(Array2D *array, int column, int row, int value)
    In each of these functions I want to test if the pointer "array" is actually pointing to an "Array2D" object, and return an error code if it isn't.

    Since there is no guarantee that the user of the functions (which are being used in a basic library) will initialise the pointer to NULL when it is created, how could I test if the pointer has been initialised to point to a structure before it is used?

    Thanks again
    Last edited by RobJ; 04-08-2006 at 04:47 AM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can't, really. That's one of the bad things about pointers: you can't tell if they're pointing to valid memory or a random location.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User RobJ's Avatar
    Join Date
    Apr 2006
    Location
    Bath, England
    Posts
    16

    Thumbs up

    That is most unhelpful. I eventually 'solved' the problem by adding another int field called elements that holds the product of the number of rows and columns. If elements == rows*columns then I accept the array as having been initialised; I suppose theoretically you could end up with three random integers that have this relationship in an uninitialised structure but it's unlikely, so it'll have to do for now.

    Thanks for the help.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    You could create a macro that would initialize every instance of the structure: #define INIT(x) (*x = NULL);

    Array2D* a;
    INIT(&a); // initialize array to null.

    That way the client users of your library would see the significance, and importance of calling on INIT for proper initialization before use - but they do not need to know it's simply doing a null assignment.
    Last edited by xeddiex; 04-09-2006 at 12:11 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or you could force variables to be declared with a macro, and the user won't know it's even being initialized:
    Code:
    #define AnArray2D(x) Array2D x = NULL
    
    AnArray2D(array);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question about pointers #3
    By maxhavoc in forum C++ Programming
    Replies: 3
    Last Post: 06-19-2004, 10:45 AM
  4. Question about pointers
    By maxhavoc in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2004, 12:46 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM