Interview Questions-embedded software development (1)

Source: Internet
Author: User

1. What is pre-compilation and when pre-compilation is required:
Answer:
1. Large Code bodies that are not frequently modified are always used. (The answer to a good project, isn't it ~~~ :))
2. A program consists of multiple modules. All modules use a set of standard inclusion files and the same compilation options. In this case, all contained files can be precompiled into a precompiled header.

2. What are the differences between char * const P char const * P const char * P and the above three?
Answer:
Char * const P; // constant pointer. Const modifies the pointer P, so the value of P cannot be modified.
Char const * P; // const modifies * P, which is a pointer to a constant. The constant value cannot be changed to const char * P; // and char const * P.

3. What is the difference between array variables and pointers?

#include  <iostream>using namespace std;int main(int argc, char const *argv[]){char str1[] = "abc"; char str2[] = "abc";const char str3[] = "abc"; const char str4[] = "abc"; const char *str5 = "abc";const char *str6 = "abc"; char *str7 = "abc"; char *str8 = "abc";cout <<  ( str1 == str2 )  << endl;cout << ( str3 == str4 )  << endl; cout << ( str5 == str6 )  << endl; cout << ( str7 == str8 )  << endl;return 0;}

The result is: 0 0 1 1 str1, str2, str3, and str4 are array variables with their own memory space. str5, str6, str7, and str8 are pointers, they point to the same constant area.

4. Are there any problems with the usage of the two sizeof in the following code? The answer is yes, as long as the length exceeds the length of the pointer ~~~

# Include <iostream> using namespace STD; void uppercase (char STR []) // convert lowercase letters in STR into uppercase letters {for (size_t I = 0; I <sizeof (STR)/sizeof (STR [0]); ++ I) if ('A' <= STR [I] & STR [I] <= 'Z') STR [I]-= ('A'-'A ');} int main (INT argc, char const * argv []) {char STR [] = "abcdeeeee"; cout <"str length:" <sizeof (STR) /sizeof (STR [0]) <Endl; uppercase (STR); cout <STR <Endl; return 0 ;}

Answer:

STR character length: 10

Abcdeeeee

There is a problem with sizeof in the function. According to the syntax, sizeof, for example, is used as an array, can only measure the size of a static array, and cannot detect the dynamically allocated or external array size. STR outside the function is a static defined array, so its size is 6. Because there is '\ 0', STR in the function is actually a pointer to a string, there is no additional array-related information, so sizeof acts on it and only looks at it as a pointer. A pointer is 4 bytes, SO 4 is returned.

PS: I have a 64-bit CPU, so the pointer is 8 bits ~~~


5. How many digits is the pointer of a 32-bit machine:
You only need to check the number of bits in the address bus. Machines later than 80386 have 32 data buses. Therefore, the number of digits of the pointer is 4 bytes. In other words, the pointer of a 64-bit host (CPU & OS) is also 32-bit, that is, 4 bytes. Therefore, the pointer is not the length of the CPU, but the number of digits of the address bus ~~~

6. array pointer

 #include "stdio.h" int main(int argc, char const *argv[]) {  int a[5]={1,2,3,4,5};  int *ptr=(int *)(&a+1);   printf("%d,%d \n",*(a+1),*(ptr-1));   return 0; }

Answer: 2,5

* (A + 1) is a [1], * (ptr-1) is a [4], the execution result is 2, 5. & A + 1 is not the first address + 1. The system will consider that the offset of adding an array a is the offset of an array (in this example, It is 5 Int values) int * PTR = (int *) (& A + 1); then PTR is actually & (A [5]), that is, the reason for a + 5 is as follows: & A is an array pointer, whose type is int (*) [5]; while adding a pointer to 1 should add a certain value according to the pointer type, different types of pointers plus 1 increase the size of different A is an int array pointer with a length of 5, so you need to add 5 * sizeof (INT)
So PTR is actually a [5] But PRT is different from (& A + 1) type (this is important) So prt-1 will only subtract sizeof (int *), & A has the same address but different meanings. A is the first address of the array, that is, the address of a [0]. & A is the first address of the object (array, A + 1 is the address of the next element of the array, that is, a [1]. & A + 1 is the address of the next object, that is, a [5].

7. What is the problem with the following code:

 #include "stdio.h" #include "string.h"  int main()  {  char a;  char *str=&a;  strcpy(str,"hello");  printf(str); return 0; }

Answer: If no memory space is allocated for STR, an exception occurs. The problem is that you copy a string into the address indicated by the character variable pointer. Although the results can be correctly output, the program crashes because internal read/write is performed out of the border. That is to say, "hello" will be written to & A and the next five (including NUL). That is to say, it is out of the border and an error may occur at some time ~~~

8. char * s = "AAA"; printf ("% s", S); s [0] = 'B'; printf ("% s", S ); what's wrong?
Answer: "AAA" is a String constant. S is a pointer pointing to this string constant, so there is a problem when declaring S. Cosnt char * s = "AAA"; then, because it is a constant, it is illegal to assign values to s [0. That is, constants cannot be written. Reading is acceptable. Replace "char * s" with "char s ~~~

9. Write a "standard" macro, which inputs two parameters and returns a smaller one.
Answer:. # define min (x, y) (x)> (y )? (Y) :( X) // No ';' at the end ';'

10. Infinite loops are often used in embedded systems. How do you use C to write an infinite loop.
Answer: While (1) {} or (;;)

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.