Free Online Tutorials ==>> COVID-19 Projects (Using AI-ML) || Machine Learning || Python Programming || DBMS || OOPs using C++ || DSA || Java Programming || Linux/Unix || C Programming
Go to your University || Python Lab || DSA Lab || AI & Machine Learning Lab || Linux Lab || OOPs Lab || DBMS Lab || JAVA Lab ||| Free Online Tutorials ||| 
companies for Campus Placement || Logical Reasoning || Quantitative Aptitude || General English || Technical-MCQ and Interview Questions || HR Interview Questions

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.8k points)
Best answer

Arrays have fixed lengths that are known within the scope of their declarations. Nevertheless, it is possible and sometimes convenient to calculate array lengths. In particular, this can make code more flexible when the array length is determined automatically from an initializer:

int array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

/* size of `array` in bytes */ 

size_t size = sizeof(array);

/* number of elements in `array` */ 

size_t length = sizeof(array) / sizeof(array[0]);

However, in most contexts where an array appears in an expression, it is automatically converted to ("decays to") a pointer to its first element. The case where an array is the operand of the sizeof operator is one of a small number of exceptions. The resulting pointer is not itself an array, and it does not carry any information about the length of the array from which it was derived. Therefore, if that length is needed in conjunction with the pointer, such as when the pointer is passed to a function, then it must be conveyed separately.

For example, suppose we want to write a function to return the last element of an array of int. Continuing from the

above, we might call it like so:

/* array will decay to a pointer, so the length must be passed separately */ 

int last = get_last(array, length);

The function could be implemented like this:

int get_last(int input[], size_t length) 


return input[length - 1]; 


Note in particular that although the declaration of parameter input resembles that of an array, it in fact declares input as a pointer (to int). It is exactly equivalent to declaring input as int *input. The same would be true even if a dimension were given. This is possible because arrays cannot ever be actual arguments to functions (they decay to pointers when they appear in function call expressions), and it can be viewed as mnemonic.

It is a very common error to attempt to determine array size from a pointer, which cannot work. DO NOT DO THIS:

int BAD_get_last(int input[]) 



size_t length = sizeof(input) / sizeof(input[0]));

return input[length - 1];           /* Oops -- not the droid we are looking for */ 


In fact, that particular error is so common that some compilers recognize it and warn about it. clang, for instance, will emit the following warning:

warning: sizeof on array function parameter will return size of 'int *' instead of 'int []' [Wsizeof-array-argument]        

int length = sizeof(input) / sizeof(input[0]);                           

^ note: declared here 

int BAD_get_last(int input[])

  Realize your learning potential with courses starting at ₹ 420
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Online Free Training:  MACHINE LEARNINGPython Programming | Database Management System(DBMS) | Object Oriented Programming(OOPs) using C++ | Data Structures and Algorithms(DSA) | Java Programming | Linux/Unix | C Programming
Exams: List of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG) | JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
About Us | Contact Us   Social::   |  |