Solved

C: Function name the same as name of local variable

Posted on 2014-03-28
10
382 Views
Last Modified: 2014-04-02
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
0
Comment
Question by:Stephen Kairys
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 39963142
0
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 39963144
#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
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 250 total points
ID: 39966868
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
 
LVL 32

Accepted Solution

by:
sarabande earned 250 total points
ID: 39969082
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
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 39969618
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 32

Expert Comment

by:sarabande
ID: 39969749
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
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 39969768
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
 
LVL 4

Author Closing Comment

by:Stephen Kairys
ID: 39969867
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
 
LVL 32

Expert Comment

by:sarabande
ID: 39971871
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
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 39972153
Thanks, Sara.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now