Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
#include <stdio.h> #include <stdlib.h> int main() { // Basic pointer int x = 42; int *ptr = &x; // ptr holds the address of x printf("%d ", *ptr); // dereference: access value at address *ptr = 100; // modify x through pointer printf("%d ", x); // 100 // Pointer arithmetic int arr[] = {10, 20, 30, 40, 50}; int *p = arr; // points to first element for (int i = 0; i < 5; i++) { printf("%d ", *(p + i)); // pointer arithmetic } // Dynamic memory allocation int n = 5; int *dynArr = (int*)malloc(n * sizeof(int)); if (dynArr == NULL) { fprintf(stderr, "Memory allocation failed "); return 1; } for (int i = 0; i < n; i++) dynArr[i] = i * i; for (int i = 0; i < n; i++) printf("%d ", dynArr[i]); free(dynArr); // always free allocated memory! return 0; }
Result
Open