C + + Learning char-type string and function, char* do return value __ function

Source: Internet
Author: User

/*2009-11-14 14:27:33
1. The char string ends with the string end symbol '/0 '. The string name is the address of the first character,
So when we pass the string name as a parameter to the function, we actually pass the address into the function.
Since the last character of the string is '/0 ', we do not have to pass the character length, as long as in the function
Set a loop body, the end character as a loop to the end of the condition.
2.
There are three cases of type C strings. 1. The array form holds the string; 2. The pointer points to a string; 3. Unnamed string.
An unnamed string is stored in a literal constant area, and its value is immutable.
And the pointer to a string of characters is pointing to unnamed
String, so the string that the batch points to is also immutable. Only the first, that is, an array of strings.
Because it holds the string in the form of an array, its value can be changed
*/
#include <iostream>
#include <string>
using namespace Std;
int get (const char *p);
int main ()
{
Char ch[15]= "Hello World"; Array in the form of a string, and its value can be changed
Ch[0]= ' s ';
Char *p= "very OK";//Pointer to string
* (p+0) = ' s ' is run with an error.
int ch_length;
Ch_length = Get (CH);
cout<< "ch length is" <<ch_length<<endl;

int P_length
P_length = Get (P);
cout<< "*p length is" <<p_length<<endl;

return 0;

}

int get (const char *p)//Custom Find string length
{
int count=0;
0 ASCII code is 0.
while (*P)
{
count++;
p++;
}
return count;
}
/* Because of the similarity between the array and the pointer, the above function can be changed to the following form *
int get (const char p[]);//Custom ask for string length
{
int count=0;
0 ASCII code is 0.
while (*P)
{
count++;
p++;
}
return count;
}

3. The return value of the function is char*

/* Function How to return a string (char type)
Because a function can return only one value, how many characters are returned. If you return a string, you can find other characters in turn, as long as you get the address of the first character in.
So as long as the address of the first character of the string is returned, it is equivalent to returning the entire string.
We know that the name of the char array is the address of the first character of the string, such as char *p = "study"
*/

#include <iostream>
using namespace Std;
Char *get (char *str); Returning the address of the first character of the string is equivalent to returning the entire string, so the return value is the pointer that holds the address of the first character of the array.
int main ()
{
Char c[10];
Char *ch;
cout<< "Please enter your name." ";
cin>>c;
Ch=get (c); Array
cout<< "Your name is" <<ch<<endl;
delete []ch; Since CH is to point to the function P, delete is an array and cannot forget to write [], otherwise the deletion is only the first address.

Ch=get ("Jack");//"Jack" is an unnamed string
cout<< "Your name is" <<ch<<endl;
delete []ch; Since CH is to point to the function P, delete is an array and cannot forget to write [], otherwise the deletion is only the first address.

  char *ch1 = "Mike";
  ch=get (CH1);//"Ch1" for unnamed string
  cout<< "Your name is" <<CH<<ENDL;
   delete []ch; Since CH is to point to the function P, delete is an array and cannot forget to write [], otherwise the deletion is only the first address.
  return 0;
}
Char *get (char *str)
{
  char *p=new char[strlen (str) +1];//strlen (str) includes only the length of the visible characters, excluding/0. So to strlen (str) +1
  strcpy (P, str);
  cout<<p
  return p;
}

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.