Scope of the variable name and function

This is my code:

#include <stdlib.h> #include <stdio.h> int sum(int,int); int sum(int x, int size) { int sum = 0; printf("%p\n", &x); printf("%p\n", &size); printf("%p\n", &sum); sum(x,size); return 0; } int main() { sum(2,4); return 0; } 

And the error I get is:

 hello.c:11:5: error: called object type 'int' is not a function or function pointer sum(x,size); ~~~^ 
+8
c scope
source share
6 answers

If you define two separate identifiers of the same name for different objects in the same namespace, they may overlap. C11 standard, chapters Β§6.2.1,

If the identifier denotes two different objects with the same name space, the areas may overlap ....

Note: why in this case both sum are in the namespace with the name

So, as soon as you redefine the identifier with a different type,

.... If so, the volume of one object (internal volume) will end strictly in front of the area of ​​another object (external coverage). Within the inner region, an identifier denotes an object declared in the inner region; an object declared in the outer region is hidden (and not displayed) in the inner region.

This means essentially in your case the inner function sum() , when you define int sum , basically you are obscuring the sum function. After overriding, sum is an identifier of type int in this scope. Thus, inside the sum() function, you cannot make a call to sum() , which is now an int type.

However, FWIW, the call to sum() in main() (or rather, outside of sum() ) must be valid, since at that moment int sum will be unavailable.

Solution: Change the variable name int sum to something else.

Thanks @pmg for correction

EDIT:

As mentioned in another @juanchopanza answer , after changing the name of the shadow variable, your program will compile, and as soon as you run it, you will encounter infinite recursion due to the unconditional call to sum() inside sum() . You need to add an interrupt condition to complete ( return from) the recursion.


Footnote:

Turning to C11 , chapter Β§6.2.3 , namespaces, we can say that there are separate namespaces for different categories of identifiers, for example. 1) label names 2) tags of structures, unions and transfers, 3) members of structures or unions, and 4) all other identifiers.

So, in this particular case, the sum() function and the int sum definition will be in the same namespace, for the sum() scope

+11
source share

You have changed the sum value here:

 int sum = 0; 

Now, in the area in which it was declared, sum is int , and this becomes nonsense:

 sum(x,size); /* Wat? sum is an int!!! */ 

Do not do this and your code will be compiled. After compilation, you may worry about stopping the recursion.

+12
source share

Scope

  • The function name sum here has a global scope.
  • The variable sum is local and has a local scope.

  • The conflict occurs because inside sum() you call sum() . Now, inside the sum() function, you have 2 objects of the same name and of a different type, therefore an error.

If you have something like

 int sum() { int sum = 0; sum = 10; printf("%d\n",sum); } 

Then your code would be ok. Since the sum variable is local to the sum() function, and it is the only object inside the sum() function with this name.

Instead of worrying about it, it’s better to rename the variable and make sure the variable name and function name are different.

+6
source share

You need to change int sum = 0 to int y = 0 , also your program will break when you do not stop recursion on sum(x,sum)

+4
source share

Just change the variable name sum inside the sum function to total

 #include <stdlib.h> #include <stdio.h> int sum(int,int); int sum(int x, int size) { int total = 0; printf("%p\n", &x); printf("%p\n", &size); printf("%p\n", &total); sum(x,size); return 0; } int main() { sum(2,4); return 0; } 
+2
source share

The variable and the function have the same name, therefore, why the function and the variable to exclude links refer to the cause of the error

+1
source share

All Articles