Reputation: 41
I have a little problem in my C application; please help me to reach a solution:
#include <stdio.h>
float t[5];
int i;
float *p;
*p=t;
int main (void)
{
for (i=0;i<=4;i++)
{
printf("t[%d]",i);
scanf("%f",&t[i]);
}
for (i=0;i<=4;i++)
{
printf("t[%d]=%f \n",i,*(p+i));
}
return 0;
}
When I compile this program the compiler gives me this problem:
[Warning] initialization from incompatible pointer type
What does this mean and how can I modify my code so it compiles and runs correctly?
Upvotes: 0
Views: 64
Reputation:
Corrections are commented
#include <stdio.h>
float t[5];
int i;
float *p;
*p=t; //here use p=t as *p will dereference it and t is already a pointer type in this assignment both will mismatch
int main (void)
{
for (i=0;i<=4;i++)
{
printf("t[%d]",i);
scanf("%f",&t[i]);
}
for (i=0;i<=4;i++)
{
printf("t[%d]=%f \n",i,*(p+i));
}
return 0;
}
Upvotes: 0
Reputation: 48268
Doing this
float t[5];
float *p;
*p=t;
will not compile
error: incompatible types when assigning to type ‘float’ from type ‘float *’
Do instead:
float t[5];
int i;
float* p = t;
Upvotes: 1
Reputation: 10425
You cannot slap some code outside of a function and hope it executes in some order.
float t[5];
float *p;
*p=t; // illegal, you probably meant p=t; anyway
float *p = t; // fine
int main (void) {}
Upvotes: 6