[Original] answers to several questions that can definitely test your C language skill

Source: Internet
Author: User

Original post address: http://chimf.bloghome.cn/posts/52793.html

No answers are provided. I wrote a reference answer. I hope you can correct it!

 

Reference answer:

Int x = 35;
Char STR [10];
// Q: What are the values of strlen (STR) and sizeof (STR?
// Reference answer: 13, 10. (In x86 machines -- little endian, descending stack, vc6.0 compiler)
// First, allocate space to X in the stack. Because the stack is little endian and the stack is decreasing, the order of X in the memory is: 00 00 23
// Allocate space to STR in the stack. Because the stack in the vc6.0 compiler is 4-byte aligned by default, 12 bytes of space will be allocated on the stack, and STR points to the lowest address.
// Therefore, in the memory, from low to high: CC 23 00 00 00, strlen (STR) = 13
// P.s. GCC seems that the stack is 16-byte aligned, so the results should be different. This is the case for a company's pen question. I personally feel that this compiler-related result is meaningless.

Strcpy (STR ," Http://www.it315.org "/ * A total of 13 letters */);
// Q: What are the values of x and strlen (STR?
// Reference answer: x = 103, strlen (STR) = 13. Based on the above analysis, this is simple. x = 'G' = 0x67 = 103

STR = "it315.org ";
// Q: Is the compilation successful?
// Reference Answer: No, char STR [10] ;=> char * const STR; pointer constants cannot be assigned again

Char * pstr;
Strcpy (pstr ," Http://www.it315.org/ ");
// Q: Can the above sentence be compiled? Is there a problem when running?
// Reference answer: it can be compiled, but there will be problems during the runtime, because pstr is not allocated memory space

Const char * P1;
Char * const P2;
// Q: What is the difference between the above two statements?
// Reference answer: P1 is a pointer to a constant. That is, P1 ++ is valid. * P1 = 'A' is invalid. P2 pointer is a constant, that is, * P2 = 'A' is legal, P2 ++ is invalid, and P2 must be initialized during definition, such as char * const P2 = STR;

P1 = (const char *) STR;
// Q: If it is p1 = STR; can the compilation be passed? Why type conversion? What is the essence of type conversion?
// Reference Answer: No, because STR is a char * const, and P1 is a const char *
 
Strcpy (P1, "ABC ");
// Q: Can the compilation be passed?
// Reference Answer: No. P1 is a const char * and cannot be changed. The strcpy parameter must be a char *
 
Printf ("% d", STR );

// Q: Is there a problem?
// Reference Answer: No problem. The output result is the value of the STR pointer, that is, an address value (10 hexadecimal)
 
Pstr = 3000;
// Q: Can the compilation be successful? If not, how can I modify it to ensure the compilation is successful?
// Reference Answer: No, pstr = (char *) 3000; you can compile
 
Long y = (long) pstr;
// Q: Can I do this?
// Reference answer: Yes. Y is the value of the pstr pointer, that is, an address.

Int * P = (int *) STR;
* P = 0x00313200;
Printf ("% s", STR );
// Q: What is the result? The following message is displayed: 0x31 corresponds to '1' and 0x32 corresponds to '2 '.
// Reference Answer: No output, STR memory layout: 00 32 31 00...

P = (int *) 3000;
// Q: What is the result of p + 1?
// Reference answer: p + 1 = P + sizeof (INT) = 3004

Char * Pc = new char [100];
// Q: How many memory blocks are occupied by the preceding statement in the memory?
// Reference answer: 104, PC occupies 4 bytes, and 100 bytes are allocated on the stack.

Void test (char ** P)
{
* P = new char [100];
}
// Q: Is there a problem with this compilation function? How do I pass parameters to call this function?
// Reference Answer: No problem, char * pstr; test (& pstr );

// Q: Do you understand the functions of typedef int (* pfun) (int x, int y?
// Reference answer: Define a function pointer type. There are two int parameters and the return value is int.

 

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.