C语言面试题

时间:2022-12-10 05:18:27
#include <stdio.h>

int main()
{
int a[] = {, , , , };
int* p1 = (int*)(&a + ); // <==> (unsigned int)&a+sizeof(*&a) => 整个数组后面的那个地址
int* p2 = (int*)((int)a + ); <=> 取a[]后三个字节拼接a[]第一个字节 => 0x02000000
int* p3 = (int*)(a + ); <=> a[] printf("%d\n", p1[-]); // (unsigned int)p1-1*sizeof(*p1) => a[4]
printf("%d\n", p2[]); // 0x02000000
printf("%d\n", p3[]); // a[3] return ;
}

一条语句实现strlen

return (assert(s), (*s ? strlen(s+)+ : ));
#size_t为无符号整数 strlen的返回值为 size_t 类型
size_t strlen(const char* s)
{
size_t length = ;
aasert(s);//当指针为空时运行出错 assert.h while(*s++)
{
length++;
}
return }

strcpy实现

#include <stdio.h>
#include <assert.h> char* strcpy(char* dst, const char* src)
{
char* ret = dst; assert(dst && src); while( (*dst++ = *src++) != '\0' ); return ret;
} int main()
{
char dst[]; printf("%s\n", strcpy(dst, "Delphi Tang!")); return ;
}

重置动态空间的大小:

#include <stdio.h>
#include <malloc.h> int reset(char**p, int size, int new_size)
{
int ret = ;
int i = ;
int len = ;
char* pt = NULL;
char* tmp = NULL;
char* pp = *p; if( (p != NULL) && (new_size > ) )
{
pt = (char*)malloc(new_size); tmp = pt; len = (size < new_size) ? size : new_size; for(i=; i<len; i++)
{
*tmp++ = *pp++;
} free(*p);
*p = pt;
}
else
{
ret = ;
} return ret;
} int main()
{
char* p = (char*)malloc(); printf("%0X\n", p); if( reset(&p, , ) )
{
printf("%0X\n", p);
} return ;
}

一维方式打印二维数组:

#include <stdio.h>
#include <malloc.h> void printArray(int a[], int size)
{
int i = ; printf("printArray: %d\n", sizeof(a)); for(i=; i<size; i++)
{
printf("%d\n", a[i]);
}
} int main()
{
int a[][] = {{, , }, {, , }, {, , }};
int* p = &a[][]; printArray(p, ); return ;
}

指针方式打印二维数组:

#include <stdio.h>

int main(int argc, char* argv[], char* env[])
{
int a[][] = {{, , }, {, , }, {, , }};
int i = ;
int j = ; for(i=; i<; i++)
{
for(j=; j<; j++)
{
printf("%d\n", *(*(a+i) + j));
}
}
}
#include <stdio.h>

int main()
{
int a[][];
int(*p)[]; p = a; printf("%d\n", &p[][] - &a[][]); // -4
}

指针阅读技巧:
右左法则
1、从最里层的(最左)圆括号中未定义的标识符看起
2、先往右看,再往左看
3、当遇到圆括号或者(方括号时可以确定部分类型),并调转方向
4、重复 2,3 知道结束

#include <stdio.h>

int main()
{
int (*p2)(int*, int (*f)(int*)); int (*p3[])(int*); int (*(*p4)[])(int*); int (*(*p5)(int*))[];
}

返回 int(*)[5] 数组指针类型

#include "stdio.h"
int test(int i)
{
return i;
} int (*test(int *p))[] //返回类型为 int(*)[5]
{
static int a[]={,,,,};
return &a;
} int main(int argc, char** argv, char* env[])
{ int (*(*p5)(int*))[];
int a = ;
//p5 = &a;
p5 = test; int(*p)[] = p5(&a); printf("%d \n", *(*p+));
printf("%d \n", *(*p+)); return ;
}

动态申请二维数组(没用一维数组指针实现,而是用的普通的int *指针)

int **malloc2d(int row, int col)
{
int** ret = (int**)malloc(sizeof(int*)*row);
int* p = (int*)malloc(sizeof(int)*row*col); int i = ;
if (ret && p)
{
for (i = ; i < row;i++)
{
ret[i] = (p + i*col);
}
}
else{
free(p);
free(ret);
} return ret;
} void free2d(int **a)
{
free(a[]);
free(a);
}

二维数组做参数

#include <stdio.h>

void access(int a[][], int row)
{
//没必要传递列,可以计算出来,c语言中只有一维数组 int col = sizeof(*a) / sizeof(int);
int i = ;
int j = ; printf("sizeof(a) = %d\n", sizeof(a)); for(i=; i<row; i++)
{
for(j=; j<col; j++)
{
printf("%d\n", a[i][j]);
}
}
} int main()
{
int a[][] = {{, , }, {, , }, {, , }}; access(a, );
}

求n个数的平均值

#include <stdio.h>
#include <stdarg.h> float average(int n, ...)
{
va_list args;
int i = ;
float sum = ; va_start(args, n); for(i=; i<n; i++)
{
sum += va_arg(args, int);
} va_end(args); return sum / n;
} int main()
{
printf("%f\n", average(, , , , , ));
printf("%f\n", average(, , , , )); return ;
}

按字节进行清零

#include <stdio.h>

#define RESET(p, len) while( len > 0) ((char*)p)[--len] = 0

void reset(void* p, int len)
{
while( len > )
{
((char*)p)[--len] = ;
}
} int main()
{
int array[] = {, , , , };
int len = sizeof(array); reset(array, len);
RESET(array, len); return ;
}