Skip to contentSkip to conversation

Issue "address of local variable returned"

DISCUSSION Amit Dang 1 participant 1 message May 25, 2005

lore
Share & tools
lore

Amit Dang

author
#0 lore raw

Hi All,
I am facing following issue

Following piece of code gives warning: "address of local variable `op' returned"
#################################
#include<iostream>
using namespace std;

struct s {
    char op[24];
    int j;
};

char* fun()

{
    struct s op;
    strcpy(op.op, "898898898\0");
    return op.op;
}

int main()

{
    cout << fun() << endl;
    return 0;
}

#################################
But when the structure is change no warning is issued Following is the new structure
#################################

struct s {
        int j;
        char op[24];
};

#################################

Obviously I know that "op.op" should not be returned.

Thanks in advance,
Amit Dang