1. 请写出下列代码中各变量的大小及存储位置(基础题)
#define SIZE 1024
typedef struct _Node
{
struct _Node *prev; // 4字节
struct _Node *next; // 4字节
unsigned char buf[SIZE]; // 1024字节
char a; // 1字节
int ib; // 4字节
short header; // 2字节
double *p; // 8字节
unsigned char (*buf)[SIZE]; // 4字节
int (*fun[3])(char (*argA)(int, int), int); // 4字节
} Node; // 一共1064字节
static double m = 2.1; // 8字节 全局初始化区
int data = 0; // 4字节 全局初始化区
int main(void)
{
int *p = NULL;
p = malloc(sizeof(int)); // 4字节,堆区
char *buf = "helloworld"; // 4字节,栈区
static double m = 10.0; // 8字节,栈区
Node *node = (Node *)malloc(sizeof(Node)); // 4字节,栈区
char buf[10]; // 10字节,栈区
int i = 0; // 4字节,栈区
extern int data; // 4字节,全局
free(p);
free(node);
}
2. int i=(j=4,k=8,l=16,m=32); printf(“%d”, i); 输出是多少?(基础题)
32
// 逗号表达式是C语言中的逗号运算符,它将两个及其以上的式子连接起来,从左往右逐个计算表达式,整个表达式的值为最后一个表达式的值。
3. 用C 写一个输入的整数,倒着输出整数的函数,要求用递归方法(中等题)
int fun(int a)
{
printf("%d", a % 10);
a = a / 10;
if (a > 0)
fun(a);
return 0;
}
int main(int argc, char *argv[])
{
int i;
printf("Please in put a nu :");
scanf("%d", &i);
fun(i);
printf("\n");
return 0;
}
4. 编写一个函数,返回两个字符串的最大公串!例如,“adbccadebbca”和“edabccadece”,返回“ccade”(中等题)
#include <iostream>
using namespace std;
char *Max(const char *p, const char *q)
{
static char a[20];
char b[20];
int max = 0;
int Len = 0;
int i = 0;
for (; *p != '\0'; p++)
{
Len = i = 0;
while ((*(q + i) != '\0') && (*p != *(q + i)))
i++;
if (*(q + i) == '\0')
continue;
while (*(p + Len) == *(q + i))
{
b[Len] = *(q + i);
Len++;
i++;
}
if (max < Len)
{
max = Len;
for (int j = 0; j < Len; j++)
a[j] = b[j];
a[Len] = '\0';
}
}
if (max == 0)
{
cout << "没有找到公共串" << endl;
getchar();
exit(1);
}
return a;
}
void main()
{
char a[] = "adbccadebbca";
char b[] = "edabccadece";
char *p = new char[20];
p = Max(a, b);
cout << p << endl;
system("pause");
}
char *func(const char* str1, const char* str2)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
int maxLen = len1 > len2 ? len1 : len2;
char *max = calloc(1, maxLen);
char *tmp = calloc(1, maxLen);
for (int i = 0; i < len1; ++i)
{
for (int j = 0; j < len2; ++j)