Deep char *, char * *, Char a[], Char *a[]

Source: Internet
Author: User
Tags constant

1 The nature of the array
An array is a collection of multiple elements that are distributed in memory in an address-connected cell, so that you can access elements of different cells by its subscript.


2 pointers.
A pointer is also a variable, except that its memory unit holds an address that identifies another location. Because the address is also an integer, under the 32-bit platform, the pointer defaults to 32-bit.


3 pointer pointing.
The direct meaning of the point is the type of data stored in the other address cells that the pointer variable holds.

int * p;//p variable saved address the data type in the memory cell is integral type

Float *q;//..... ....... ...... ....... ...... ...... ... Floating-point type

The pointer variable itself is always an integral type, regardless of the type of data being pointed to, because the address it holds.


4-character array ...
Literal means an array in which the elements in the array are characters ... Indeed, that is the essence of it.

Char str[10];
An array with 10 elements is defined, and the element type is a character.

Can be initialized when a variable is defined in the C language.

Char str[10] = {"Hello"};

When the compiler encounters this sentence, it fills the hello\0 from the first element in the STR array.

Because there is no real string type in the C language, a string can be represented by a character array because its element address is contiguous, which is sufficient.

The C language stipulates that the array represents the first address of the memory location of the array and is also the address of str[0], that is, str = &str[0];

and printf ("%s", str); Why can I output a string with a first address?

Because there is another key, in C language, the essence of string constants is actually an address, which is more difficult for many beginners to understand the problem ...

Example:

Char *s;

s = "the";

Why can I assign a string to a pointer variable ...

Isn't this type of inconsistency ...

This is the key point mentioned above.

The compiler in C language assigns an address to a string constant if "China" is stored in memory in the 0x3000 0x3001 0x3002 0x3003 0x3004 0x3005.

s = "A", what is consciousness, right, address.

In fact, the real meaning is S = "" "= 0x3000;

Look at it, you think of it as a string, but the compiler sees it as the address 0x3000, that is, the intrinsic expression of the string constant is the address of its first character ...

s = 0x3000

It seems to be more in line with the intuitive meaning ...

Find out the problem.

So%s, it is actually the output string through the first address of the string, printf ("%s", s); It's actually the address of the string that s saved ...

Like what
#include <stdio.h>

int main ()
{
Char *s;
s = "Hello";
printf ("%p\n", s);
return 0;
}

Results:

00422020

You can see S = 0x00422020, which is also the first address of "Hello"

So, printf ("%s", 0x00422020) is also equivalent.



Character array:

Char str[10] = "Hello";

As has been said, str = &str[0] is also equal to the first address of "Hello".

So printf ("%s", str); Nature is also printf ("%s", address);

The operation string in C language is carried out through the first address of the memory cell, which is the ultimate nature of the string ...


5 char * with Char a[];

Char *s;

Char a[];

As mentioned earlier, a represents the first address of a string, and s This pointer also holds the address of the string (actually the first address), the address of the first character, the data in this address cell is a character,

This is also consistent with the char that s points to.

So it can be s = A;
But not a = s;
The array name in C can be copied to the pointer to represent the address, but it cannot be assigned to the array name, which is a constant type, so it cannot be modified.
Of course, you can do this:

char a [] = "Hello";
Char *s =a;
for (int i= 0; i < strlen (a); i++)
printf ("%c", S[i]);
or printf ("%c", *s++);

The character pointer can use the indirect operator * to get its contents, or you can use the subscript form of an array [], and the array name can also be manipulated because it represents an address.

For example, printf ("%c", *a); Will print out ' h '

The essential difference between char * and Char a[]:

When you define char a[10], the compiler assigns 10 cells to the array, and the data type of each cell is a character.

When you define char *s, this is a pointer variable that takes up only four bytes and 32 bits to hold an address.

sizeof (a) = 10;

sizeof (s) =?

Of course it is 4, the compiler allocated 4 bytes 32 bits of space, this space will save the address ...

printf ("%p", s);

This represents the saved address in the cell of S.

printf ("%p", &s);

This represents the variable itself, the address of the deposit. , don't get mixed up.

In a nutshell, the Char *s is just a pointer variable that holds the first address of the string, and Char a[] is a number of contiguous memory units in which the elements in the unit are char, and char * is used to achieve

Char a[] effect, or the nature of the string, address, that is, give you a string address, you can do whatever you want to do. However, the intrinsic properties of char* and Char a[] are not the same.



6 char * * with char *a[];
First look at Char *a[];

Because [] the priority is higher than * so a first and [], he is an array, the element in the array is char *, the previous mentioned char * is a variable, save the address.

So char *a[] = {"French", "America", "German"};

As you can see, the elements in the array are strings, so sizeof (a) is how much, some people think that five words accounted for the total number of bytes in memory 6+7+8+7 = 28;

But in fact sizeof (a) = 16;

Why, as mentioned earlier, string constants are essentially addresses, the elements in a array are char * pointers, and pointer variables are four bytes, then four elements are 16 bytes.

Take a look at the example:

#include <stdio.h>
int main ()
{
Char *a [] = {"A", "French", "America", "German"};
printf ("%p%p%p%p\n", a[0],a[1],a[2],a[3]);
return 0;
}

You can see that the four elements in the array hold four memory addresses, which represent the first address of four strings, not the string itself ...

So sizeof (a) is of course 16.

Note that these four addresses are discontinuous, it is the compiler for "the", "French", "America", "German" allocated memory space address, so, four addresses are not associated.

#include <stdio.h>
int main ()
{
Char *a [] = {"A", "French", "America", "German"};
printf ("%p%p%p%p\n", a[0],a[1],a[2],a[3]); The address saved in the array element
printf ("%p%p%p%p\n", &a[0],&a[1],&a[2],&a[3]);//The address of an array element cell itself
return 0;
}

You can see the 0012ff38 0012ff3c 0012ff40 0012ff44, these four are the address of the element unit, each address is four bytes, because each element is a pointer variable that occupies four bytes ...

Char **s;
char * * is a level two pointer, s to save the address of a pointer char *, about the level two pointer is not discussed in detail here, simply say the error point of the two-level pointer.

Example:
Char *a[] = {"A", "French", "America", "German"};
char **s = A;

Why can I assign a to s because the array name a represents the first address of the memory unit of the array element, that is, a = &a[0] = 0012ff38;

and 0x12ff38 that a[0] is saved is 00422FB8, this address, 00422fb8 as the string "China" the first address.

*s = 00422fb8 = "the";

This allows you to use the data in the S operation

printf ("%s", *s);
printf ("%s", a[0]);
printf ("%s", *a);

It's all the same ...

But note that you can't have a = s, as you said before, a is a constant.

Look at a more error-prone point:

char **s = "Hello World";

This is wrong,

Because the type of S is char * and the type of "Hello World" is char *

Although they are all addresses, they point to a different type, so you cannot use this. , from the nature of it, "Hello World, which represents an address, such as 0x003001, where the content is ' h ', char, and S also holds an address, the content of this address (*s) is char *, is a pointer type, so the two types are different. 。。

What if it is so.

Char **s;
*s = "Hello World";

Seemingly reasonable, compilation is OK, but printf ("%s", *s) will crash

Why??

Let's have a look at it slowly.

printf ("%s", *s); , first you have to have the address of S save, and then find the address of char * in this address, namely *s;

Example:

s = 0x1000;

The "Hello World" address 0x003001, *s = 0x003001, is saved in the memory unit where 0x1000 resides;

So printf ("%s", *s);

This will find 0x1000 first, and then find 0x003001;

if directly char **s;

*s = "Hello World";

The s variable holds an invalid, randomly unavailable address, and no one knows where it is pointing .... , the *s operation crashes.

So when you use Char **s, you assign it a memory address.

Char **s;
s = (char * *) malloc (sizeof (char**));
*s = "Hello World";

So S assigns an available address, such as S = 0x412f;
Then in the memory where 0x412f resides, save the value of Hello World.

Another example:



#include <stdio.h>
void Buf (char **s)
{
*s = "message";
}
int main ()
{
Char *s;
BUF (&s);
printf ("%s\n", s);
}


A simple use of a second-level pointer .... , to be blunt, the level two pointer holds the address of a primary pointer, its type is a pointer variable, and the first pointer holds the address of the memory unit where the data resides, although it is an address, but the type is different ...


Finally, the Sizoof (pointer) size is more computer operating system, the general 32-bit operating system memory size is 4, 64-bit operating system pointer size is 8.

This blog is a turn.

Char **s;
*s = "Hello World";
Print printf on my Ubuntu ("%s", *s), no crash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.