1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#include <stdio.h>
int main (void)
{
/*
* Arrays in C are just sequential data items
* stored in a memory location
* The address of the array, is the same as the first
* element in the array
* The name of the array is also the address of the array.
*/
char str1[] = "Hello cruel world"; /* \0 is automatically added */
/* Those variables look the same, but they are not. */
/* This is an array */
/*
* str2, is the address of the "ARRAY" which the characters
* of the string are stored
* str2 is not a 'pointer' to the location of the Hello char array
* str2 IS THE LOCATION of the array.
*/
char str2[] = "Hello";
/*
* This is a pointer.
*
* Whose value is not the string, but the - address of that string -
*
*/
char *str3 = "Goodbye";
str3 = NULL;
str2 = NULL;
/*
* &str1, &str1[0] and str1, all points to the very same address.
*
* The array name, str1, is also the address of the array.
* */
printf("%s, %c, %d, %d %d\n", str1, str1[0], &str1, &str1[0], str1);
printf("%p %p %s\n", &str2, str2, str2);
printf("%p %p %s\n", &str3, str3, str3);
}
|