Created
September 22, 2015 17:09
-
-
Save cgmb/c5e25bf7460085a8665b to your computer and use it in GitHub Desktop.
Arrays vs pointers in C
This file contains 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> | |
void print_sizeof(char* ptr) { | |
printf("The passed variable is %u bytes large\n", (unsigned int)sizeof(ptr)); | |
} | |
int main() { | |
char arr[] = "Hello World!"; | |
char* ptr = "Hello World!"; | |
printf("arr is %u bytes large\n", (unsigned int)sizeof(arr)); | |
printf("ptr is %u bytes large\n", (unsigned int)sizeof(ptr)); | |
printf("when passed to a function, an array decays into a pointer.\n"); | |
print_sizeof(arr); | |
print_sizeof(ptr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment