Program Console Vignaankosh.com
Execution Panel
Console is empty.
Return pointer from function is an interactive C dry run visualizer from the Pointersfunctions programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.
This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.
#include <stdio.h>
#include <stdlib.h>
int* findElement(int *arr, int size, int target);
// GOOD: Return pointer to static variable (persists between calls)
int* getStaticCounter() {
static int counter = 0;
counter++;
return &counter;
}
// GOOD: Return pointer to dynamically allocated memory
int* createInteger(int value) {
int *ptr = (int*)malloc(sizeof(int));
*ptr = value;
return ptr;
}
// BAD: Never return pointer to local variable!
int* badReturn() {
int local = 42;
return &local; // DANGER! local disappears after function returns
}
int main() {
// Using static variable pointer
int *p1 = getStaticCounter();
int *p2 = getStaticCounter();
int *p3 = getStaticCounter();
printf("Static counter values:\n");
printf("p1 = %d, p2 = %d, p3 = %d\n", *p1, *p2, *p3);
printf("All point to same address: %p\n", p1);
// Using dynamic allocation
int *num = createInteger(100);
printf("\nDynamic allocation: *num = %d\n", *num);
free(num); // Don't forget to free!
// Demonstrate array search returning pointer
int arr[] = {10, 20, 30, 40, 50, 60};
int *found = findElement(arr, 6, 40);
if(found != NULL) {
printf("\nFound element %d at address %p\n", *found, found);
}
return 0;
}
int* findElement(int *arr, int size, int target) {
int i;
for(i = 0; i < size; i++) {
if(arr[i] == target) {
return &arr[i]; // Return pointer to found element
}
}
return NULL;
}