Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lesson2/25_B_return_pointer.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
#include <stdio.h>
// WARNING: The get_pointer() function below contains UNDEFINED BEHAVIOR in C.
// It returns a pointer to a local variable that is destroyed when the function exits.
// This example is intentionally shown to demonstrate how Go differs from C:
// Go handles this safely using automatic memory management,
// while C results in undefined behavior if the pointer is dereferenced.


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

// Note: Running this program may appear to work on some systems due to
// undefined behavior, but it can crash or produce garbage output.

int main(void) {
int *p = get_pointer();
printf("%p\n", p);
Expand Down