Thread: iterating over arrays of pointers

  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
    28,413
    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.
    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
    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
    28,413
    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.
    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

  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, 10: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, 04: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, 11:13 AM