华为面试题的总结

1.给出下列定义:

char acX[] = "abcdefg";
char acY[] ={'a', 'b', 'c', 'd', 'e', 'f', 'g'};

则正确的叙述是:数组acX的长度小于数组acY的长度

2.下面代码输出的结果是:

#include <iostream>
using namespace std;
void example(char acHello[])
{
    printf("%d", sizeof(acHello));// 4
    return;
}
int main(){
    char acHello[] = "hello, 51_CC";
    example(acHello);
    return 0;
}

3.以下叙述中不正确的是(D)

(A)在不同的函数中可以使用相同名字的变量

(B)函数中的形式参数是在栈中保存

(C)在一个函数内定义的变量只在本函数范围内有效

(D)在一个函数内复合语句中定义的变量在本函数范围内有效(复合语句指函数中的成对括号构成的代码)

#include <iostream>
using namespace std;

int main(){
    int a = 10;
    {
        int b = 5;
        cout<<a<<endl;
    }
    cout<<b<<endl;//b会显示没有定义,出了复合语句块,就出了作用域
}

 4.下面代码输出的结果

#include <iostream>
using namespace std;

int main(){
    unsigned long pulArray[] = {6, 7, 8, 9,10};
    unsigned long *pulPtr;
    pulPtr = pulArray;
    *(pulPtr + 3) += 3;
    printf("%d, %d\n",*pulPtr, *(pulPtr + 3));// 6, 12
}

5.下面代码输出的结果

#include <iostream>
#include <string.h>
using namespace std;
void example()
{
    int i;
    //char acNew[20] = {0};//初始化都是‘\0’
    char acNew[20];//随机的,没有初始化,所以不确定‘\0’会出现在哪里
    for (int j = 0; j < 5; ++j) {
        acNew[j] = '0';
    }
    printf("%d\n", strlen(acNew));// 15 值是随机值
}

int main(){
    example();
}

6.全局变量可以定义在被多个.c文件包含的头文件中?(×)

解答:要使用那个变量(全局)就把哪个变量定义在要使用的源文件中,如果其他的文件也要引用的话,就在都包含的头文件中用extern定义即可。PS:在源文件中,用static定义的全局变量,只能在本文件中使用。

7.分析下面代码的输出

#include <iostream>
using namespace std;
struct stu{
    int num;
    char name[10];
    int age;
};
void fun(struct stu *p)
{
    cout<<(*p).name<<endl;
}
int main(){
    struct stu students[3] = {
        {9801,"zhang",20},
        {9802,"wang", 19},
        {9803,"zhao",18}
    };
    fun(students + 1);// wang
    return 0;
}

8.设有下列语句的宏定义,z的值是多少:

#include <iostream>
using namespace std;
#define N 4
#define Y(n) ((N+2)*n)
int main(){
    int z = 2 *(N + Y(5 + 1));//70,直接把宏展开带入表达式,PS:展开之后运算的优先级可能会发生变化
    cout<<z<<endl;
    return 0;
}

9.以结构体作为函数的传递参数,一般都是以指针作为参数,减少函数参数压栈的系统开销。

10.下列代码的输出是

#include <iostream>
using namespace std;

int main(){
    unsigned char a = 200;
    unsigned char b = 100;
    unsigned char c = 0;
    c = a + b;
    //cout<<a + b<<" "<<c <<endl;
    printf("%d %d",a + b,c); // 300 44
    return 0;
}
#include <iostream>
using namespace std;
#define SIZE 256
int main(){
    unsigned char i = 0;
    //这是一个死循环
    for(;i < SIZE;++i){
        printf("i = %d\n", i);
    }
    return 0;
}

11.下面的代码pointer分配了()个字节的空间

#include <iostream>
using namespace std;
#define A 2
#define B 3
#define MAX_SIZE A + B
struct _Record_Struct{
    unsigned char Env_Alarm_ID : 4;
    unsigned char Paral : 2;
    unsigned char state;
    unsigned char avail : 1;
}*Env_Alarm_Record;
int main(){
    struct _Record_Struct *pointer = (struct _Record_Struct *)malloc(sizeof(_Record_Struct)* MAX_SIZE);//总共开辟9个字节的空间
    return 0;
}

12.下面代码的输出是

#include <iostream>
using namespace std;

int main(){
    char c;
    unsigned char uc;
    unsigned short us;
    c = 128;
    uc = 128;
    us = c + uc;
    printf("0x%x\n",us);// 0x0
    us = (unsigned char)c + uc;
    printf("0x%x\n",us);// 0x100
    us = c + (char)uc;
    printf("0x%x\n",us);// 0xff00
    return 0;
}

PS:负数在内存中是用补码来存的,输出的时候要转化为原码,转换过程:先-1,再取反(取反符号位不变)

#include <iostream>
using namespace std;

int main(){
  char a = -128;// 0x80
  char b = -127;// 0x81
  char c = -1; //  0xff
  char d = -2; //  0xfe
  char f = 0; //   0x00
}

13.下列代码的输出是

#include <iostream>
using namespace std;
unsigned short *sum(unsigned char a , unsigned char b)
{
    unsigned short s = 0;
    s = a + b;
    return &s;
}
int main(){
   unsigned short *p = nullptr;
   unsigned char a = 1, b=2;
   p = sum(a, b);
   printf("%u + %u = %u\n",a,b,*p);
    return 0;
}

gcc返回了一个调用函数里面的临时值,崩掉了。vc结果是3,不同的系统处理的方式不同。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值