Magic C language, this is the C language Daniel operation, as an interview problem, even if the second kill everyone

Source: Internet
Author: User

Of course, the following list of points is the basic use of C, except that these usages may not be noticed at ordinary times. So many things the first time you see, may feel very strange, but carefully think about it can be a good understanding, you can better understand some of the characteristics of C language. But in the specific coding process, I still hope to be able to honestly behave. Because programmers do not need too many edges and corners, it is much more important to write code neatly than to be smart. Here are 5 examples to illustrate some of the problems, if you are a veteran to see this smile, if it is a novice, I believe there will be some inspiration.

1. #和 # #在宏中的作用, as well as with the parameter, the transfer of parameters.

2. Calculation of the offset position of the domain in the structure.

3. Definition of structure and usage of initialization.

4. The equivalence between arrays and pointers in operations.

5. The array is "mutated" in the stack.

1. Example:

Description

A) Pre-compilation # is to convert the right argument to a string, # #是将左右两边的参数连接成一个字符串. The example is the use of #.

B) The parameters in the macro are actually separated by commas (,), the other characters are actually treated as the same parameter, but the line breaks and whitespace are actually processed, so that the parameters are in the same row. Be interested in doing more tests, this usage can be used to write a string containing special characters, lest you write a lot of escape characters (), but the middle can not have commas, hehe ~

2. Example:

Description

A) & (struct _st*) 0) The->b function is to output the offset of B in the _st structure. Why use 0 as a pointer, in fact very good understanding: if the address of a _st struct is 0, then the address of B is actually B in the structure of the offset.

B) In fact, if done first ((struct _st*) 0)->b operation, then the program is definitely abnormal, so the compiler still do the optimization, the specific compiler how to do, I did not delve into.

3. Example:

Description

A) in the initialization of a struct, you can specify the domain to initialize, as in the example. C = 1, the order can be reversed, the advantage of doing so is that the readability is strong, for the initialization of large structure, in reading is very convenient. The disadvantage is that the lower version of the compiler may not be supported.

B) in the declaration of the struct, you can specify the size of the field, such as the int a:1 in the example; Note A only takes up a bit and shows the affinity of C to binary processing.

C) Why the S.C output is 1, not 1, is actually very simple, because 0xFFFFFFFF represents 1, then a 1bit size variable, all bits above are 1, then it also represents-1. So the process of coding, signed and unsigned mixed is actually a very dangerous thing.

4. Example:

Description

a) 0[a] = ' x '; what kind of thing? If you write a[0]= ' x ', you know what it means, but plainly, a[0] and 0[a] look the same in the compiler. Because the array is doing the [] run, it is actually doing the addition of the pointer to run: A[0] equivalent to * (a+0). So 0[a] is also equivalent to * (0+A) is completely correct.

5. Example:

Description

A) Why are the results of the two lines different? In general, as I understand it, the values of an array of A,&a and &a[0] are the same. But when a is in a formal parameter, it is different. In the example, a in the Func function, in fact a variable is in the stack of the Func function, inside the func, a actually has been converted to Char *a, so &a is the address of the pointer variable A in the stack, and &a[0] Represents the address of the first element of the memory space that the pointer points to, in fact the address of the first element of the array that the caller passed in. I don't know, I understand!

B) This may be difficult to understand, the key is to understand that the array as a formal parameter is converted to a pointer view.

I have a public number, often share some of the C language/c++ technology related dry goods; If you like my share, you can use the search "C language learning Tribe" to focus on

Welcome to join thousands of people to exchange questions and answers skirt: 627+012+464

Magic C language, this is the C language Daniel operation, as an interview problem, even if the second kill everyone

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.