its been around 1 week i started C.im learning it from "teach yourself C in 21 days".im in the 9th day and ive finished the chapter and doing the exercises.theres an exercise that asks "how to assign the address of a float value called radius to a pointer"
at the back of the book the answer of this exercise is just
Code:
float *var = &radius;
what i tried on the VC++ 6 compilter is
Code:
#include<stdio.h>
float *var,radius=4;
main()
{
*var=&radius;
printf("var has the address of %d and it points to %f",var,*var);
return 0;
}
unfortunately im getting the error " 1.c(6) : error C2115: '=' : incompatible types "
I tried var=&radius instead of *var=&radius and it worked.
but for the case of passing arrays as arguments to functions:
Code:
#include<stdio.h>
int array[10];
void f1(int);
main()
{
f1(array);
return 0;
}
void f1(int *x)
{printf("%d",x);}
After compiling im getting warnings.
1.c(7) : warning C4047: 'function' : 'int ' differs in levels of indir
m 'int [10]'
1.c(7) : warning C4024: 'f1' : different types for formal and actual p
1.c(12) : warning C4028: formal parameter 1 different from declaration
but the * in the void f1(int) is fixing the warnings
can someone explain why doesn't the *var=&radius work in the first code but when passing array to the *x in function f1 (as void f1(int *x)) its working ? like they are the same thing ... in the second code we are trying to *x = array , by passing array to the argument ( int *x)
also why is the Warnings for and how come they are fixed when in the function prototype the f1(int) is replaced by f1(int *) ?