C,C++语言笔试面试中常见问题

更新时间:2023-11-27 16:30:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

阿尔卡特朗讯(中国)C语言面试题

http://hr.c114.net (2009-2-20 14:49:00)

据说是阿尔卡特(中国)的面试题目阿尔卡特C语言面试题 阿尔卡特C语言面试题全部用C语言完成:

1.自己定义数据结构,写出程序:在一个单向链表中,往I位置插入一个节点。 2.自己定义数据结构,写出程序:二叉树的前序遍历。 3.不允许使用系统时间,写出一个随机数生成函数。 http://bbs.yingjiesheng.com/thread-673785-1-1.html

sizeof的用法,在C,C++语言笔试面试中常见问题 收藏 很常见的一些问题,但是很多时候不会!

这里只总结一些最常用的(32位编译环境 ):

1,基本数据类型

cout<

结果是1

cout<

结果是4

cout<

结果是4

cout<

结果是4

cout<

结果是2

cout<

结果是4

cout<

结果是8

2,指针变量

指针变量的sizeof的值与指针所指的值没有任何关系,所以指针变量的内存大小都是相等的 ,如:

int* a;

char* b=\

char** c=&b;

sizeof(a)==sizeof(b)==sizeof(c)==4; 但是

sizeof(*a)=4;

sizeof(*b)=1;

sizeof(*c)=4;

sizeof(**c)=1;

3,数组的大小

数组的sizeof值等于数组所占用的内存字节数

char a1[] = \int a2[3];

sizeof( a1 ); // 结果为4,字符 末尾还存在一个NULL终止符 sizeof( a2 ); // 结果为3*4=12(依赖于int)

但是,当数组作为函数参数的时候呢?这时候,数组是传地址的,即这时用指针来处理 ,所以,如果有:

void foo(char a[]) {

int b= sizeof( a ); // c == 4 }

所以,这时候,a的类型为char*,而sizeof(char*)==4;

4,联合体和结构体

联合体:

union a{int a;char b;float c;};

由于联合体公用储存空间,所以只取最大的来计算联合体占用空间,这里sizeof(a)==4;

结构体:

比较麻烦,注意3点:

1,首地址应为结构中最宽成员的倍数,守成员都是0

2,整个结构体长度应为最宽成员的倍数

3,把单一元素看成结构体 如,

struct a{double a;char b;int c;};

sizeof(a)==8+1+3+4==12;

这里,先分配double,首地址为8的倍数0,再分配char,这时其偏移量为8,为1的倍数,再分配int,其偏移量为9,不是4的倍数,应该用3个空字节补齐,所以int的偏移为9+3=12,再加上4=16,这时16为最宽成员8的倍数,所以该结构体的长度为16。

当结构体中包含结构体时,最宽成员不包括内部结构体,最宽成员应从这两个结构体的基础成员中去找。

struct a{char a;int b;};

sizeof(a)==8;

sturct b{char a; a b;char c};

sizeof(b)==16;

把内部结构体打开来看,分配char为1,再分配a,a中最宽成员为4,则其起始偏移应为最宽的倍数,1+3=4,4+8=12,再分配char,这时,其偏移为12,是char的倍数,12+1=13,根据规则2,再用3来补齐,13+3=16,是int的倍数,所以结构体b的长度为16.

5,类的sizeof大小

1,空类大小为1

2,虚函数大小为4,构造析构不算大小

3,类大小等于所有数据成员大小之和

4,继承中类大小等于父类和子类的数据成员大小之和

本文来自CSDN博客,转载请http://blog.csdn.net/shinhwach/archive/2009/10/11/4636036.aspx http://zhidao.http://www.wodefanwen.com//question/110881469.html?fr=ala0 http://blog.sina.com.cn/s/blog_4a601c06010007mh.html

http://wenku.http://www.wodefanwen.com//view/761bb4d8ce2f0066f533221c.html http://zhidao.http://www.wodefanwen.com//question/80915904.html

标明出处:

C语言常见面试过程中的笔试题2

嵌入式开发吧 2010-03-11 18:01:24 阅读74 评论0 字号:大中小 订阅

1、用变量a 给出下面的定义 a) 一个整型数(An integer)

b) 一个指向整型数的指针(A pointer to an integer)

c) 一个指向指针的的指针,它指向的指针是指向一个整型数( A pointer to a pointer to an

integer)

d) 一个有10个整型数的数组(An array of 10 integers)

e) 一个有10个指针的数组,该指针是指向一个整型数的(An array of 10 pointers to integers)

f) 一个指向有10个整型数数组的指针(A pointer to an array of 10 integers)

g) 一个指向函数的指针,该函数有一个整型参数并返回一个整型数( A pointer to a function

that takes an integer as an argument and returns an integer)

h) 一个有10个指针的数组,该指针指向一个函数,该函数有一个整型参数并返回一个整型 数( An array of ten pointers to functions that take an integer argument and return an integer )

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

2、关键字volatile 有什么含意?

一个定义为volatile 的变量是说这变量可能会被意想不到地改变,这样,编译器就不会去假 设这个变量的值了。精确地说就是,优化器在用到这个变量时必须每次都小心地重新读取这

个变量的值,而不是使用保存在寄存器里的备份。

3、const 符号常量; (1)const char *p (2)char const *p (3)char * const p 说明上面三种描述的区别;

如果const 位于星号的左侧,则const 就是用来修饰指针所指向的变量,即指针指向为常量;

如果const 位于星号的右侧,const 就是修饰指针本身,即指针本身是常量。

(1)const char *p

一个指向char 类型的const 对象指针,p 不是常量,我们可以修改p 的值,使其指向不同的char,

但是不能改变它指向非char 对象,如:

const char *p; char c1='a'; char c2='b'; p=&c1;//ok p=&c2;//ok *p=c1;//error (2)char const *p (3)char * const p

这两个好象是一样的,此时*p 可以修改,而p 不能修改。

4、简答题

(1)、头文件中的ifndef/define/end if 干什么用?

答:防止该头文件被重复引用。

(2)、#include 和#include “filena me.h” 有什么区别? 答:对于#include ,编译器从标准库路径开始搜索filena me.h 对于#include “filena me.h” ,编译器从用户的工作路径开始搜索filena me.h

(3)、const 有什么用途?(请至少说明两种)

答:( 1)可以定义const 常量,( 2)const 可以修饰函数的参数、返回值,甚至 函数的定义体。被const 修饰的东西都受到强制保护,可以预防意外的变动,能

提高程序的健壮性。

(4)、在C++ 程序中调用被C 编译器编译后的函数,为什么要加extern “C”? 答:C++语言支持函数重载,C 语言不支持函数重载。函数被C++编译后在库中

的名字

与C 语言的不同。假设某个函数的原型为: void foo(int x, int y);该函数被C 编 译器编译后在库中的名字为_foo , 而C++编译器则会产生像_foo_int_int 之类 的名字。C++提供了C 连接交换指定符号extern“C”来解决名字匹配问题。

5、编写strcpy 函数

已知strcpy 函数的原型是char *strcpy(char *strDest, const char *strSrc); 其中

strDest 是目的字符串,strSrc 是源字符串。 (1)不调用C++/C 的字符串库函数,请编写函数strcpy

char *strcpy(char *strDest, const char *strSrc);

{

assert((strDest!=NULL) && (strSrc !=NULL));

char *address = strDest; // 2 分 while( (*strDest++ = * strSrc++) != ?\\0? )

NULL ;

return address ; // 2 分

}

(2)strcpy 能把strSrc 的内容复制到strDest,为什么还要char * 类型的返回值? 答:为了实现链式表达式。// 2 分例如int length = strlen( strcpy( strDest, “hello

world”) );

6. 编写用C 语言实现的求n 阶阶乘问题的递归算法:

long int fact(int n)

{ int x; long int y; if(n<0) { printf(\

} if(n==0) return 1; x=n-1; y=fact(x); return (n*y);

}

7 链表题:一个链表的结点结构

struct Node

{ int data ; Node *next ;

};

typedef struct Node Node ;

已知链表的头结点head,写一个函数把这个链表逆序( Intel)

Node * ReverseList(Node *head) //链表逆序

{

if ( head == NULL || head->next == NULL )

return head; Node *p1 = head ; Node *p2 = p1->next ; Node *p3 = p2->next ; p1->next = NULL ; while ( p3 != NULL )

{

p2->next = p1 ; p1 = p2 ; p2 = p3 ; p3 = p3->next ;

}

p2->next = p1 ; head = p2 ; return head ;

}

http://yanjing12260302.blog.163.com/blog/static/139876240201092545142809/?latestBlog

几个C语言排序算法

C/C++ 2010-10-25 16:51:42 阅读5 评论0 字号:大中小 订阅

首先先介绍排序:

1,大家都知道的冒泡排序: #i nclude

#i nclude using namespace std;

template

void swap(type x[],int,int);

template

void BubbleSort(type x[],int);

int main()

{

srand(time(0)); const int n=10;

int x[n];

for(int i=0;i x[ i]=rand()?; for(int i=0;i cout<<\cout<<\

system(\

return 0;

} template

void BubbleSort(type x[],int n)

{

for(int i=n-1;i>=0;i--)

{ int flag=0;

for(int j=0;j if(x[ j]>x[ j+1])

{ swap(x,j,j+1); flag=1;

} if(flag==0)

return;

} } template

void swap(type x[],int n,int m)

{ int temp=x[n]; x[n]=x[m]; x[m]=temp;

}

2,简单的选择排序 #i nclude

#i nclude using namespace std;

template

void swap(type x[],int,int);

template

void SlectSort(type x[],int);

int main()

{

srand(time(0)); const int n=10;

int x[n];

for(int i=0;i x[ i]=rand()?; for(int i=0;i cout<<\

cout<<\

return 0;

} template

void swap(type x[],int n,int m)

{ int temp=x[n]; x[n]=x[m]; x[m]=temp;

} template

void SlectSort(type x[],int n)

{ for(int i=0;i {

for(int j=i+1;j if(x[ j] swap(x,i,j);

} } 3,插入排序 #i nclude #i nclude

using namespace std; template void swap(type x[],int,int); template void InsertSort(type x[],int);

int main()

{

srand(time(0)); const int n=10;

int x[n]; for(int i=0;i

for(int i=0;i

return 0;

}

template void swap(type x[],int n,int m)

{ int temp=x[n]; x[n]=x[m]; x[m]=temp;

}

template void InsertSort(type x[],int n)

{

for(int i=1;i0;j--) if(x[ j]

}

4:希尔排序 #i nclude

#i nclude using namespace std; template void swap(type x[],int,int); template void ShellSort(type x[],int);

template

void ShellSorthelper(type x[],int,int);

int main()

{

srand(time(0)); const int n=10;

int x[n]; for(int i=0;i

cout<<\

for(int i=0;i

return 0;

}

template void swap(type x[],int n,int m)

{ int temp=x[n]; x[n]=x[m]; x[m]=temp;

}

template void ShellSort(type x[],int n)

{

for(int i=n/2;i>=1;i/=2) for(int j=0;j

}

template

void ShellSorthelper(type x[],int len,int n)

{

for(int i=len;i0;j-=len) if(x[ j]

}

5,快速排序 #i nclude

#i nclude using namespace std;

本文来源:https://www.bwwdw.com/article/l2et.html

Top