• C

C: Function name the same as name of local variable

Hi,
I'm wondering how the compiler allows this.

We have a function

void LogUser(int LogOut, char *initials, int Success, int InvalidUser)

It has a local structure variable
LOGIN_INFO *LogUser;
which is allocated and freed.

How can I be allowed to have a local variable with the same name as its function? Could this cause any issues?

Thanks,
Steve
LVL 4
Stephen KairysTechnical Writer - ConsultantAsked:
Who is Participating?
 
sarabandeConnect With a Mentor Commented:
Nothing wrong with both having the same name.

that is only true if the function was not used in the scope where the variable is valid. the following gives an error:

int f(int i)
{
   int f = i-1;
   if (f < 0) return 0;
   return f(f);
}

Open in new window

 
because the compiler could not solve the f(f) call.

beside of that it is even possible to use the same name for a type as for a function, for example struct stat and function stat (used to get file status).

#include <sys/stat.h>
....
struct stat fs = { 0 };
if (stat("temp.txt", &fs) != 0)
{
     .... 

Open in new window

c++ compilers may have problems when you define functions/types/variables with ambiguous names. whenever the statement could be mismatched with a forward declaration, the latter wins. you would need namespace and explicit syntax to resolve from errors.

however for the stat example c++ allows to have all type/function/variable the same name:

the following compiles in a c++ source:

struct stat stat = { 0 };
if (::stat("temp.txt", &stat) != 0)
{

Open in new window



Sara
0
 
Minh Võ CôngCommented:
0
 
Minh Võ CôngCommented:
#include <stdio.h>
void my_int_func(int x)
{
    printf( "%d\n", x );
}


int main()
{
    void (*foo)(int);
    foo = &my_int_func;

    /* call my_int_func (note that you do not need to write (*foo)(2) ) */
    foo( 2 );
    /* but if you want to, you may */
    (*foo)( 2 );

    return 0;
}
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
HooKooDooKuConnect With a Mentor Commented:
Nothing wrong with both having the same name.  The compiler has all sorts of rules that deals with "scope" trying to match variable names.

After all, there is nothing wrong with the following code:
int i;  //global variable  

void func1()
{
    i = 1;
}

void func2()
{
    int i;
    i = 2;
}

Open in new window

If you call func1, it will assign a value to the global variable.
But if you call func2, it will assign a value to the local variable.

In the case of a function, because the function is defined outside the function, the variable defined inside the function takes president in a similar manner (i.e. the local variable takes priority over a global function name).

To put it another way... a function is defined outside of the function much like a global variable.  So if you have a local variable inside the function with the same name as the function, the local variable takes priority over the global function name.
0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Thank you HooKooDuKoo and Sara.

So Sara, are you saying that in this function if I don't have a RECURSIVE call to f() that I am OK?  

Also I am actually working in C rather than C++ do your comments still apply?
0
 
sarabandeCommented:
yes, the first two code samples are compiled with ansi c.

if you have

int f(int i)
{
   int f = i-1;
   if (f < 0) return 0;
   return f;
} 

Open in new window


it compiles with c++ and with c without problems because none of the statements is ambiguous.

ambiguity also occurs if defining or using a function pointer of f.

the function name and the variable name f may not be used within the same scope.

Sara
0
 
HooKooDooKuCommented:
I know that Sara's 1st example will work in C++ if you simply include scope.  In this case, simply adding :: in front of the function (or variable) changes the scope from local to global.
int f(int i)
{
   int f = i-1;
   if (f < 0) return 0;
   return ::f(f);
}

Open in new window

I'm not sure if the same syntax holds with a C compiler.  But the idea of 'scope' will still keep you protected.  In other words, because the function 'f' is in the global scope, when you create a local variable 'f' within the function 'f', the compiler will not get confused because it will know that inside the function, the local 'f' variable has priority over the global 'f' function, so the compiler/linker will always properly reference the local 'f' variable inside the function.

IF you needed recursion, then you should still be able access the function 'f' using function pointers like Minh was talking about... something like this:
int f( int i );   //Function declaration
int (*pf)( int i );    //Function pointer declaration

int f( int i )
{                  //start of the actual definition for function 'f'
    int x = (*pf)( i * 2 );  //Call function 'f' using the function pointer
}

main()
{
    pf = &f;  //Assigns the function pointer pf to point at function 'f'
    f( 1 );     //Call function f
}

Open in new window

0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Thanks again Sara and HooKooDuKoo. Good to know we don't have to change our existing code. Sara, I have you "best solution" b/c of the additional info your provided, but HooKooDuKoo, you had the rest of it so you get credit too. :)
0
 
sarabandeCommented:
I'm not sure if the same syntax holds with a C compiler.
no, I tried in a .c source and it fails to compile. the :: syntax is c++ only. it is an enhancement for to explicitly specifying global scope for a function within a member function. in ansi c all functions have global scope and function names may not be ambiguous.

Sara
0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Thanks, Sara.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.