sizeof pointer array

This is a discussion on sizeof pointer array within the C Programming forums, part of the General Programming Boards category; Hi all! Is there any short way to get the number of elements in an pointer array? for example char ...

  1. #1
    x77
    x77 is offline
    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
    21,310
    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).
    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

  4. #4
    x77
    x77 is offline
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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, 09: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, 05:58 PM

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