(1)
#include <stdio.h>
#include <stdlib.h>
int a = 10, b = 20 , c = 30, d, e, *pa, *pb, *pc;
d= 10;
e= 100;
pa = &a;
pb = &b;
int main()
{
printf("%i, %i, %i, %i", pa, pb, d, e);
return 0;
}
(2)
#include <stdio.h>
#include <stdlib.h>
int a = 10, b = 20 , c = 30, d, e, *pa, *pb, *pc;
d= 10;
e= 100;
int main()
{
pa = &a;
pb = &b;
printf("%i, %i, %i, %i", pa, pb, d, e);
return 0;
}
Why do I get an error when I initialize the pointer variables pa and pb outside of the main function (1)? When pa and pb are inside the main function it works perfectly fine (2). Why can I initialize normal variables outside of the main function (d,e) but not pointer variables?
The error message I get in CodeBlocks is: Conflicting file types for pa. Previous declaration of pa was here: line 4.
mainto execute like you would expect them to in yourmain. Though you can declare variables. I believe that when you writepa = &aby that time you compiler doesn't know that you have already declaredpa. Further, if you do not give a data type, it is automatically taken asintso, when you writepathe compiler makes itint pa(without taking into consideration of your previous declaration) and then you try to assign a pointer to yourintthat why it gives you an error!d= 10;,pa = &a;, etc. lines are redeclaring those variables, not just setting their value... this is allowed at file scope provided the types match the earlier declarations. Since you don't give the type on those lines, it's assumed to beint, so it matches the earlier declaration fordande, but not forpaandpb.