Koray Tugay
Koray Tugay

Reputation: 23780

Scope of a variable and function name

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 am getting is:

hello.c:11:5: error: called object type 'int' is not a function or function pointer
sum(x,size);
~~~^

Upvotes: 8

Views: 732

Answers (6)

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

If you define two separate identifiers of same name for different entities in the same name space, they might overlap. C11 standard, chapter §6.2.1 states,

If an identifier designates two different entities in the same name space, the scopes might overlap....

Refer Footnote: Why in this scenario, both sums are in same name space

So, once you re-define the identifier with some other type,

....If so, the scope of one entity (the inner scope) will end strictly before the scope of the other entity (the outer scope). Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.

That means, essentially, in your case, inside function sum(), when you're defining int sum, basically you're shadowing the function sum. After the re-definition, sum is an identifier of type int, in that function scope. Thus, inside the function sum(), you cannot make a call to sum() as that is an int type now.

However, FWIW, the call to sum() in main() (or, rather, outside sum() itself) should be valid, as at that point, int sum will be out of scope.

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

Thanks to @pmg for the correction

EDIT:

As mentioned in the other answer by @juanchopanza, after changing the shadowing variable name, your program will compile and once you run it, you'll face infinite recursion due to the unconditional call to sum() inside sum() itself. You need to add some break condition to end (return from) the recursion.


FootNote:

Referring to C11, chapter §6.2.3, name spaces, we can say, there are separate name spaces for various categories of identifiers, e.g. 1) label names 2) the tags of structures, unions, and enumerations, 3) the members of structures or unions and 4) all other identifiers.

So, in this particular case, the function sum() and the int sum definition will reside in the same name space, for the sum() function scope

Upvotes: 11

Rahul Verma
Rahul Verma

Reputation: 68

Variable and function is of same name thats why it is de-referencing function and variable same time thats the cause of error

Upvotes: 1

Gopi
Gopi

Reputation: 19864

The scope matters

  1. Function name sum here has a global scope.
  2. Variable sum is local and has local scope.

  3. The conflict is because inside sum() you call sum(). Now inside function sum() you have 2 objects of same name and of different type hence the error.

If you had something like

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

Then your code would have been fine. Since variable sum is local to function sum() and it is the only object within function sum() with that name.

Instead of worrying about this better to rename the variable and make sure variable name and function name is different

Upvotes: 6

Anant Kolvankar
Anant Kolvankar

Reputation: 1050

Simply change variable sum name inside 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;
}

Upvotes: 2

Iwan1993
Iwan1993

Reputation: 1769

You need to change int sum = 0 to int y = 0, also your program will break as you dont stop recursion at sum(x,sum)

Upvotes: 4

juanchopanza
juanchopanza

Reputation: 227370

You changed the meaning of sum here:

int sum = 0;

From now on, in the scope in which it was declared, sum is an int, and this becomes nonsense:

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

Don't do that, and your code will compile. Once it compiles, you can worry about stopping the recursion.

Upvotes: 12

Related Questions