Thread: HELP NEEDED: Count the number of element of array of pointer inside function

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    30

    HELP NEEDED: Count the number of element of array of pointer inside function

    I need help for calculate the number of element of the array of pointer inside a function. But I always get a return value of 1 element. Is there any way to program it to get the correct value? Thank you very much.


    Code:
    #include "stdafx.h"
    #include <stdlib.h>
    
    unsigned int read_file(char*my_filename[])
    {
    	return	sizeof(my_filename)/sizeof(my_filename[0]);
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	unsigned int num_file=0;
    	char * my_filename[]={"a","b","c"};
    	num_file=read_file(my_filename);
    	printf("%d\n",num_file);
    	system("pause");
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I need help for calculate the number of element of the array of pointer inside a function.
    This is not possible. When you pass an array to a function it decays to a pointer and you loose all size information. You need to either keep track of the array size, or compute the size in the function where the array is declared. You should always pass the size of the array into the function as another parameter.
    Code:
    unsigned int read_file(char*my_filename[], size_t array_size);
    
    int main(int argc, char* argv[])
    {
        unsigned int num_file=0;
        char * my_filename[]={"a","b","c"};
        size_t array_size = sizeof(my_filename)/sizeof(my_filename[0]);
        num_file=read_file(my_filename, array_size);
        printf("%d\n",num_file);
    
        return 0;
    }
    Jim

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    30
    Thanks jimblumberg. I have try this out. I know that doing in main can work for it. But I wonder is there another way to do it? Or string is it possible for it?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zhengkoon8
    I know that doing in main can work for it. But I wonder is there another way to do it?
    You could write a macro such as:
    Code:
    #define ARRAY_ELEMENT_COUNT(array) (sizeof(array) / sizeof((array)[0]))
    But in the end you must still have an array, not a pointer, for this to work correctly, hence it will only simplify this:
    Code:
    size_t array_size = sizeof(my_filename)/sizeof(my_filename[0]);
    num_file=read_file(my_filename, array_size);
    to:
    Code:
    num_file = read_file(my_filename, ARRAY_ELEMENT_COUNT(my_filename));
    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
    Dec 2009
    Posts
    30
    Thanks for this suggestion. I learned some new stuff

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-16-2012, 04:42 AM
  2. puting an element from many areas inside one big array?
    By nick753 in forum C++ Programming
    Replies: 13
    Last Post: 11-06-2010, 02:08 PM
  3. count/display non-repeated element of array
    By azsquall in forum C++ Programming
    Replies: 15
    Last Post: 07-10-2008, 09:42 AM
  4. Replies: 2
    Last Post: 05-10-2006, 01:43 PM
  5. Count the number of element in 2D char array
    By alice in forum C Programming
    Replies: 2
    Last Post: 04-24-2004, 12:51 AM

Tags for this Thread