C program to calculate the size of the given array without using sizeof operator.
#include <stdio.h>
#define MAXLIMIT 256
int main() {
int array[MAXLIMIT], size, val1, val2;
/* get the address of first and second element */
val1 = (int)&array[0];
val2 = (int)&array[1];
/* diff b/w the address gives the size of one element in the array */
size = val2 - val1;
/* printing the size of an element in the i/p array */
printf("Size of an element in the given array is %d\n", size);
/* printing the address of 1st and second element in i/p array */
printf("&array[0]=>%x \n&array[1]=>%x\n",
(int)&array[0], (int)&array[1]);
/* printing the address difference */
printf("&array[1] - &array[0] => %d\n", size);
/* calculating the given array size */
size = (size * MAXLIMIT);
/* printing the result */
printf("\nSize of the given array is %d\n", size);
return 0;
}
#define MAXLIMIT 256
int main() {
int array[MAXLIMIT], size, val1, val2;
/* get the address of first and second element */
val1 = (int)&array[0];
val2 = (int)&array[1];
/* diff b/w the address gives the size of one element in the array */
size = val2 - val1;
/* printing the size of an element in the i/p array */
printf("Size of an element in the given array is %d\n", size);
/* printing the address of 1st and second element in i/p array */
printf("&array[0]=>%x \n&array[1]=>%x\n",
(int)&array[0], (int)&array[1]);
/* printing the address difference */
printf("&array[1] - &array[0] => %d\n", size);
/* calculating the given array size */
size = (size * MAXLIMIT);
/* printing the result */
printf("\nSize of the given array is %d\n", size);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Size of an element in the given array is 4
&array[0]=>bfda587
&array[1]=>bfda5878
&array[1] - &array[0] => 4
Size of the given array is 1024
Size of an element in the given array is 4
&array[0]=>bfda587
&array[1]=>bfda5878
&array[1] - &array[0] => 4
Size of the given array is 1024
No comments:
Post a Comment