Created
March 9, 2021 20:12
-
-
Save alt-romes/4180db9b5305780e913ba421a92037d2 to your computer and use it in GitHub Desktop.
Example program to explain pointers to a friend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(int argc, char *argv[]) { | |
int arr[10]; | |
int * arrptr; | |
int x; | |
int * xptr; | |
printf("Size of a pointer: %lu\n", sizeof(xptr)); | |
printf("Size of an int: %lu\n\n", sizeof(x)); | |
printf("-- Memory positions --\n"); | |
printf("Value of '&arr': %p\n", &arr); | |
printf("Value of '&arrptr': %p\n", &arrptr); | |
printf("Value of '&x': %p\n", &x); | |
printf("Value of '&xptr': %p\n", &xptr); | |
printf("\n"); | |
arr[0] = 42; | |
arrptr = arr; | |
x = 1; | |
xptr = &x; | |
printf("-- Names values --\n"); | |
printf("Value of 'arr': %p\n", arr); | |
printf("Value of 'arrptr': %p\n", arrptr); | |
printf("Value of 'x': %d\n", x); | |
printf("Value of 'xptr': %p\n", xptr); | |
printf("\n"); | |
printf("-- Pointed by Names values --\n"); | |
printf("Value of '*arr': %d\n", *arr); | |
printf("Value of '*arrptr': %d\n", *arrptr); | |
printf("x is not a pointer"); | |
printf("Value of '*xptr': %d\n", *xptr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment