Typical pointer pen questions

Source: Internet
Author: User

1. Write the output of the Program (2012 sogou school recruitment test)

char *c[] = { "ENTER", "NEW", "POINT", "FIRST" }; char **cp[] = { c+3, c+2, c+1, c }; char ***cpp = cp; int main(void){ printf("%s", **++cpp); printf("%s", *--*++cpp+3); printf("%s", *cpp[-2]+3); printf("%s\n", cpp[-1][-1]+1); return 0;}

 

Pointers are cumbersome and should be fine-tuned. The analysis is as follows:

The first output is as follows:

The second output is as follows:

The third output is as follows:

 

The fourth output is as follows:

The final result is pointerstew.

Reference: http://blog.csdn.net/hopeztm/article/details/8008345

 

2. How do I store an integer 0x100 on the memory address 0x12ff7c?

int *p = (int *)0x12ff7c;*p = 0x100;

Or

*(int*) 0x12ff7c = 0x100;

3,

int a[3]; a[0]=0; a[1]=1; a[2]=2; int *p, *q; p=a; q=&a[2]; //a[q-p] = ?

 

Answer: A [q-p] = 2;

Q is the address of a [2], p is the address of a [0], the difference between the two addresses p-Q is the address difference between a [2] and a [0, then a [q-p] = A [2]

Q = A; // q = & A [0];

Q = P + 2; // q = & A [2];

This makes it easier to understand

 

4. What is the output value below?

#include <stdio.h>void func(int* ptr, int &value){ptr = &value;}int main(){int i = 10, j = 5;int *ptr = &i;func( ptr, j);printf("%d", *ptr);return 0;}

The answer is: 10.

This mainly involves the function parameter problem. Like the int type, the pointer also has the reference problem. If the preceding function declaration is changed:

Void func (int * & PTR, Int & value );

The answer is 5.

 

5. Fill in the blanks:

struct Test{int Num;char *pcName;short sDate;char cha[2];short sBa[4];}*p;

Assume that the value of P is 0x100000. What are the values of the following table expressions?

P + 0x1 = 0x ___?

(Unsigned long) P + 0x1 = 0x ___?

(Unsigned int *) P + 0x1 = 0x ___?

 

Answer:

The value of P + 0x1 is 0x100000 + sizof (TEST) * 0x1. The size of this struct is 20 bytes, and the memory is aligned. Therefore, the value of P + 0x1 is 0x100014. (The hexadecimal value of 20 is 14)

(Unsigned long) What is the value of P + 0x1? Forced conversion is involved here. The value saved by the pointer Variable P is forcibly converted to an unsigned long integer. Once a value is forcibly converted, its type changes. Therefore, this expression is actually an unsigned long integer plus another integer. Therefore, the value is 0x100001.

(Unsigned int *) What is the value of P + 0x1? Here, P is forcibly converted into a pointer pointing to an unsigned integer. So the value is: 0x100000 + sizof (unsigned INT) * 0x1, equal to 0x100004.

6,

Use variable A to give the following definition
A) An integer
B) a pointer to an integer (a pointer to an integer)
C) a pointer to a pointer pointing to an integer (a pointer to an intege) r
D) an array of 10 integers (an array of 10 integers)
E) an array with 10 pointers pointing to an integer. (An array of 10 pointers to integers)
F) A pointer to an array with 10 integers (a pointer to an array of 10 integers)
G) a pointer to a function. The function has an integer parameter and returns an integer number (a pointer to a function that takes an integer as an argument and returns an integer)
H) An array with 10 pointers pointing to a function, this function has an integer parameter and returns an integer (an array of ten pointers to functions that take an integer argument and return an integer)

The answer is:
A) int A; // an integer
B) int * A; // a pointer to an integer.
C) int ** A; // a pointer to the Pointer a pointer to an integer
D) int A [10]; // an array of 10 Integers
E) int * A [10]; // an array with 10 pointers of 10 pointers to Integers
F) int (* A) [10]; // a pointer to an array of 10 integers.
G) int (* A) (INT); // a pointer to the function a pointer to a function a that takes an integer argument and returns an integer
H) int (* A [10]) (INT); // an array with 10 pointers, points to an integer and has an integer parameter an array of 10 pointers to functions that takes an integer argument and return an integer

 

7. What is the difference between reference and pointer?

Answer: There are three differences:

(1) direct access to a variable is referenced, while indirect access to a pointer.

(2) A reference is the alias of a variable. Instead of allocating its own memory space, the pointer has its own memory space.

(3) The reference is bound to a memory space at the beginning (initial values must be assigned at the beginning). Therefore, the reference can only be the name of the memory space, but cannot be changed to another one, of course, the value of this memory space can be changed.

 

8. How can I determine whether a program is compiled by C or C ++?

#ifdef __cplusplus  cout<<"c++";  #else  cout<<"c";  #endif   

9. Relationship between reference and Polymorphism

References are another method that can produce polymorphism effects except pointers. This means that a base class reference can point to its derived class instance. (Improper answers)

 

10. What are the features of using "Reference" as a function parameter?

(1) passing a reference to a function has the same effect as passing a pointer. In this case, the parameter of the called function becomes an actual parameter variable or an alias of the object in the original main function, therefore, the operations on the parameters in the called function are the operations on the corresponding target object (in the main function.

(2) using the reference parameter to pass the function does not generate a copy of the real parameter in the memory, it is directly to operate on the real parameter; while using the general variable to pass the function parameter, when a function call occurs, you need to allocate storage units to the parameters. The parameters are copies of the real variables. if the object is passed, the copy constructor will also be called. Therefore, when the data transmitted by a parameter is large, it is better to use reference than to transmit a parameter using a common variable.

(3) Although using pointers as function parameters can achieve the same effect as using references, in the called function, storage units must be allocated to the parameters, the "* pointer variable name" format must be used repeatedly for calculation, which can easily lead to errors and the program reading is poor. On the other hand, at the call point of the main function, the variable address must be used as the real parameter. References are easier to use and clearer.

11. Why must the input parameter of the copy constructor be of the reference type?

Assuming that the copy constructor parameters are passed as values, the compiler will call the copy constructor to store the real parameters in a copy, which leads to an endless loop.

12. When to use regular reference (const &)

If you want to use references to improve program efficiency and protect the data transmitted to the function from being changed in the function, you should use regular references. If it is not a const reference, pay attention to the specification. Please refer to the code below

 

 

So under what circumstances will temporary variables be generated? In two cases: 1. the type of the Shape participates in the real parameter is different. 2. It is not the left value.

 

13. If you do not run the program, what is the output of the following code?

#include<iostream>using namespace std;int main(){char *str[]={"welcome","to","Fortemedia","Nanjing"};char**p=str+1;str[0]=(*p++)+2;str[1]=*(p+1);str[2]=p[1]+3;str[3]=p[0]+(str[2]-str[1]);cout<<str[0]<<endl;cout<<str[1]<<endl;cout<<str[2]<<endl;cout<<str[3]<<endl;return 0;}

Answer:

First, declare a pointer array in line 5, and the STR value is the starting address value of the array. The STR value is parsed into the char ** variable value by default.

Line6 declares a 2-level pointer, char ** P = STR + 1; Because STR is Char ** type, that is, STR points to char * type, so the value of P is (INT) STR + sizeof (char *). Obviously, P points to the address of STR [1], the second element of Str.

Line7 :( * P ++) + 2; because the post operator ++ has a higher priority than *, the value of P is changed to (INT) after ++ operations) P + sizeof (char *), it is clear that P points to the address of STR [2] and then performs the * operation. Because the back end is ++, it returns the value of unchanged p, the STR [1] value is obtained, that is, the pointer value pointing to the string "to", because * p type is char *, so] (* P ++) + 2; STR [0] eventually points to '\ 0' at the end of "to", so it is null when STR [0] is output.

Line8: Str [1] = * (p + 1); apparently STR [1] points to the first address of the string "Nanjing. Cout <STR [1] <Endl; Output string Nanjing

Line9: Apparently, cout <STR [2] <Endl; outputs Jing

Line10: Since P points to the STR [2] address from line7 analysis, the & STR [2] ~ The data in & STR [2] + 3 is changed to the first address of the character "jing. STR [2]-str [1] = 3, so the final cout <STR [3] <Endl; Output G.

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.