FIrst off, arrays in C start from a zero index, not one.
So if an array
arr
has three elements 101, 102, and 103 then you can only access it's content using then indexes 0, 1, and 2:
arr[0] == 101
arr[1] == 102
arr[2] == 103
Second, integers and floats are not necessarily the same size: an
int
is commonly 16 bits (or 2 bytes) or 32 bits (or 4 bytes) wide, while a
float
is generally 32 bits (4 bytes) wide. Trying to use an
int
pointer to access
float
data may or may not works at all, depending on your compiler and / or operating system!
Thirdly, floats and integers aren't stored the same way:
int
is stored as a "straight binary value" with the most significant bit determining if the value is positive or negative. A
float
is much more complicated:
IEEE 754 - Wikipedia[
^]
So again, using a
int
pointer to access a
float
array isn't going to give you the results you expect!
Try it like this:
#include <stdio.h>
int main()
{
float cgpa[] = {25, 39, 45, 95, 98, 9,
9, 48, 45,546, 8, 41,
4, 8, 84, 84, 95};
float *pointer = &cgpa[0];
printf("%0.0f\n",*pointer);
return 0;
}
And it'll start to work.
But ... the name of an array is a pointer to it's first element (that's defined in the language specification) so this code is both more readable and simpler:
#include <stdio.h>
int main()
{
float cgpa[] = {25, 39, 45, 95, 98, 9,
9, 48, 45,546, 8, 41,
4, 8, 84, 84, 95};
float *pointer = cgpa;
printf("%0.0f\n",*pointer);
return 0;
}