Summary of the C ++ interview questions

Source: Internet
Author: User
Tags array definition

Questions

Explanation

 

The question is wrong. It is: youyuan YunAlgorithmOperator, which refers to the operator in the form of friends.

Two Methods of Operator Overloading: member functions and friends

If a member function has a hidden parameter during overload * This, the answer is C.

If it is a friend, no default parameter exists. The answer is B.

 

This error is mainly because it is not necessary to see clearly. Functions are mainly the return values of function bodies.

 

Array definition and instantiation:

This topic defines a two-dimensional array, which is X [0] and X [1.

So X [1] is the 4 address. * X [1] indicates 4.

In addition, for the initialization method in the definition of X [] [] Two-dimensional, refer

Http://see.xidian.edu.cn/cpp/biancheng/view/152.html

 

Evaluate the meaning of escape characters. \ 0, \ t, \ n, and so on are called escape characters, because the subsequent characters are not their original ASCII characters.
However, another test is that the string ends with \ 0. Therefore, strlen does not detect the character length after \ 0. The size of the St is the size of the array 20.

 

Or the question of the string: What is the length of a character?
I think the answer is wrong,
Of course, I use sizeof to get 4. Use strlen to get 3.
We can know that sizeof indicates that the length of char * includes \ 0.
Strlen ends at \ 0 and does not include it.
 

This question has been encountered during the interview. Below are several situations:

Int arr [3]

1. pointer Array

Int * P [3]

This defines an array of pointers. The elements are three pointers pointing to three int-type addresses respectively.

EquivalentInt * (p [3])

2. pointer

INT (* P) [3]

Defines a pointer pointing to the address of an array with three elements

For example, P = & arr;

Operator priority:

In my opinion, it is hard to remember, but generally it should be remembered that: for example, the assignment operator, &, |, and so on have a lower priority.

() The highest priority.

Http://en.cppreference.com/w/cpp/language/operator_precedence

For more information about the priority table, see.

Question :! The priority is higher than &, so ,! If X is true, then calculate the true value following. In addition, for & and |, there is also the ability of the compiler to optimize computing. For example, if the value before & is false, there is no need to calculate the subsequent value. In fact, the compiler works like this.

The following questions

We know that () has the highest priority, = the value assignment operation is relatively low, and the comparison operation is relatively high. So, & the previous value

Is (M = A> B)

The first step is to calculate the order.> the value is higher than the value assignment, so m = 0 is obtained. Then it is the return value after the value assignment,

Int operator = (INT) returns the value of the value, which is 0.

Therefore, the value of the entire expression is 0 and the subsequent value is not executed.

 

Another question is that people think the test is quite good. I will post it here:

Reply: 21:25:10
Void getmemory (char * P)
{
P = (char *) malloc (100 );
}
The p Parameter copies the value of the original pointer instead of the original pointer. Therefore, even if P points to the new address again, the original Pointer Points to the address unchanged.
Char * getmemory (void)
{
Char P [] = "helloworld ";
Return P;
}
P is a local variable. After leaving the scope, the stack space is recycled and the result is unpredictable.
Void getmemory2 (char ** P, int num)
{
* P = (char *) malloc (Num );
}
P is the pointer to the passed pointer. * P is the value assigned to the passed pointer. Therefore, the value of the passed pointer can be changed as follows:
Void getmemory (char * & P)
{
P = (char *) malloc (100 );
}
Same effect

 

 

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.