C/C++基础面试题整理

1.sizeof

sizeof(0) == sizeof(int)
sizeof(0L) == sizeof(long)
sizeof(0LL) == sizeof(long long)

int array[5]; sizeof(array) == sizeof(int) * 5

2.大端小端

大端低字节高地址,高字节低地址;小端低字节低地址,高字节高地址(TCP/IP)采用大端

某32位数据在内存中的布局为(左为低地址)

大端读数为0x12345678;小端读数为0x78563412

4.结构体和数组当作参数传递到函数

结构体会拷贝一个副本给函数,在函数内的修改对原始数据不产生影响;

数据传递的是首地址,函数内的修改对原始数据有影响。

5.C宏

定义一年有几秒(忽略瑞年)

#define SECONDS_PER_YEAR (365 * 24 * 60 * 60)UL

二数求小的宏

#define MIN(x, y) ((x) > (y) ? (y) : (x))

 

6.C中基本类型的定义

a) int a; // An integer 

b) int *a; // A pointer to an integer 

c) int **a; // A pointer to a pointer to an integer 

d) int a[10]; // An array of 10 integers 

e) int *a[10]; // An array of 10 pointers to integers 

f) int (*a)[10]; // A pointer to an array of 10 integers 

g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer 

h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer

 

7.关键字static的作用

1). 在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变。

2). 在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所用函数访问,但不能被模块外其它函数访问。它是一个本地的全局变量。

3). 在模块内,一个被声明为静态的函数只可被这一模块内的其它函数调用。那就是,这个函数被限制在声明它的模块的本地范围内使用。

4). 在类内,静态函数表示不需要实例就可以调用

 

8. 引用与指针的区别

1) 引用必须被初始化,指针不必。

2) 引用初始化以后不能被改变,指针可以改变所指的对象。

2) 不存在指向空值的引用,但是存在指向空值的指针.

 

9.死循环的写法

1while(1){}

2) for(;;){}

3)Loop:{}goto Loop;

10.C++ String类的实现

#include <iostream>

#include <string.h>

using std::ostream;

class String {
    friend ostream& operator<<(ostream& out, const String& str);

    public:
        String(const char* str);        //Constuctor
        String(const String& other);        
        ~String(void);
        String& operator=(const String& other);
        char* data(void) const;
    private:
        ostream& Print(ostream& out) const;
        char* _data;
};

ostream& operator<<(ostream& out, const String& str)
{
    return str.Print(out);
}

String::String(const char* str)
{
    if (str == NULL) {
        _data = new char[1];
        // if _data == NULL throw Exception
        _data[0] = '';
    } else {
        size_t len = strlen(str);
        _data = new char[len + 1];
        // if _data == NULL throw Exception
        strcpy(_data, str);
    }
}

String::~String(void)
{
    delete[] _data;
}

String& String::operator=(const String& other)
{
    if (this != &other) {
        delete[] _data;
        size_t len = strlen(other.data());
        _data = new char[len + 1];
        // if _data == NULL throw Exception
        strcpy(_data, other.data());
    }
    return *this;
}

char* String::data(void) const
{
    return _data;
}

ostream& String::Print(ostream& out) const
{
    out << _data;
    return out;
}

 11. C++ new

new函数可以重载;

new操作符不可以重载。

12. 枚举

枚举只能是整型,值会自动连续

 enum e {
         A = -5,
         B,
         C = 3, 
         D, 
         E = -8, 
         F};
 printf("%d, %d, %d, %d, %d, %d", A, B, C, D, E, F);

// 输出-5 -4 3 4 -8 -7

 13.memcpy, memmove, strcpy 实现

void* memcpy(void* dest, const void* src, size_t n)
{
      char* d = (char*) dest;
      const char* s = (const char*) src;
      
      while (n--)
            *d++ = *s++;
      return dest;
}

void* memmove(void* dest, const void* src, size_t n)
{
      char* d = (char*) dest;
      const char* s = (const char*) src;
      
      if (s > d) {            // start at beginning of s
            while (n--)
                  *d++ = *s++;
      } else if (s < d) {     // start at end of s
             d = d + n - 1;
             s = s + n - 1;
             while (n--)
                   *d-- = *s--;
      }
      return dest;
}

char* strcpy(char* dest, const char* src)
{
      assert(dest);
      assert(src);
      char* d = dest;
      const char* s = src;
      
      while (*str != '')
            *d++ = *s++;
      *d = '';
      return dest;
}
原文地址:https://www.cnblogs.com/Leo-Forest/p/3130451.html