Created
February 15, 2025 16:42
-
-
Save wainejr/a5e4d02c6cfabf7450f285ffc86c7306 to your computer and use it in GitHub Desktop.
Arrays are not pointers
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> | |
#include <stdlib.h> | |
int main(){ | |
int array[3][4]; | |
int* pointer; | |
int** double_pointer; | |
int* random_allocation; | |
pointer = (int*) malloc(12 * sizeof(int)); | |
double_pointer = (int**) malloc(3 * sizeof(int*)); | |
random_allocation = malloc(20 * sizeof(int)); | |
for(int i = 0; i < 3; i++){ | |
free(random_allocation); | |
double_pointer[i] = (int*) malloc(4 * sizeof(int)); | |
random_allocation = malloc((i+1) * 30 * sizeof(int)); | |
} | |
printf("&array[0][1] - &array[0][0] = %p\n", &(array[0][1]) - &(array[0][0])); | |
printf("&array[1][0] - &array[0][0] = %p\n", &(array[1][0]) - &(array[0][0])); | |
printf("&array[1][1] - &array[0][0] = %p\n", &(array[1][1]) - &(array[0][0])); | |
printf("\n"); | |
printf("&pointer[1] - &pointer[0] = %p\n", pointer+1 - pointer+0); | |
printf("&pointer[4] - &pointer[0] = %p\n", pointer+4 - pointer+0); | |
printf("&pointer[5] - &pointer[0] = %p\n", pointer+5 - pointer+0); | |
printf("\n"); | |
printf("&double_pointer[0][1] - &double_pointer[0][0] = %p\n", &(double_pointer[0][1]) - &(double_pointer[0][0])); | |
printf("&double_pointer[1][0] - &double_pointer[0][0] = %p\n", &(double_pointer[1][0]) - &(double_pointer[0][0])); | |
printf("&double_pointer[1][1] - &double_pointer[0][0] = %p\n", &(double_pointer[1][1]) - &(double_pointer[0][0])); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment