Skip to content

lesson2/25_B_return_pointer.c: misleading C example for pointer semantics #182

@p4sttt

Description

@p4sttt

on slide 61 of lesson 2 (sub-theme Pointers), an example C code snippet is presented to illustrate pointer behavior, specifically returning the address of a local variable

int* get_pointer(void) {
    int i = 42;
    return &i;
}

the slide then juxtaposes this with a Go equivalent, implying similar behavior or direct analogy:

func getPointer() *int {
    var i int = 42
    return &i
}

the C code example for get_pointer() that returns the address of a stack-allocated variable int i immediately leads to Undefined Behavior upon dereferencing the returned pointer in main printf("%d\n", *p);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions