Thread: size of an array poited by array element

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    size of an array poited by array element

    hi i have a problem with this :

    Code:
    char *aa[] = { "a1","a2","a3" };
    char *ab[] = { "b1","b2" };
    char *ac[] = { "c1","c2","c3","c4" };
    
    char **pabc[] = { aa, ab, ac };
    i want to call

    Code:
    int n = sizeof (pabc[0])/sizeof(char*);
    to get the size of array aa.
    i think i'm doing it the wrong way

    thanks for help

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    No,You can't. since the type of pabc[0] is pointer to pointer to char.
    sizeof(pabc[0]) = sizeof(char**)

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    no way

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    By size of array aa do you mean the number of bytes it takes up in storage or the number of elements it contains? The formula you're using looks like an alteration of a common way to get the number of elements in an array. If the scope you're in can "see" the definition of aa, you can do:
    Code:
    int n_elements = sizeof(aa) / sizeof(aa[0]);
    int n_bytes = sizeof(aa);
    If you're in some scope that can't see the definition, you're out of luck. Declare aa to be some fixed size, like so:
    Code:
    #define AA_LEN 3
    char *aa[AA_LEN] = {"a1", "a2", "a3"};
    or use a sentinel value (null terminate it) and write a function to get the length:
    Code:
    char *aa[] = {"a1", "a2", "a3", NULL};
    int strv_len(char *aa[])
    {
        int i;
        for (i = 0; aa[i]; i++);
        return i;
    }

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Quote Originally Posted by anduril462 View Post
    By size of array aa do you mean the number of bytes it takes up in storage or the number of elements it contains?
    yes, i want to check the number of elements and thank you for your reply.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by mitofik View Post
    yes, i want to check the number of elements and thank you for your reply.
    You have to keep track of that on your own.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Location
    China
    Posts
    7
    i don't know what your means ,but you can try fthe code as following
    Code:
    int n = sizeof(pabc) / sizeof(pabc[0]);

    it get the number of a array .

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    zfk: you should actually read this thread to avoid giving an incorrect reply.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Maximum array size?
    By code2big in forum C Programming
    Replies: 2
    Last Post: 05-25-2004, 08:16 AM