[Solved] Build code: “strlwr” by pointer [duplicate]


The first example:

char *s1 ="Con Chim Non";

You declare a pointer to a text literal, which is constant. Text literals cannot be modified.

The proper syntax is:

char const * s1 = "Con Chim Non";

Note the const.

In your second example, you are declaring, reserving memory for 100 characters in dynamic memory:

char *s=new char[100];
gets(s);

You are then getting an unknown amount of characters from the input and placing them into the array.

Since you are programming in the C++ language, you should refrain from this kind of text handling and use the safer std::string data type.

For example, the gets function will read an unknown amount of characters from the console into an array. If you declare an array of 4 characters and type in 10, you will have a buffer overflow, which is very bad.

The std::string class will expand as necessary to contain the content. It will also manage memory reallocation.

solved Build code: “strlwr” by pointer [duplicate]