Finding num of items in array

This is a discussion on Finding num of items in array within the C++ Programming forums, part of the General Programming Boards category; How can that be achieved? I need somehting similar to strlen, but for any array. Can I use strlen for ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    Finding num of items in array

    How can that be achieved? I need somehting similar to strlen, but for any array. Can I use strlen for that matter?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    That didn't work, it returned 1, it should be 24 according to what I counted.

    My array is type of int*, here is the code:
    Code:
    int *indices=NULL;
    
    ...
    ...
    int ind[] = {1,2,3,4,
                       2,4,2,1,
                        .........
                     };
    
    //Then I do
    indices = ind;
    //(is this correct?)
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    strlen() only works for null terminated char array (ie, strings).

    Arrays must have a constant size throughout a program. However, not every available spot in memory needs to be occupied. For example:

    int array[3] will hold up to 3 ints. However:

    array[0] = 0;
    array[1] = 1;

    works fine, even if array[2] isn't used. To keep track of how many items are actually in the array I usually use a counting variable. Then the fun starts because the maximal size is often called the capacity and the actual size is called size, which is actually a counting variable.

    int array[10];
    int size = sizeof(array)/sizeof(array[0]);

    I believe will give you size = 10

    but why bother? You already know what the size is, one way or the other. I suspect you want to know how many values are actually in the array, not what the maximal number of elements could be.
    Last edited by elad; 12-02-2002 at 10:58 AM.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    That is what I do. Well, I think I should better post the whole class code, it is not that big.
    Code:
    //class:
    class RT3DAPI CRTRenderer
    {
    public:
    	CRTRenderer() 
    	{
    		vertices=NULL;
    		indices=NULL;
    	}
    
    	void PassVertices(void* vertices, int* indices);
    	void Render(rtPrimType PT);
    
    private:
    	void* vertices;
    	int* indices;
    };
    
    //cpp file: code:
    void CRTRenderer::PassVertices(void* vertices, int* indices)
    {
    	this->vertices = vertices;
    	this->indices = indices;
    
    	glEnableClientState(GL_VERTEX_ARRAY);
    	
    	glVertexPointer(3, GL_FLOAT, 0, this->vertices);
    }
    
    void CRTRenderer::Render(rtPrimType PT)
    {
    	//find how many items are in the array:
    	int size=0;
    	size = sizeof(this->indices) / sizeof(this->indices[0]);
    
    	char test[100];
    	sprintf(test, "%i", size);
    	MessageBox(0,test,"",0);
    
    	glDrawElements(PT, size, GL_UNSIGNED_INT, this->indices);
    
    }
    Then, I use PassVertices with 2 arrays, one my type, Vec3 which consists of x,y,z floats and the int one, which describes the indices. This is something:
    Code:
    	Vec3* g_cube = new Vec3[8];
    	g_cube[0] = Vec3(-1,1,1);
    	g_cube[1] = Vec3(1,1,1);
    	g_cube[2] = Vec3(1,-1,1);
    	g_cube[3] = Vec3(-1,-1,1);
    	g_cube[4] = Vec3(-1,1,-1);
    	g_cube[5] = Vec3(1,1,-1);
    	g_cube[6] = Vec3(1,-1,-1);
    	g_cube[7] = Vec3(-1,-1,-1);
    
    	int g_ind[] = {0,1,2,3,
    				   4,5,1,0,
    				   3,2,6,7,
    				   5,4,7,6,
    				   1,5,6,2,
    				   4,0,3,7};
    renderer.PassVertices(g_cube, g_ind);
    renderer.Render(RTPT_QUADS);
    Thats it.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    to see if it works how about placing code aftet the declaration of g_ind[]?


    int g_ind[] = {0,1,2,3,4,5,1,0,3,2,6,7,5,4,7,6,1,5,6,2,4,0,3,7};
    int size = sizeof(g_ind)/sizeof(g_ind[0]);
    cout << size << endl;

    if it works there but it doen't work in:

    void CRTRenderer::Render(rtPrimType PT)

    then I would try changing this:

    void CRTRenderer::PassVertices(void* vertices, int* indices)
    {
    this->vertices = vertices;
    this->indices = indices;


    to this:

    void CRTRenderer::PassVertices(void* vertices, int* _indices)
    {
    this->vertices = vertices;
    this->indices = new int(sizeof( _indices)/sizeof( _indices[0]);

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    The one you said worked, but as you said it didnt work at the class using the int*.

    I changed it to what you said, using new, but I still have the same result. Here is my changed class code:
    Code:
    void CRTRenderer::PassVertices(void* vertices, int* indices)
    {
    	this->vertices = vertices;
    	this->indices = new int(sizeof(indices)/sizeof(indices[0]));//indices;
    	this->indices = indices;
    
    	glEnableClientState(GL_VERTEX_ARRAY);
    	
    	glVertexPointer(3, GL_FLOAT, 0, this->vertices);
    }
    
    void CRTRenderer::Render(rtPrimType PT)
    {
    	//find how many items are in the array:
    	int size=0;
    	size = sizeof(this->indices) / sizeof(this->indices[0]);
    
    	char test[100];
    	sprintf(test, "%i", size);
    	MessageBox(0,test,"",0);
    ....
    }
    Maybe there is a mistake in the Render method, calculating the size? I dont know.. But we are vreally close... Why wouldnt it work with an int* and it would work as the g_ind is defined? I dont get that.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    don't know for sure. suspect compiler having problems separated the array you pass in called indices and the member variable you call indices. That's why I changed the name of the passed in array called indices to _indices. I'd place the underscores as in my code in your code to see if it makes a difference explicitly telling the compiler which "indices" it should use.

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    I did that after posting this reply, but it had the same result. Any other method of finding the num of items in the array?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    passing the size is a time honored technique. Adjust the declaration and definition of Render to include an int parameter, calculate size of g_ind before passing it to Render, and pass the size as a parameter.

    int g_ind[] = {0,1,2,3,4,5,1,0, 3,2,6,7,5,4,7,6,1,5,6,2,4,0,3,7};
    int size = sizeof(g_ind)/sizeof(g_ind[0]);
    renderer.PassVertices(g_cube, g_ind);
    renderer.Render(RTPT_QUADS, size);

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Yea, I believe I will do it like that if I can't find the solution.

    I wanted the Renderer to automatically get the size, however, passing the size as a parameter could be beneficial in the end Anyway, thanks everyone for your help! I might come back to this matter at another topic.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  2. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  3. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  4. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  5. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

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