I wrote an array_length function like this:
int array_length(int a[]){
return sizeof(a)/sizeof(int);
}
However it is returning 2 when I did
unsigned int len = array_length(arr);
printf ("%i" , len);
where I have
int ...
|
Like in the following. Though it gives the expected result with gcc4.0, is it really safe to do that? What if it's not double but some non-POD type? ----------------------------------------- #include #include #include int main() { using namespace std; const size_t n = 5; complex(ca); for (size_t i = 0; i < 2*n; ++i ... |
hi all! This is an interesting question asked in interview of HCL. Suppose a situation that u have a pointer to an array and u don't the size of array, so how could u find the size of array by having only pointer to that array? I tried but haven't ound any solution yet. Regards, Zia |
Hi, I write the following code: void someproc() { int controlids [] = { IDC_BUTTON_RC_CONNECT, IDC_BUTTON_RC_CONNECTPUBLIC, IDC_BTN_SENDHELLO }; someproc2(controlids); } void someproc2(int * e) { int size = sizeof(e)/sizeof(int); //.. do something with size.. like use in a for loop etc... } I expected to get the correct size, but it just gives me 1. What am I doing wrong? regards, ... |
|
|
#include #include void print_length(void * p) { int len = sizeof(p) / sizeof(p[0]); printf("the length of str is %d\n",len); return ; } int main() { char str[11] = "hello world"; int len = sizeof(str) / sizeof(str[0]); printf("the length of str is %d\n",len); print_length(&str); return 0; } linux@localhost:/data/learning/C/variantLen.c> gcc -o length length.c linux@localhost:/data/learning/C/variantLen.c> ./length the length of str is 11 ... |
|
|
#include #include #include #define SIZE 256 int getWord(char * newString, int size); int main(){ int j; int len; char get[SIZE]; getWord(get, SIZE); len = strlen(get); printf("len = %d\n", len); for(j=0; j |
|
There is no such thing in the C or C++ language as such. You can achieve the same effect, however, by declaring a pointer to the type you want an array of, and then allocate sufficient space, possibly re-allocating later on (if you don't know beforehand how many items you need). There is even a function called realloc, which allocates a ... |
No, the original problem was that the size needed to be passed to the function. There's no way around this. Either you pass in the size somehow, as a separate parameter or as part of a class or struct, or through some obscure method like global variables. Or you hard-code it. Basically, there's no way around it, C or C++ nonwithstanding. ... |
How do you determine the length of an array? For example: unsigned char HillsData[163840]; When I use: DebugTest[3] = sizeof(HillsData); I get 4 rather than 163840. I'd like to know how you determine the size of an array, as to prevent overflow. The image, for example, is 1024x40 pixels at 32-bit color. If one bothers to modify it to 1024x48 at ... |
char name[] = "John Smith"; asks a compiler to count number of bytes in the string "John Smith" add one byte for the 0 character, allocate enouhg space for it and copy this string into allocated buffer. char name [11]; asks compiler to allocate 11 bytes for later use char name[]; ask compiler to allocate a buffer but does not provide ... |
|
simple array question from a c beginner. basically i have an integer array as an argument to a function. my problem is getting its length. sample code of what i have so far is: int arrLen (int a[]) { return (sizeof a / sizeof a[0]); } but this returns 1, which i'm guess is 4 bytes divided by 4 bytes. any ... |
|
|
|
#include #include #include 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 ... |
Hi, A word of advice, I am NOT a sir, it is best not to assume that all programmers are men! You do not need to use length(), it has no meaning for you, since YOU set the length of the array when you declared it. I think you need to find a good reference book and read more about arrays, ... |