Created
November 9, 2016 11:04
-
-
Save andrewjbennett/487e745d62a0c2dcc15b3b1b1f864f97 to your computer and use it in GitHub Desktop.
pointer stuff: differences between char* and char[] when used as a stack variable
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
Looking at variable: str1[] | |
&str1[]: -> 0xfff42d72 (address) | |
str1[]: -> 0xfff42d72 (value) | |
*str1[]: -> 0x41 (dereference) | |
Looking at variable: str2[] | |
&str2[]: -> 0xfff42d77 (address) | |
str2[]: -> 0xfff42d77 (value) | |
*str2[]: -> 0x41 (dereference) | |
Looking at variable: strp1 | |
&strp1: -> 0xfff42d68 (address) | |
strp1: -> 0x8048770 (value) | |
*strp1: -> 0x41 (dereference) | |
Looking at variable: strp2 | |
&strp2: -> 0xfff42d6c (address) | |
strp2: -> 0x8048770 (value) | |
*strp2: -> 0x41 (dereference) | |
looking at char *: strp1 == strp2 | |
equal p | |
looking at char []: str1 == str2 | |
not equal |
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> | |
int main() { | |
char str1[] = "ABCD"; | |
char str2[] = "ABCD"; | |
char *strp1 = "ABCD"; | |
char *strp2 = "ABCD"; | |
printf("Looking at variable: %s\n", "str1[]"); | |
printf(" &%s: -> %p (address)\n", "str1[]", &str1); | |
printf(" %s: -> %p (value)\n", "str1[]", str1); | |
printf(" *%s: -> %p (dereference)\n", "str1[]", *str1); | |
printf("Looking at variable: %s\n", "str2[]"); | |
printf(" &%s: -> %p (address)\n", "str2[]", &str2); | |
printf(" %s: -> %p (value)\n", "str2[]", str2); | |
printf(" *%s: -> %p (dereference)\n", "str2[]", *str2); | |
printf("Looking at variable: %s\n", "strp1"); | |
printf(" &%s: -> %p (address)\n", "strp1", &strp1); | |
printf(" %s: -> %p (value)\n", "strp1", strp1); | |
printf(" *%s: -> %p (dereference)\n", "strp1", *strp1); | |
printf("Looking at variable: %s\n", "strp2"); | |
printf(" &%s: -> %p (address)\n", "strp2", &strp2); | |
printf(" %s: -> %p (value)\n", "strp2", strp2); | |
printf(" *%s: -> %p (dereference)\n", "strp2", *strp2); | |
printf("looking at char *: strp1 == strp2\n"); | |
if (strp1 == strp2) { | |
printf("equal p\n"); | |
} else { | |
printf("not equal p\n"); | |
} | |
printf("looking at char []: str1 == str2\n"); | |
if (str1 == str2) { | |
printf("equal\n"); | |
} else { | |
printf("not equal\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment