Considering this structure of data:
typedef struct {
float x;
float y;
} point;
And i'm using this function to permute the coordinates:
point permute(point M)
{
point N;
N.x = M.y;
N.y = M.x;
return N;
}
Why is declaring a variable with the name (point) which is a typedef not giving any error?
int main(void)
{
point A;
point B;
int point = 7;
A.x = 0;
A.y = 1;
B = permute(A);
printf("A (%.2f, %.2f)\n", A.x, A.y);
printf("B (%.2f, %.2f)\n", B.x, B.y);
printf("point = %d\n", point);
return 0;
}
Output: http://ideone.com/wCxbjD
A (0.00, 1.00)
B (1.00, 0.00)
point = 7
Scope.
An identifier declared in an outer scope (in your case, at file scope, i.e., outside any function) may be redeclared in an inner scope (in your case, inside the main function). The inner declaration hides the outer declaration until you reach the end of the inner scope.
This applies to declarations in general, not just to typedef names.
A simple example:
#include <stdio.h>
typedef struct { int x, y; } point;
int main(void) {
point p = { 10, 20 };
/* p is of type point */
int point = 42;
/* This hides the typedef; the name "point"
now refers to the variable */
printf("p = (%d, %d), point = %d\n", p.x, p.y, point);
/* p is still visible here; its type name "point" is not
because it's hidden. */
}
The output is:
p = (10, 20), point = 42
If we modified the above program to move the typedef into the same scope as the variable declaration:
#include <stdio.h>
int main(void) {
typedef struct { int x, y; } point;
point p = { 10, 20 };
int point = 42;
printf("p = (%d, %d), point = %d\n", p.x, p.y, point);
}
we'd get an error; gcc says:
c.c: In function ‘main’:
c.c:5:9: error: ‘point’ redeclared as different kind of symbol
c.c:3:34: note: previous declaration of ‘point’ was here
(The C language could have been defined to permit this, with the second declaration of point hiding the first for the rest of the scope, but the designers apparently felt that hiding declarations from outer scopes could be useful, but doing so within a single scope would cause more confusion than it's worth.)
A slightly more complex example:
#include <stdio.h>
int main(void) {
typedef struct { int x, y; } point;
{ /* Create an inner scope */
point p = { 10, 20 };
/* Now the type name is hidden */
int point = 42;
printf("p = (%d, %d), point = %d\n", p.x, p.y, point);
}
/* We're outside the scope of `int point`, so the type name is
visible again */
point p2 = { 30, 40 };
printf("p2 = (%d, %d)\n", p2.x, p2.y);
}
Such hiding is probably not the best idea; using the same name for two different things, though it's no problem for the compiler, can be confusing to human readers. But it allows you to use names at block scope without having to worry about all the names that might have been introduced at file scope by all the headers you've include`.
This is clearly a question of scope:
typedef struct {
float x;
float y;
} point;
point point = {2.0, 3.0};
gives an error
blo.c:6:7: error: ‘point’ redeclared as different kind of symbol
point point = {2.0, 3.0};
^
blo.c:4:3: note: previous declaration of ‘point’ was here
} point;
whereas
void blo() {
typedef struct {
float x;
float y;
} point;
{
point point = {2.0, 3.0};
}
}
is legal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With