Interesting things in C Language _ classical interview question series _ 2

Source: Internet
Author: User

Recently, I have always felt that my body is not as good as a day, and I have always lost myself. Oh, years ..................

Here is a suggestion for children's shoes still being educated in China ............. when you are at school, you are still playing games. When you are at work, you will find that

Not only is there no time for the cost, but the cost of the body is no longer ..........

The last time I talked about some interesting interview questions, today we will look at some interview questions about pointers and sizeof:

Below are some interview questions I have seen

5. Brief Introduction
Char * const p;
Char const * p;
Const char * p;
.

First, we will discuss:
Char * const p; here we can know that the const modifier is the pointer Variable p; therefore, the pointer p cannot be changed.
The definition must be initialized first, otherwise it cannot be compiled. However, the storage content of the variable pointed to by p can be changed.
Exp:
Char chTest1;
Char chTest2;
Char * const p = & chTest1;
// P = & chTest2; this field is incorrect and cannot be assigned as a value.
* P = 'C ';
ChTest1 = 'B ';
Next we will discuss:
Char const * p; here we can know that const modifies * p; therefore, we can know that the value of the variable cannot be changed through * p pointing;
The pointer p can be changed, and the value can be changed through the original variable.
Exp:
Char chTest1;
Char chTest2;
Char const * p = & chTest1;
P = & chTest2;
ChTest = 'a ';
Here we can know: 1) the point of p can be changed.
2) A value cannot be assigned through * p =.
3) The value of the variable pointed to by the pointer can be changed by itself.

Last discussed:
Const char * p = & chTest1; here we can know that const modifies * p; therefore, this is the same as char const * p.
Exp:
Char chTest1;
Char chTest2;
Const char * p = & chTest1;
P = & chTest2;
ChTest1 =;
// * P = a; this location is incorrect and cannot be modified like this.

Summary:
The modifier only takes effect on the modified object and does not take effect on other variables.

6. Are there any problems with the usage of the two sizeof in the following code?
Void UpperCase (char str []) // converts lowercase letters in str to UpperCase letters.
{
For (size_t I = 0; I <sizeof (str)/sizeof (str [0]); ++ I)
If (a <= str [I] & str [I] <= z)
Str [I]-= (a-);
}
Char str [] = "aBcDe ";
Cout <"str character length:" <sizeof (str)/sizeof (str [0]) <endl;
UpperCase (str );
Cout <str <endl;
Exp:
Sizeof is used to investigate this problem. So how should we look at this issue?
1. The first thing we need to know here is that sizeof is an operator with an operation Priority. Where is the sizeof operation priority?
You can see the definition of operator priority: sizeof has the same priority as * p (P is the pointer) and & p (variable, take the address), ranking second in the priority.
2. Understand the role of the sizeof keyword.
The sizeof keyword returns the number of bytes occupied by the operands in the memory.
The operation object of the sizeof operator is as follows:
1) type keyword
Sizeof (int): Depends on the system definition. When it is on a 32-bit machine; sizeof int = 4
On a 16-bit server, sizeof int = 2
2) variables
Int iTest;
Sizeof iTest; in this way, the space occupied by the variable type in the memory is returned.
That is:
Sizeof iTest = sizeof int;
3) String Literal Value
Sizeof "abcd ";
The returned result is:

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.