iterating over arrays of pointers

This is a discussion on iterating over arrays of pointers within the C Programming forums, part of the General Programming Boards category; What is the appropriate way to iterate over an array of pointers? How do you determine when you've reached the ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    69

    iterating over arrays of pointers

    What is the appropriate way to iterate over an array of pointers? How do you determine when you've reached the last pointer?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    What is the appropriate way to iterate over an array of pointers?
    The same way you would iterate over any array, e.g., with a for loop.

    How do you determine when you've reached the last pointer?
    You need to know the size of the array. Alternatively, you leave a null pointer at the end, imitating the null character trick of strings, but at the cost of having to have such a null pointer and risking accessing the array out of bounds if it is somehow not there.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    Quote Originally Posted by laserlight View Post
    You need to know the size of the array. Alternatively, you leave a null pointer at the end, imitating the null character trick of strings, but at the cost of having to have such a null pointer and risking accessing the array out of bounds if it is somehow not there.
    Thanks for the response.

    What is the standard way of doing this in C? Suppose I have a function whose parameter list contains an array of pointers, how do I specify how many items are in that array? Does it make sense to also include an additional parameter that allows the caller to explicitly define the number of pointers? Or is there a better way of doing this?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Suppose I have a function whose parameter list contains an array of pointers, how do I specify how many items are in that array? Does it make sense to also include an additional parameter that allows the caller to explicitly define the number of pointers?
    Yes, it does. It may even be better to define a struct that has the array as one member and the array size as the other member.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You could pass two pointers, one to the beginning and one to the end, and loop through it with pointer arithmetic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 09:40 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  4. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 03:12 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 10:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21