Solved

C: Function name the same as name of local variable

Posted on 2014-03-28
10
394 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 34

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
 
LVL 34

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 34

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

635 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