Thread: sizeof pointer array

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    8

    sizeof pointer array

    Hi all!

    Is there any short way to get the number of elements in an pointer array?

    for example

    char *s2[3];

    int count = sizeof(s2)/sizeof(s2[0]); shows me 1

    Thank you!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That is strange since it shows 3 to me alright.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This program prints 3, not 1:
    Code:
    #include <stdio.h>
    
    int main()
    {
        char *s2[3];
    
        int count = sizeof(s2)/sizeof(s2[0]);
        printf("%d\n", count);
        return 0;
    }
    The problem is probably because you have passed the array to a function. Since an array decays to a pointer to its first element, sizeof in the function works a pointer, not an array, hence you get 1. One solution is to pass the size of the array as well, and another solution is to designate some special element as the terminator (like how the null character is used to terminate strings).
    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

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    8
    Yes sorry I forgot to mention that I pass this pointer array to a function.

    But I think its not comfortable to pass the number of elements to a function because it should be a string function and I can never know how many elements I'll pass.

    The second solution what you've mentioned is i think the better way but then I must set (before i call the function) the last pointerarrayelement to NULL

    thanks

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > But I think its not comfortable to pass the number of elements to a function because it should be a string function and I can never know how many elements I'll pass.
    fgets() has no trouble with this concept.

    And sizeof() needs to see the actual array declaration for this approach to work.
    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.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But there is no solution to this problem.
    If it bothers you, you must create a set of functions to manipulate, say, your custom array type that keeps track of the size. This is exactly what C++ has done with its vector class. When you work with the object, it keeps track of its size so you never have to worry.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. pointer to multidimensional array
    By Bigbio2002 in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2006, 10:29 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM