#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    help me find length of array by pointer


    I want find size of array by pointer in a function.example :

    //char array[] is a random array,i can alloc dynamic,so i can know size,i want access a random possition in array.

    char* pointer;
    void myfunc (char * array)
    {
    pointer = array;

    //how many elements in the array???
    }
    i use C programming.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    The short answer is, you can't.

    Code:
    void func ( char *ptr ) {
        size_t len = strlen(ptr);
    }
    
    int main ( ) {
        char a[100] = "hello";
        char *b = malloc(100);
        strcpy(b,"hello");
        func(a);
        func(b);
    }
    For char arrays only, you can use strlen() to find out the length of a regular (that is, \0 terminated string).
    But there is nothing you can do that will give you the answer 100 in either case.
    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
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,161
    Rep Power
    2222
    You should already know the size of that array, since you had created it yourself. Keep track of that information.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Thanks salem and dwise1_aol !
    but my purpose can't use strlen(),because example:

    void func ( char *ptr ) {
    size_t len = strlen(ptr);
    printf("sizeof len = %d ",len);
    }

    int main ( ) {
    // char a[100] = "hello";
    char a[6] ={ 1,2,3,0,4,5 };
    char *b =(char*) malloc(100);
    strcpy(b,a);
    func(a);
    func(b);
    }

    it'll print len(a) and len(b) =3 (But i want len(a) and len(b) equal 6).How way i can get size of array and can alloc for pointer b.example:

    void func ( char *ptr ) {
    char* px;
    char size_of_ptr m,k;
    // m= size of array that ptr point to.I can't get size of array.
    px=ptr;
    char *b =(char*) malloc(m);
    memcpy(b,px,m);
    // k= size of array that b point to.I can't get size of array.
    printf("sizeof len = %d ",k);
    }

    int main ( ) {
    // char a[100] = "hello";
    char a[6] ={ 1,2,3,0,4,5 };
    char n[8] ={ 1,2,0,5,4,5,8 };
    func(a);
    func(n);
    }

    Originally Posted by salem
    The short answer is, you can't.

    Code:
    void func ( char *ptr ) {
        size_t len = strlen(ptr);
    }
    
    int main ( ) {
        char a[100] = "hello";
        char *b = malloc(100);
        strcpy(b,"hello");
        func(a);
        func(b);
    }
    For char arrays only, you can use strlen() to find out the length of a regular (that is, \0 terminated string).
    But there is nothing you can do that will give you the answer 100 in either case.
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    So change it to be
    Code:
    void func ( char *ptr, size_t size ) {
    }
    
    int main ( ) {
       char a[6] ={ 1,2,3,0,4,5 };
       func(a,sizeof(a));
    }
    There is no other way to solve the problem.
    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
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    thanks you!let i try,maybe find a good idea! :)
    Originally Posted by salem
    So change it to be
    Code:
    void func ( char *ptr, size_t size ) {
    }
    
    int main ( ) {
       char a[6] ={ 1,2,3,0,4,5 };
       func(a,sizeof(a));
    }
    There is no other way to solve the problem.

IMN logo majestic logo threadwatch logo seochat tools logo