Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable scope inside while loop

Tags:

c

variables

scope

This is perhaps one of the most odd things I've ever encountered. I don't program much in C but from what I know to be true plus checking with different sources online, variables macroName and macroBody are only defined in scope of the while loop. So every time the loop runs, I'm expecting marcoName and macroBody to get new addresses and be completely new variables. However that is not true.

What I'm finding is that even though the loop is running again, both variables share the same address and this is causing me serious headache for a linked list where I need to check for uniqueness of elements. I don't know why this is. Shouldn't macroName and macroBody get completely new addresses each time the while loop runs?

I know this is the problem because I'm printing the addresses and they are the same.

while(fgets(line, sizeof(line), fp) != NULL) // Get new line
{
    char macroName[MAXLINE];
    char macroBody[MAXLINE];

    // ... more code

    switch (command_type)
    {
        case hake_macro_definition:
            // ... more code

            printf("**********%p | %p\n", &macroName, &macroBody);
            break;

        // .... more cases
    }
}

Code that is part of my linked-list code.

struct macro {
    struct macro *next;
    struct macro *previous;
    char *name;
    char *body;
};    

Function that checks if element already exists inside linked-list. But since *name has the same address, I always end up inside the if condition.

static struct macro *macro_lookup(char *name)
{
    struct macro *temp = macro_list_head;

    while (temp != NULL)
    {
        if (are_strings_equal(name, temp->name))
        {
            break;
        }    

        temp = temp->next;
    }

    return temp;
}
like image 663
ThePedestrian Avatar asked Mar 10 '13 02:03

ThePedestrian


People also ask

What is the scope of the variable for a while loop?

when while loop is executed - the variable i defined inside while is having local scope, where as the variable under (i > 3) follows the global variable, and doesn't refer to local scope.

Can you declare a variable in a while loop?

Yes. you can declare a variable inside any loop(includes do while loop.

Do while loops have their own scope?

As with while loops, until is not a method. Therefore, until loops do not have their own scope.

Are variables in loops local Python?

In Python, on the other hand, variables declared in if-statements, for-loop blocks, and while-loop blocks are not local variables, and stay in scope outside of the block.


1 Answers

These arrays are allocated on the stack:

char macroName[MAXLINE];
char macroBody[MAXLINE];

The compiler has pre-allocated space for you that exists at the start of your function. In other words, from the computer's viewpoint, the location of these arrays would the same as if you had defined them outside the loop body at the top of your function body.

The scope in C merely indicates where an identifier is visible. So the compiler (but not the computer) enforces the semantics that macroName and macroBody cannot be referenced before or after the loop body. But from the computer's viewpoint, the actual data for these arrays exists once the function starts and only goes away when the function ends.

If you were to look at the assembly dump of your code, you'd likely see that your machine's frame pointer is decremented by a big enough amount for your function's call stack to have space for all of your local variables, including these arrays.

like image 170
chrisaycock Avatar answered Sep 28 '22 02:09

chrisaycock