Shanghai Bell's written test topic (attached) answer)

xiaoxiao2021-03-06  110

Several procedures about C language

1. Please fill in the IF statement of BOOL, FLOAT, pointer variables and "zero" comparison. (10 points) Please write the IF statement of BOOL FLAG and "Zero" comparison. (3 points) Standard answer: if (flag) if (! Flag) is a bad style, not score. IF (Flag == True) IF (Flag == 1) if (FLAG == FALSE) IF (FLAG == 0) Please write the IF statement of Float X and "Zero" comparison. (4 points) Standard answer example: const float epsinon = 0.00001;

IF ((x> = - epsinon) && (x <= epinon) Do not use "==" or "! =" with digital comparison, you should try to translate into "> =" or "<=" In the form. The following is the wrong way of writing, no score. If (x == 0.0) if (x! = 0.0) Please write the IF statement of char * p and "zero" comparison. (3 points) Standard answer: IF p == NULL) IF (p! = null) is a bad style, no points. IF (p = 0) IF (p! = 0) if (p) IF (!) 2, here is Windows NT The lower 32-bit C program, calculate the value of SizeOf (10 points) char str [] = "Hello"; char * p = str; int N = 10;

Calculate SizeOf (STR) = 6 (2 points) SIZEOF (P) = 4 (2 points) SIZEOF (N) = 4 (2 points) Void Func (CHAR STR [100]) {please calculate SIZEOF (STR) = 4 (2 points)} void * p = malloc (100); please calculate sizeof (p) = 4 (2 points) three, short answer questions (25 points) 1, what is IFNDEF / Define / Endif in the header file? (5 points) Answer: Prevent the header file from being repeatedly referenced. 2, # DE? And #include "filename.h" What is the difference? (5 points)

A: For #include?, The compiler starts searching for FileName.h from the standard library path to #include "filename.h", the compiler starts searching for FileName.h from the user's work path

3, what is the use? (Please explain at least two) (5 points) Answer: (1) You can define a Const Constant (2) Const can modify the parameters of the function, return the value, and even the definition body. Things that are modified by const are mandatory, prevent accidents, can improve the robustness of the program. 4. Call the function compiled by the C program, why do you want to add externaln "c"? (5 points) Answer: C language support function overload, C language does not support function overload. The function is compiled by C and the name in the library is different from the C language. Assuming a function of a function is: Void foo (int X, int y); the function is compiled by the C compiler to _foo in the library, while the C compiler produces a name like _foo_int_int. C provides a C-connection exchange specifying symbol EXTERN "C" to solve the name match problem. 5. Please briefly describe the advantages and disadvantages of the following two for cycles (5 points) for (i = 0; i

Optimize the loop to reduce efficiency. Advantages: high efficiency of cycles

Disadvantages: The program is not simple, the story of memory (5 points per small question, a total of 20 points) Void getMemory (char * p) {p = (char *) malloc (100);} void test (void) {char * str = null; getMemory (STR); STRCPY (STR, "Hello World"); Printf (STR);} What kind of results will there be a Test function? A: The program crashes. Because GetMemory does not pass dynamic memory,

The STR in the TEST function has always been NULL. STRCPY (STR, "Hello World"); will make the program crash. Char * getMemory (void) {char p [] = "Hello World"; returnom p;} void test (void) {char * str = null; str = getMemory (); printf (str);}, please ask the Test function What kind of results? A: It may be garbled. Because getMemory returns a pointer to "stack memory", the address of the pointer is not null, but its original content has been cleared, and the new content is unknown. Void getMemory2 (char ** p, int num) {* p = (char *) Malloc (NUM);

} Void test (void) {char * str = null; getMemory (& STR, 100); STRCPY (STR, "Hello"); Printf (STR);} What kind of results will there be a Test function? Answer: (1) Output Hello (2) memory leak void test (void) {char * str = (char *) malloc (100); strcpy (Str, "Hello"); Free (STR); if (STR! = NULL) {STRCPY (STR, "World"); Printf (STR);}} What kind of results will there be a Test function? A: Tamper with the content of the dynamic memory area, the consequences are difficult to expect, very dangerous. Because Free (STR); then, the STR becomes a wild pointer, if (str! = Null) statement does not work. 5. Writing a strcpy function (10 points) The prototype of the strcpy function is char * strdest, const char * strsrc); STRDEST is the destination string, and strsrc is a source string. (1) Do not call C / C string library functions, please write function strcpy char * struct (char * strdest, const char * strsrc); {assert ((strDest! = Null) && (strdst! = Null); // 2 points char * address = strDest; // 2 points while (* strDest = * strsrc )! = '/ 0') // 2 points null; return address; // 2 points}

(2) STRCPY can copy the contents of strsrc to strDest, why should the return value of the type?

A: In order to achieve a chain expression. // 2, such as Int Length = Strlen (STRDEST, "Hello World"); Sixth, write String constructor, destructor and assignment function (25 points) Known String prototype: Class String {Public: string (const char * str = null); // Ordinary constructor string (const string & other); // copy constructor ~ string (void); // Destructor String & Operate = (const string & other) ; // Assign value private: char * m_data; // Used to save string}; please write the above four functions of String. Standard answer: // String destructor

String :: ~ string (void) // 3 points {delete [] m_data; // Since M_DATA is the internal data type, it is also possible to write into delete m_data;} // String ordinary constructor string :: string (const char * string ) // 6 points {if (str == null) {m_data = new char [1]; // If you can add NULL to determine, better * m_data = '/ 0';} else {Int length = Strlen (STR) ; M_data = new char [length 1]; // If you can add NULL to determine, better strcpy (m_data, str);}} // Copy constructor string :: string (const string & other) // 3 points {INT Length = strlen (other.m_data); m_data = new char [length 1]; // If you can add NULL to determine, better strcpy (m_data, other.m_data);} // Assignment function string & string :: Operate = (const string & other) // 13 points {// (1) Check self-assignment // 4 points if (this == & other) return * this; // (2) Release the original memory resources // 3 points DELETE [ ] m_data; // (3) Assign new memory resources, copy content / 3 points int length = strlen (other.m_data); m_data = new char [length 1]; // If you can add NULL to judge more Good strcpy (m_data, other.m_data); // (4) Return to this object / 3 points

转载请注明原文地址:https://www.9cbs.com/read-125507.html

New Post(0)