C language and C ++ interview sites

Source: Internet
Author: User

1. Solving

[CPP]
View plaincopyprint?
char str[100] = "Hello World";sizeof(str) = ?;srlen(str) = ?char str1[100] = "Hello\0World";strlen(str1) = ?

 

Test site:

Sizeof function and strlen function. The sizeof function is used to calculate the memory space occupied by STR strings in the memory. When STR is defined, it applies for 100 bytes in the memory, so sizeof (STR) = 100; the strlen function calculates the actual length of the string, which is the length of the first '\ 0' in the string, so strlen (STR) = 10 (not including' \ 0'), strlen (str1) = 5.

Note: the string length obtained by the strlen function does not contain '\ 0 '.

The strlen function calculates the length from the string to the First '\ 0'.

 

 

 

2. Solving

[CPP]
View plaincopyprint?
char str[] = "Hello";sizeof(str) = ?;strlen(str) = ?;

 

Test site: string.

If the string ends with '\ 0', one more byte is allocated in the memory to save' \ 0', but the array does not indicate the size, the compiler judges the value in [] based on the following "H, E, l, l, O, \ 0", then sizeof (STR) = 6; if the length obtained by strlen does not include '\ 0', strlen (STR) = 5.

 

 

 

3. Solution

[CPP]
View plaincopyprint?
void *p0;int *p1;char *p2;long *p3;float *p4;double *p5;sizeof(p0) = ?;sizeof(p1) = ?;sizeof(p2) = ?;sizeof(p3) = ?;sizeof(p4) = ?;sizeof(p5) = ?;

 

Test site: pointer length.

Because pointer of any type occupies 4 bytes in memory, sizeof (P0), sizeof (P1), sizeof (P2), sizeof (P3), sizeof (P4) and sizeof (P5) are equal to 4.

 

 

 

4. Solving

[CPP]
View plaincopyprint?
Char * STR = "hello"; sizeof (STR) = ?; Strlen (STR) = ?; In memory? Bytes

 

Test site: pointer and string.

STR is a pointer, then sizeof (STR) = 4; STR points to 5 characters in length, then strlen (STR) = 5; string ends with '\ 0, therefore, STR is actually stored in memory as "H, E, l, l, O, \ 0", then STR occupies 6 bytes in memory.

 

 

 

5. Incorrect search

[CPP]
View plaincopyprint?
void test1(){ char stri10]; char *str1 = "0123456789"; strcpy( str, str1 );}

 

Test site: array out of bounds.

Str1 occupies 11 bytes (the string ends with '\ 0'), while the STR array has only 10 bytes of memory. The STR array cannot store str1 strings, which may result in an out-of-bounds array.

 

 

 

6. Incorrect search

[CPP]
View plaincopyprint?
char s1[10] = "Hello";char s2[20];s2 = s1;

 

Test site: array assignment.

Because S2 is a character array, you cannot use another character array to directly assign values with equal signs. You should use strcmp to assign values, that is, strcpy (S2, S1 ).

 

 

 

7. Solving

[CPP]
View plaincopyprint?
String STR; CIN> STR; (input Hello World) cout <STR <Endl;

 

Test site: String CIN mechanism.

(1) read and ignore all leading blank characters (such as spaces, line breaks, tabs, etc.); (2) read the characters until a blank character is encountered again, and the read ends. Then, STR uses cout to output hello.

 

 

 

8. Solving

[CPP]
View plaincopyprint?
void Test(char str[100]){    cout << sizeof(str) << endl;}

 

Test site: pointer.

In the test (char STR [100]) function, as a form parameter, the array name in the function will lose its meaning and is just a pointer. At the same time, STR also loses its constant feature in the function body and can be auto-incrementing, auto-subtracted, or modified. Because STR is only a pointer, sizeof (STR) = 4.

 

 

 

9 solving

Write the expression for comparing Boolean variables with zero values

 

Test site: Boolean variable.

Assuming that the Boolean variable is named flag, the standard if statement for comparing it with the zero value is as follows:

[CPP]
View plaincopyprint?
If (FLAG) // indicates that flag is true if (! Flag) // indicates that the flag is false.

 

 

 

10 solving

Write two uses of const

 

Test site: const

(1) const constants can be defined.

(2) const can modify function parameters, return values, and even function definitions. All things modified by const are protected by force, which can prevent unexpected changes and improve program robustness.

Related Article

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.