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;
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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]
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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.
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
i have posted above the code itself.
I don't believe that's the smallest compilable solution to reproduce the problem.
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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
}
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
How do you make an array dynamically expandable?
Using a 'normal' array this isn't possible in C, you could maybe try your hands on a linked list ?
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953