function pointers and pointer functions

Source: Internet
Author: User

#include <iostream>

using namespace Std;

Int (*ptr) (int a,int b);
int fun (int x,int y)
{
int z;
z=x>y?x:y;
return z;
}

int fun3 (int a)
{//Pass a copy
cout<< "* * *" <<endl;
return a+100;
}
int main ()
{
Ptr=fun; The function pointer, essentially a pointer, has the same return type and argument list as the indicated function
int a=0,b=0;
for (int i=0;i<6;i++)
{
cin>>b;
A=ptr (A, b);
}
cout<<a<<endl;
Fun3 (a);
cout<<a<<endl;
}

Pointer functions

Functions that return pointers

#include <iostream>
#include <string>

using namespace Std;

Char *fun (String str)
{

int Len=str.length ();
Char *str2=new char[len+1];
for (int i=0;i<len;i++)
{
Str2[i]=str[i];
}
str2[len]= ' + ';
return str2;

}
int main ()
{
String s= "What is the fuck";
Cout<<fun (s) <<endl;
}

Note that in C + + the string ends with ' str2[len]= ', and if the above does not add ' n ', then Cout<<fun (s) <<endl; may output a lot of useless garbled

There are also member variables in the C + + class that prohibit initialization. Private variables must not be accessed directly

Knowledge Supplement: for reproduced content

The-S is the end of a string in C + +, stored at the end of the string, although it does not count toward the string length, but takes up a byte of memory space. In the Baidu encyclopedia to see the entry, there will be such a sentence: C + + is specified in the end of the string "". Some might think that in C (c + + would be different), ' "" is not a character type, but an int type. Here, we would like to keep in line with the Baidu entry author, that the "and" is equivalent. Because of the different number of bits in different processors, '-s ' is not necessarily a 8-bit 00000000. In fact, because of the different bits of the processor, sizeof (int) returns the results are different, and sizeof (char) return the result is generally 1, for 8-bit machine, a byte consists of 8 bits, 16 bits of a byte is composed of 16 bits, we usually use the computer is usually 32-bit, That is, a byte is made up of 32 bits, which is now a 64-bit machine. The CPU is typically read in bytes as a unit. But generally we still agree that 1Byte equals 8bit, because this is the smallest number of digits.

' \ s ' is an escape character, meaning to tell the program that this is not the number 0. ' + ' and 2 are basically generic, for example: string[i]! = ' + ' and string[i]! = 0 are the same. However, the type of '-s ' is char, and 0 is of type int, so on most computers, sizeof (0) = 4 and sizeof ('% ') = 1, which is not universally available in special cases. In addition, ' 0 ' is also different, they are characters, but their ASCII code is different: ' "" ASCII code value of 0, ' 0 ' can also be written as ' \0x30 ' ASCII value of 48.

There is no specific string variable in the C language, usually a character array to hold a string. The string always takes '% ' as the terminator of the string. So when a string is stored in an array, it is also stored in the array as the Terminator, and as a flag for whether the string ends. With the ' \ S ' flag, it is not necessary to use the length of the character array to determine the length of the string.

'/' is the string end flag. For example, assign a string to the array: char str1[] = {"welcome!"}. In fact, the actual storage of the array str1 in memory is: ' W ' e ' ' l ' ' C ' o ' m ' E '! ' '/'. The following ' \ ' is automatically added by the C compiler system. Therefore, it is generally not necessary to specify the length of the array when assigning the initial value to the string, which is handled by the system itself. Copies the string from the character array str1 to the character array str2. The end of the string is also copied together.

But...... There are some exceptions. For example, suppose we specify an array length but the array length is not enough.

such as: char str1[8] = {"welcome!"}. Since the character group STR1 has a length of 8, the following information is lost, that is, '% ' is lost.

In addition, if you assign a value to an array, each character is enclosed in quotation marks, and you are lost.

For example: char str1[] = {' W ', ' e ', ' l ', ' C ', ' O ', ' m ', ' e ', '! '};

If you want the array to end with ' \ n ', either write: char str1[] = {"welcome!"};

Either write (manually add ' m '): char str1[] = {' W ', ' e ', ' l ', ' C ', ' O ', ' m ', ' e ', '! ', ' + '};

Either write (intentionally reserve an empty space for the array): char str1[9] = {' W ', ' e ', ' l ', ' C ', ' O ', ' m ', ' e ', '! ', ' + '};

Finally, interested children's shoes can think about the difference between '/' and ' null ', and you will find that there are a lot of details that we don't notice in C.

function pointers and pointer functions

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.