Skip to content

Instantly share code, notes, and snippets.

@cgmb
Created September 22, 2015 17:09
Show Gist options
  • Save cgmb/c5e25bf7460085a8665b to your computer and use it in GitHub Desktop.
Save cgmb/c5e25bf7460085a8665b to your computer and use it in GitHub Desktop.
Arrays vs pointers in C
#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