String constants for C + +

Source: Internet
Author: User
Tags function prototype

Today to do a pen test of Baidu, the title is such, the use of C + + language to write a function, to achieve the reversal of the string, requires no system functions, and the time complexity of the least. The function prototype is: Char *reverse_str (char *str).
After a few thoughts, wrote the following code on the VC6.0: #include <stdio.h>char *reverse_str (char *str);

void Main ()
{
char* s = "ABCD";
printf ("%s\n", Reverse_str (s));
}

Char *reverse_str (char *str)
{
     Char *p=str,*q=str,t;
    while (*q!= ')
    q++;
    q--;
    while (P&LT;Q)
    {
        t = *p;
        *p = *q;
        *q = t;
        p++;
        q--;
    }
    return str;
}

As a result, the debug encountered 0xc0000005:access violation error. The understanding of C + + string constants is involved here. 1. Local variables are stored in the stack, 2. Global variables, static variables are stored in static storage, 3.new of memory is in the heap, and 4. String constants are also stored in the static storage area.
Note: The variable memory in the stack is automatically freed with the end of the defined interval, and for the heap, manual free is required. For static stores, the variable constants are persisted during the run of the program, are not freed, and have only one copy.
Char *s = "ABCD"; defines a string constant stored in a static storage area. It cannot be modified by a pointer. In fact, the usual notation is const char *s = "ABCD"; If you want to modify the string contents, you should define the string variable as follows: char s[5] = "ABCD"; note that the character array is 1 more than the number of visible characters because the compiler adds the character ' Identifies as a string end. So, the following test code: Char *str1 = "ads";
Char *str2 = "ads";
Char *STR3 = "ads";
Char str4[4] = "ads";
Char str5[4] = "ads";

if (STR1==STR2)
cout<< "STR1==STR2" <<endl;
if (STR2==STR3)
cout<< "STR2==STR3" <<endl;
if (STR3!=STR4)
cout<< "STR3!=STR4" <<endl;
if (STR4!=STR5)
cout<< "STR4!=STR5" <<endl;
The output is:STR1==STR2
Str2==str3str3!=str4STR4!=STR5
Go back to the beginning of the code, make the following changes, you can successfully execute. #include <stdio.h>Char *reverse_str (char *str);

void Main ()
{
char s[5] = "ABCD";
printf ("%s\n", Reverse_str (s));
}

Char *reverse_str (char *str)
{
    Char *p=str,*q=str,t;
    While (*q!= ')
    q++;
    q--;
    While (P<Q)
    {
        t = *p;
        *p = *q;
        *q = t;
        p++;
        q--;
    }
    return str;
}

String constants for C + +

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.