Search code examples
cpointersdangling-pointer

Does this result in a dangling pointer?


Does such a function result in a dangling pointer?

int *foo () {
    int a[2] = {2, 3};
    int *p = NULL;
    p = a;
    return p;
}

Solution

  • Yes. You are returning a pointer to an array with automatic storage duration. When the function returns the pointer is invalid.