Everything that kills me makes me feel alive!

**

Remember a failed interview

**

2020.3.19 sunny

"What is your professional ranks?"
"**%." "Are
you on what programming-related courses?"
"C language, assembly, computer theory, computer control technology, SCM principles."
"What is most familiar with? " // worried not answer, also close to ask what is best at
" C language. " "
then talk about the life cycle of variables. " "
global variables local variables ...... ...... "
" said that under static global and local. "
" ...... static "
," Hong said at? " "
macro ...... "
" before compiling what action will the macro? "
"? " // concept can not remember, I was too dishes

"First to write a structure, which contains two plastic, an array of characters."

“OK”

struct one{
	int a;
	int b;
	char c[6]; 
};

" Seeking character array in the offset c relative structural body structure. " // a question

I: The offset of the body structure element, what?

Seemingly solid as an old dog, but in reality Huangbuzelu to write the code, and then under the guidance of teachers to teach students like the code changed to this:

typedef struct one aa;
int move(){
	struct one *p;
	int x;
	p=(aa*)malloc(sizeof(aa));
	x=(p->c)-(&p->a);
	return x;
}

The result was wrong, but this is the final version.
/ * Do compiler trial and error, I am very desperate ah, knock past the code is compiled and then finished Qiaowan reform. (So this is my excuse dish) * /
listening to the problems of the moment it dawned on me - I never played. I know I was born to be useful vegetables, so I did not expect tight food to eat on the menu.
Then the interviewer asks other projects that I'm good at, trying to rescue it.
But I did understand style to place farther and farther down the road of suicide.
Then there is no then the.
After parting we say hello.
Until that moment I understood the seriousness of the matter.
It turned out that this is the end of.
"I said the good do question it ???" I was a big question mark.
I understand the fact that, after it was over:. "Oh, did such a simple subject can not do, can not save the."

Indeed, so simple, can not save the ah

Soon received a cool message.
The compiler opens the breathing rhythm gradually gentle.
Yes ah, it's that simple.
In a moment I thought of two possibilities.
Method 1: Pointer casts subtraction.
Method 2: Save the total length character array structure placeholder.

//定义结构体 
typedef struct one{//之后程序中,若定义struct变量,C语言下struct不能省略,c++语言下可以省略struct 
	int a;
	int b;
	char c[CL];
}one; 

//
//方法一:指针强制类型转换,相减 
void roadOne(one *p){
	long long n;
	n=(long long)p->c-(long long)&p->a;//64位系统,指针为8位 ,故强制转换为long long 
	printf("方法一:指针强制类型转换,相减 ,字符数组的偏移量为:%lld\n",n);
} 

//
//方法二: 构体总长减字符数组占位
void roadTwo(one *p,int length,int cLength){
	int n;
	if(cLength%4==0)//若整除4,则字符数组长度等于其在结构体中的占位 
	   n=length-cLength;
	else//否则不足4位,补足 4位 
	   n=length-4*(cLength/4+1);
	printf("方法二:结构体总长减字符数组占位,字符数组的偏移量为:%d\n",n);
}

I like a simple enough mentally.
There Baidu to Method 3: Use offsetof ().

//方法三:使用offsetof()
void roadThree(){
	int n;
	n=offsetof(one,c);//求偏移量函数,输入为结构体名和其中变量名 
	printf("方法三:使用offsetof(),字符数组的偏移量为:%d\n",n);
}

awsl
However, everything that kills me makes me feel alive!

Words and short, the failure of the interview told me:
please use the "I choose, I am responsible, I want to approach"
instead of "I do not know, I'm afraid, I chose to give up" !!!

"Go solving it! Like all questions have the same answer!"

(+0.5 experience, current experience value of 1.5.)

Published an original article · won praise 1 · views 20

Guess you like

Origin blog.csdn.net/wonlyyh/article/details/104969891