hi,

i know this is a basic question and i have tried searching GOOGLE but it does not solve my problem.

I have a array which is declared as

int frameScores[];

but when i tried to get its size after adding elements by using sizeof, i get a 0.

I'm sure that the array has values but i just can't get its length which i normally get by using the sizeof method.

Any possible solution?

hi,

i know this is a basic question and i have tried searching GOOGLE but it does not solve my problem.

I have a array which is declared as

int frameScores[];

but when i tried to get its size after adding elements by using sizeof, i get a 0.

I'm sure that the array has values but i just can't get its length which i normally get by using the sizeof method.

Any possible solution?

You can only declare an array like this:

int frameScores[];

if you directly initialize it, otherwise that declaration is impossible (there's no way for the compiler to find out the length of the array you want to make).
So assuming you did it the correct way, you can get it's size like this:

sizeof frameScores / sizeof *frameScores;

That's exactly what i am trying to do

sizeof(x) / sizeof(x[0])

but it seems that it just doesnt work, its always zero even if its value

Did you declare the array like this:

int x[] = {5, 7, 9, 2}; // put some values in it

?

[edit]
Could you provide me with the smallest compilable solution which doesn't work correctly on your system?
[/edit]

Edited 7 Years Ago by mvmalderen: n/a

No,

how do i make it expand without exact array length?

here is the code:

#include<stdio.h>
#include<conio.h>
#include<string.h>

const int ROLLING_FIRST_BALL = 0;
const int ROLLING_SECOND_BALL =1;
const int STRIKE_LAST_BALL =2;
const int TWO_CONSEC_STRIKE =3;
const int STRIKE_2_BALLS_AGO =4;
const int SPARE_LAST_BALL =5;

class Scorer{
	private:
	int rollingFrame;
	int totalScore;
	int state;
	int firstBallInFrame;
	int lastFrameNumber;
	void addFrame(int toAdd)
	{
         //printf("Size of framescore=%d\n",sizeof(frameScores)/sizeof(int));
         totalScore = totalScore + toAdd;
         if (sizeof(frameScores)/sizeof(frameScores[0]) < lastFrameNumber) 
         {
            int temp[(sizeof(frameScores)/sizeof(frameScores[0]))+1];
            for (int k=0; k<(sizeof(frameScores)/sizeof(frameScores[0])); k++) 
            {
                temp[k] = frameScores[k];
            }
            temp[(sizeof(frameScores)/sizeof(frameScores[0]))] = totalScore;
            //printf("Size of framescore=%d\n",frameScores[0+1]);
            for (int k=0;k<(sizeof(temp)/sizeof(temp[0])); k++) 
            {
                frameScores[k] = temp[k];
            }
            //frameScores = temp;
         }
    }
	public:      
    int frameScores[];    
	Scorer(int frameCount){
               lastFrameNumber = frameCount;
               rollingFrame = 1;
	           totalScore = 0;
           	   frameScores[0];
   	           state = ROLLING_FIRST_BALL;
    }
	int frameNumber( )
    {
        if ((sizeof(frameScores)/sizeof(frameScores[0])) == lastFrameNumber) {
               return lastFrameNumber+1;// game is over
        } else if (rollingFrame > lastFrameNumber) {
               return lastFrameNumber;// we're in last frame
        } else {
               return rollingFrame;
        }
    }
	int scoreSoFar(){
        if((sizeof(frameScores)/sizeof(frameScores[0])) == lastFrameNumber)
            return frameScores[lastFrameNumber-1];
	    else
		    return totalScore;
    }
    bool gameIsOver(){
         return frameNumber() > lastFrameNumber;
    }
    void roll (int ball,int k,int s,int p)
    {
        if(state == ROLLING_FIRST_BALL)
        {
                 if(ball == 10)
                 {
                       rollingFrame++;
                       state = STRIKE_LAST_BALL;
                 }else 
                 {
                       firstBallInFrame = ball;
                       state=ROLLING_SECOND_BALL;
                 }
        }else if(state == ROLLING_SECOND_BALL)
        {
              if(firstBallInFrame + ball == 10)
              {
                 rollingFrame++;
                 state = SPARE_LAST_BALL;
              }else
              {
                   rollingFrame++;
                   addFrame(firstBallInFrame + ball);
                   state = ROLLING_FIRST_BALL;
              }
        }else if(state==SPARE_LAST_BALL)
        {
              addFrame(10 + ball);
              if(ball == 10) 
              {
                      rollingFrame++;
                      state = STRIKE_LAST_BALL;
              }else 
              {
                    firstBallInFrame = ball;
                    state = ROLLING_SECOND_BALL;
              }
        }else if(state == STRIKE_LAST_BALL)
        {
              if(ball==10)
              {
                     rollingFrame++;
                     state = TWO_CONSEC_STRIKE;
              }else
              {
                      firstBallInFrame = ball;
                      state = STRIKE_2_BALLS_AGO;
              }
        }
        else if(state == TWO_CONSEC_STRIKE)
        {
               addFrame(20 + ball);
               if(ball == 10)
               {
                       rollingFrame++;             
               }else
               {
                       firstBallInFrame = ball;
                       state = STRIKE_2_BALLS_AGO;
               }
        }
        else if(state == STRIKE_2_BALLS_AGO)
        {
                addFrame(10 + firstBallInFrame + ball);
                if(firstBallInFrame + ball == 10)
                {
                         rollingFrame++;
                         state=SPARE_LAST_BALL;
                }else
                {
                         totalScore = totalScore + firstBallInFrame + ball;
                         rollingFrame++;
                         addFrame(firstBallInFrame + ball);
                         state = ROLLING_FIRST_BALL;
                }
        }else
        {
         //invalid state: state
        }
    printf("checking length of result of ball %d:%d:%d\n", k,s,sizeof(frameScores));
	for (int j=0;j<(sizeof(frameScores)/sizeof(frameScores[0]));j++) 
    {
	    printf("checking frame %d in result of ball %d:%d:%d\n",j,k,p,frameScores[j]);
	}
}
};

int main()
{
     Scorer s = Scorer(3);
     int balls[] = {10, 7, 3, 8, 2, 10};
     int lengths[ ] = {0, 0, 1, 2, 2, 3};
     int frames[ ] = {2, 2, 3, 3, 3, 4};
     int finalResult[ ] = {20, 38, 58};
     
     for (int k=0; k<(sizeof(balls)/sizeof(balls[0])); k++) 
     {
     //int *result = s.roll (balls[k]);
        s.roll(balls[k],k,lengths[k],finalResult[k]);
		printf("checking frame number after ball %d:%d:%d\n",k,frames[k],s.frameNumber());
		if (lengths[k] == 0) 
        {
		printf("checking score after ball %d:0:%d\n",k,s.scoreSoFar());
		} else 
        {
			printf("checking score after ball %d:%d:%d\n",k,s.frameScores[sizeof(s.frameScores)/sizeof(s.frameScores[0])],s.scoreSoFar());
		}
		//System.out.println("checking if game is over after ball " + k  + ":" + k == balls.length-1 + ":"+ s.gameIsOver ( ));
        printf("-----------------------------------------------\n");
     }
     getch();
     return 0;
}

Edited 7 Years Ago by ivatanako: n/a

No,

how do i make it expand without exact array length?

Do I understand this correctly? You want to create an array which' size can increase dynamically as elements are added or removed?

Again: please post down the smallest compilable code which I can use to reproduce your problem.

Do I understand this correctly? You want to create an array which' size can increase dynamically as elements are added or removed?

Again: please post down the smallest compilable code which I can use to reproduce your problem.

yes sir, that's exactly what i am trying to do.

i have posted above the code itself.

i have posted above the code itself.

I don't believe that's the smallest compilable solution to reproduce the problem.

I don't believe that's the smallest compilable solution to reproduce the problem.

Sorry, here it is

int frameScores[];

frameScores[0]=1;
frameScores[1]=2
for (int j=0;j<(sizeof(frameScores)/sizeof(frameScores[0]));j++) {
	// this wont execute since j =0 and its size is =0    
}

Sorry, here it is

int frameScores[];

frameScores[0]=1;
frameScores[1]=2;
for (int j=0;j<(sizeof(frameScores)/sizeof(frameScores[0]));j++) {
	// this wont execute since j =0 and its size is =0    
}

But how can you compile that? It isn't possible, my compiler even produces error messages, which is logical in such a case:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
tttt.cpp:
Error E2449 tttt.cpp 6: Size of 'frameScores' is unknown or zero in function main()
Error E2449 tttt.cpp 6: Size of 'frameScores' is unknown or zero in function main()
Error E2109 tttt.cpp 11: Not an allowed type in function main()
*** 3 errors in Compile ***

You should rather do it like this:

int frameScores[] = {1, 2};

for (int j=0;j<(sizeof(frameScores)/sizeof(frameScores[0]));j++) {
	// this wont execute since j =0 and its size is =0    
}

Edited 7 Years Ago by mvmalderen: n/a

The array sizeof thing only works when the definition of the array is in scope - that is, the compiler can see the actual array.

An array declaration, say extern int array[ ]; or an array parameter void foo ( int array[ ] ); needs a different approach.

This article has been dead for over six months. Start a new discussion instead.