Basic questions for embedded software engineer position (2)

And zero judgment

Write out the comparison statement between BOOL, int, float, pointer type variable a and "zero" respectively

if(!a) or if(a);	// BOOL
if(a == 0)			//int
// float
const EXPRESSION EXP = 0.000001
if(a<EXP && a>EXP)

if(a != NULL) or if(a == NULL)	//指针pointer

Add and subtract

a++: Execute a first, then add 1.
++a: Add 1 first, then execute a.

Example: Please write the input content of the following code

#include <stdio.h>
int main()
{
    
    
	int a,b,c,d;
	a = 10;
	b = a++;
	c = ++a;
	d = 10*a++;
	printf("b, c, d: %d, %d, %d\n",b,c,d);
	return 0;
}

答: 1012120

When there are several "+" or "-" that are difficult to distinguish to form an operator string, the C language stipulates: Take as many symbols as possible from left to right to form an operator.

int a = 5,b = 5;
a+++b  	//相当于(a++)+b,结果为10,运算后a为6,b不变
a---b	//相当于(a--)-b,结果为0,运算后a为4,b不变

The difference between array and pointer

Answer: Arrays are either created in static storage (such as global variables) or created on the stack. The pointer can point to any type of memory block at any time.

  1. Differences in modified content

    char a[] = "Hello";
    a[0] = 'X';
    
    char *p = "World" //注意p指向常量字符串
    p[0] = 'X'		  //编译器不会报错,但运行时会报 段错误(Segmentation fault)
    
  2. Use operators sizeofto calculate the capacity (number of bytes) of the array. sizeof(p), P is the number of bytes of a pointer variable, not the memory capacity pointed to by p.

    char a[] = "Hello World";
    char *p = a;
    printf("a = %ld\n",sizeof(a));	//12字节
    printf("p = %ld\n",sizeof(p));	//32位—4字节 64位—8字节
    
  3. Note that when an array is passed as a function parameter, the array automatically degenerates into a pointer of the same type

    void Func(char a[100])
    {
          
          
    	// 8 个字节而不是 100 字节
    	printf("Array Size = %ld\n",sizeof(a));
    }
    

Strcpy function reproduction

Do not call C/C++ string library functions, write the function strcpy:

char *strcpy(char *strDes, const char *strSrc)
{
    
    
	assert((strDes != NULL) && (strSrc != NULL));
	char *address = strDes;
	while((*strDes++ = *strSrc++) != '\0');
	return address;
}

++ has a priority greater than *

While part of the code analysis :

Assign the *strSrcvalue to *strDest, and then judge whether it has reached \0 (that is, the end of the character), and at the same time, the strSrc and strDest pointers are moved backward at the same time after the assignment is performed.


Array

Number of array elements

Given an array table, use a macro definition to find the number of data elements

#define	NTBL (sizeof(table)/sizeof(table[0]))
Clear array
//置字节字符串s的前n个字节为零且包括‘\0’
void bzero(void *s, int n);	

//将s中前n个字节用 ch 替换并返回 s 
void *memset(void *s,int ch,size_t n);

Routine: Clear the contents of the array str[10]

#include <stdio.h>

int main()
{
    
    
	char str[10] = "Hello,Mcu";
	char *pStr;
	
	printf("First str: %s\n",str);
	//方法一
	bzero(str,10);
	printf("Clear str: %s\n",str);
	
	//方法二
	pStr = memset(str,0,10);
	printf("Clear str: %s\n",pStr);
	printf("Clear str: %s\n",str);
	return 0;
}

basis

  1. Describe the basic characteristics of real-time operating systems
    to complete specific tasks within a specific time, with high real-time and strong reliability

  2. On the ARM platform, when C language function is called, parameters are passed through the stack

    Answer: When a function is called, it actually jumps from the running function to the called function. At this time, if the parameters used by the current program are not saved, then when the called function is executed, it jumps back to the original function. When the necessary parameters are missing, the program cannot be executed normally. Therefore, before calling the function, you need to save the field information, that is, push the parameters on the stack first, and then return the address.

Keep updating

Guess you like

Origin blog.csdn.net/qq_30722795/article/details/108160482