A recent review of experimental data structure, encountered such a line of code, looking a bit uncomfortable.
typedef char** HuffmanCode;
Whereby the comb arrays and pointers.
int* p; //指针变量p中存的地址代表的内存单元中的数据是整型
In the 32-bit platform, since the p address is stored, so the value of p is 32-bit.
Regardless of what type of data pointed to by p, the pointer variable p is itself an integer.
c No string
type, it is generally represented by a character string array.
char str[15] = "Hello World";
c Specifies an array name indicates the first address in the memory array, that is str = &str[0]
, when we generally output string printf("%s",str);
, the first address you can output the whole string.
You can do it in c:
char *s;
s = "Hello";
A string assigned to a pointer variable? ?
In fact, c compilers will allocate memory for string constants, assuming that "Hello"
the address is 0x0000000000404000 0x0000000000404001 0x0000000000404002 0x0000000000404003 0x0000000000404004 0x0000000000404005
, (I am a 64-bit environment)
in fact s = "Hello" = 0x0000000000404000
, c compiler to address this string as initials.
To be a test:
printf("%s\n",s);
printf("%s\n",0x0000000000404000);
这两行的效果是一样的。
char str[10];
char* s;
str
Represents the first address, s
but also save the first address, so it can be:
s = str;
但是不可以 str = s;
Because the array name is a constant that can not be assigned.
In fact, char str[10]
the compiler allocates the memory unit 10, but char* s
only the definition of a pointer variable, allocating 32-bit environment only four bytes used to store the first address of the string.
Do not believe the test:
sizeof(str) = 10;
sizeof(s) = 4; //分配四个字节来保存地址
So array name and pointer variables are essentially different.
Next we look at char**与char* a[]
:
for char* a[]
, a substantially or an array, the array elements are saved char*
type, char*
that is, keep the address of a variable.
So it can be:
char* a[] = {"me","you","him"};
In this case the 32-bit environment sizeof(a) = 12
, since a three elements are in char*
the pointer, and the pointer variable is four bytes.
You can print out on:
printf("%p %p %p\n",a[0],a[1],a[2]);
printf("%p %p %p\n",&a[0],&a[1],&a[2]);
3 array element holds three addresses, which is the address of the first three strings.
For char** s
:
two pointer variables s save a pointer char*
address, we can:
s = a;
Array name a=&a[0]=62FE30
, and this address (ie a [0]) is stored in 404000
the address, which is the string "me"
first address, namely:
*s = 404000 = "me";
Error-prone point 1:
char** s = "Hello";
This is wrong, because s is char**
, and "Hello" is char*
.
Although there are address, but the address "Hello" content is represented by H, char type;
the contents of the address stored in s ( *s
) is a char*
type, pointer type.
Error-prone point 2:
char** s;
*s = "Hello";
So that the compiler can not be wrong, but the runtime printf("%s",*s);
will collapse.
Assume s=0x1000
, in 0x1000
memory of the memory unit is "Hello" address 0x2000
, that is *s = 0x2000
, when the implementation of this first find 0x1000
, then 0x2000
, no problem.
However char** s;
, s memory is a random address, which is the field guide, *s
it may crash.
You must first assign an address:
char** s;
s = (char**) malloc(sizeof(char**));
*s = "Hello";
So there s the available addresses.
Reference:
https://blog.csdn.net/liusicheng2008_liu/article/details/80412586