how to calculate an array's length?
-
A simple question. I define an array.How to calculate its length? int[] arInt = { 1, 2, 3 ,4, 5 }; Thanks in advance.
-
A simple question. I define an array.How to calculate its length? int[] arInt = { 1, 2, 3 ,4, 5 }; Thanks in advance.
-
A simple question. I define an array.How to calculate its length? int[] arInt = { 1, 2, 3 ,4, 5 }; Thanks in advance.
Peter, Chan wrote:
I define an array.How to calculate its length
Did you have a look at the sizeof()
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
Peter, Chan wrote:
int[] arInt = { 1, 2, 3 ,4, 5 };
The correct syntax should be:
int arInt[] = {....
int nSize = sizeof(arInt) / sizeof(int);
this is this.
Then if it is a array of string.Should I write like this. char * arLang[] = {"english", "chinese"}; int nSize = sizeof(arLang) / sizeof(char);
-
Then if it is a array of string.Should I write like this. char * arLang[] = {"english", "chinese"}; int nSize = sizeof(arLang) / sizeof(char);
Nope Then you get the number of char's in the array, not the number of strings You can't count it this way, because each string has a different lenght. You can determine the length of arLang, then loop on it and count the occurence of \0 (End of a String) So you get the number of strings in the array, if i tell no mistakes :-) Good luck
-
Then if it is a array of string.Should I write like this. char * arLang[] = {"english", "chinese"}; int nSize = sizeof(arLang) / sizeof(char);
No, it can't be done with multi-dimensional arrays. You can only do it with single-dimensional arrays like: char arLang[] = {"english"}; And the length will be 8 bytes. And whenever you get the operator * anywhere, you just can't be sure of the size. But you can write:
int nSize1 = strlen(arLang[0]) + 1; int nSize2 = strlen(arLang[1]) + 1;
etc.this is this.
-
Then if it is a array of string.Should I write like this. char * arLang[] = {"english", "chinese"}; int nSize = sizeof(arLang) / sizeof(char);
Peter, Chan wrote:
int nSize = sizeof(arLang) / sizeof(char);
Mistake! change as follows:
int nSize = sizeof(arLang) / sizeof(char *);
or (better):
int nSize = sizeof(arLang) / sizeof(arLang[0]);
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
A simple question. I define an array.How to calculate its length? int[] arInt = { 1, 2, 3 ,4, 5 }; Thanks in advance.
It depends on what you want. If the array is dynamically declared, i.e. You used 'malloc', there is no knowing, unless if you build in your own scheme of determining these things. At any rate, the size of the array is known to you, because you had to give the size to the malloc function. In these cases, you need to devine a scheme to pass this knowledge on to where it is needed. in statically declared arrays, you can use the sizeof() operator to get the size of an array in bytes. But, still again, beware and look at the language definition. i.e.: char * Arr[]={"First Item","Second Item"}; This is an array of pointers to character strings. Arr contains two 4-Bit Items ( or 2 8-Bit items in a 64 bit Compiler), namely the Addresses of the two strings, "First Item" and "Second Item". So in the above example, sizeof(ARR) would always give 8 ( or 16 in a 64 bit Compiler environment). If you need to know wat's needed to store the structure ans it's data, you need:- size_t GetTotalDataSize(){ size_t NrOfItems= sizeof(Arr)/sizeof(char*); size_t TotalSize=sizeof(Arr); for(int i=0;i
-
It depends on what you want. If the array is dynamically declared, i.e. You used 'malloc', there is no knowing, unless if you build in your own scheme of determining these things. At any rate, the size of the array is known to you, because you had to give the size to the malloc function. In these cases, you need to devine a scheme to pass this knowledge on to where it is needed. in statically declared arrays, you can use the sizeof() operator to get the size of an array in bytes. But, still again, beware and look at the language definition. i.e.: char * Arr[]={"First Item","Second Item"}; This is an array of pointers to character strings. Arr contains two 4-Bit Items ( or 2 8-Bit items in a 64 bit Compiler), namely the Addresses of the two strings, "First Item" and "Second Item". So in the above example, sizeof(ARR) would always give 8 ( or 16 in a 64 bit Compiler environment). If you need to know wat's needed to store the structure ans it's data, you need:- size_t GetTotalDataSize(){ size_t NrOfItems= sizeof(Arr)/sizeof(char*); size_t TotalSize=sizeof(Arr); for(int i=0;i
Your description is very clear. I now know clearly how to use sizeof(). I still just have one question. Just as your sample.
char * Arr[]={"First Item","Second Item"};
Do I need to delete Arr when it is useless.
delete Arr[0]; delete Arr[1];
-
Your description is very clear. I now know clearly how to use sizeof(). I still just have one question. Just as your sample.
char * Arr[]={"First Item","Second Item"};
Do I need to delete Arr when it is useless.
delete Arr[0]; delete Arr[1];
Peter, Chan wrote:
Do I need to delete Arr when it is useless.
In this case, no. Since you did not allocate the strings using
new
, you do not have to free them usingdelete
.Best wishes, Hans
[CodeProject Forum Guidelines] [How To Ask A Question] [My Articles]