Thoughts Caused by char * STR and char STR []

Source: Internet
Author: User

Yesterday I wrote a string to reverseProgramIn the beginning, it is written as follows:

# Include <iostream>
Using Namespace STD;

Char * Reverseword ( Char * Val );

Int Main ()
{
Char * STR = " Hello " ;

Cout <reverseword (STR );

Return 0 ;
}

Char * Reverseword ( Char * Val)
{
Char * Right = Val + strlen (VAL )- 1 ;
Char * Left = val;

// R * Right = val;
// While (* right! = '\ 0 ')
// {
// Right ++;
// }
// Right --;
// Char * temp = 'a ';

While (Right> left)
{
* Left = * left ^ * right;
* Right = * left ^ * right;
* Left = * left ++ ^ * Right --;
}

Return Val;
}

Running result: the program is compiled successfully, but the running crashes.

After debugging, I cannot debug the * Left = * left ^ * right; statement. The following situation occurs:

The debugging process is as difficult as below: http://hi.baidu.com/%C2%ED%D0%C2%CC%CE/blog/item/393421acbfe3b2034b36d6bc.html

Although I have read the Assembly for debuggingCodeBut I still don't know where the problem is. I posted a post and got the correct answer. Well, you know the problem is right under your eyes. Don't be alarmed when you encounter a bug!

# Include <iostream>
Using Namespace STD;

Char * Reverseword ( Char * Val );

Int Main ()
{
Char STR [] = " Hello " ;

Cout <reverseword (STR) <Endl;

Return 0 ;
}

Char * Reverseword (Char * Val)
{
Char * Right = Val + strlen (VAL )- 1 ;
Char * Left = val;

// R * Right = val;
// While (* right! = '\ 0 ')
// {
// Right ++;
// }
// Right --;
// Char * temp = 'a ';

While (Right> left)
{
* Left = * left ^ * right;
* Right = * left ^ * right;
* Left = * left ++ ^ * Right --;
}

Return Val;
}

Running result: olleh

The solution to the problem is: Change char * STR to Char STR []. when the problem is solved, you will know why you should change char * STR to Char STR [].

So the differences between char * STR and char STR [] are as follows:

In fact, my previous articleArticle. Http://www.cnblogs.com/chenyuming507950417/archive/2011/12/29/2306043.html

I suggest you search for the authoritative understanding of online experts.

(1) The two declarations are not the same.

Char PTR [] = "string ";Declares a char array of size7And initializes it with the characters
S,T,R,I,N,GAnd\ 0. You areAllowedTo modify the contents of this array.

char * PTR = "string "; declares PTR As a char pointer and initializes it with address of string literal "string" which is Read-Only . modifying a string literal is an undefined behavior . what you saw (SEG fault) is one manifestation of the undefined behavior.

(2)Char * test = "string test ";Is wrong, it shoshould have beenConst char *. This Code compiles just because of backward comptability reasons. The memory pointedConst char *Is a read-only memory and whenever you try to write to it, it will invoke undefined behavior. On the other handChar test [] = "string test"CreatesWritableCharacter array on Stack. This like any other regualr local variable to which you can write.

After reading the above two articles, everything else is about pointers and arrays.

Original post :( 1) http://topic.csdn.net/u/20120404/20/5ed24daa-6a18-41b0-be25-7df249061976.html

(2) http://topic.csdn.net/u/20120404/19/b0d265db-5f5f-460d-88d4-0f3471a08e8b.html

References:

(1) http://stackoverflow.com/questions/3862842/difference-between-char-str-string-and-char-str-string

(2) http://hi.baidu.com/%C2%ED%D0%C2%CC%CE/blog/item/393421acbfe3b2034b36d6bc.html

(3) http://blog.csdn.net/lzpaul/article/details/2884095

(4) http://dmacy.bokee.com/4728674.html

(5) http://www.cnblogs.com/octobershiner/archive/2012/04/03/2431544.html#2347328 (character string reverse tour)
(6) Chen Hao: http://blog.csdn.net/haoel/article/details/1395358

 

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.