This stack, String Array and string pointer

Source: Internet
Author: User

I am still working on a small job today and finally set up the framework. I am going to use C language to parse strings from STL. But encountered many problems.
the original stack is not quite familiar. Now I know a little about the difference between the string pointer and the array. Ah
I wrote several simple definition and initialization methods for strings
char * szname1 = "dir \ Ad \ What Are You Doing "; // Q global const region
char szname2 [] = "dir \ Ad \ What Are You Doing "; // stack
char * szname3 = new char [64]; // stack
szname3 = "dir \ Ad \ What Are You Doing ";
char * szname4 = szname2; // stack zone Pointer Points to stack zone memory
Debug, originally, only szname [2] is Char [] type
debug information:

-Szname1 0x00000064c "dir dwhat are you doing" char *
32 ''char
-Szname3 0x00000064c "dir dwhat are you doing" char *
32 ''char
-Szname4 0x0012ff38 "dir dwhat are you doing" char *
32 ''char
-Szname2 0x0012ff38 "dir dwhat are you doing" char [27]
[0] 32'' char
[1] 32'' char
[2] 100 'D' char
......
I understand that:
The pointer only stores the first address of the string, while the string array stores the information of the entire string. The array name can be used to access the first address, but it is not a pointer!

I wrote a function to pass pointer. When I pass szname1, szname3, and szname4 to the address, it is okay to pass & szname2, if the error message is displayed, the array pointer cannot be converted to the pointer.
I have tried to pass a pointer reference to modify the string pointer. szname1, szname3, and szname4 can also be used to transmit the reference, but szname2 still does not work. In fact, I want to get an official explanation, so I will understand it first.

Char amessage [] = "Now is the time";/* an array */
Actually equivalent
Char amessage [16];
Strcpy (amessage, "Now is the time ";
16 is the length of the string "Now is the time" plus 1.
We can see that the memory space is allocated on the stack.

Char * pmessage = "Now is the time";/* a pointer */
The content that pmessage points to is always a constant and cannot be changed.
P = strdup (a) is actually equivalent
P = (char *) malloc (strlen (A) + 1 );
Strcpy (P, );
It can be seen that the pointer returned by strdup points to the newly allocated memory on the heap.
To modify the content in the newly allocated memory space, it is not the content pointed to by.

ProgramCode:

# Include "stdio. H"
# Include "string. H"

Const int max_length = 256;

Void strrestrex (char ** pnewstr, char C)
{
Size_t I = 0;
For (; I <strlen (* pnewstr); I ++)
{
If (* pnewstr) [I]! = C)
{
Break;
}
}
* Pnewstr = * pnewstr + I;
}

Void strrestrex1 (char * & newstr, char C)
{
Size_t I = 0;
For (; I <strlen (newstr); I ++)
{
If (newstr) [I]! = C)
{
Break;
}
}
Newstr = newstr + I;
}

Int main ()
{
Char * szname = "dir \ Ad \ What Are You Doing"; // Q global const Region
Char szname2 [] = "dir \ Ad \ What Are You Doing"; // stack Zone
Char * szname3 = new char [max_length]; // heap
Szname3 = "dir \ Ad \ What Are You Doing ";
Char * szname4 = szname2; // stack zone pointer pointing to stack zone memory
Printf ("% s \ nszname: % d \ n", szname, strlen (szname ));

Char * temp = szname;
Strrestrex1 (szname ,'');
Printf ("% s \ nszname1: % d \ n", szname, strlen (szname ));

Strrestrex1 (szname3 ,'');
Printf ("% s \ nszname3: % d \ n", szname3, strlen (szname3 ));

Strrestrex1 (szname4 ,'');
Printf ("% s \ nszname4: % d \ n", szname4, strlen (szname4 ));

// Strrestrex1 (szname2 ,'');
// Printf ("% s \ nszname: % d \ n", szname2, strlen (szname2 ));
Return 0;
}

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.