C语言面试编程题(精选5篇)
考查的是结构体和数组的内存布局情况。
#include
#include
typedef struct array1{
int ID;
struct array1* next;
}A;
typedef struct array2{
int ID;
int a;
int b;
int c;
}* B;
int main
{
A s1[15];
A* s2;
B s3;
for(int i=0;i<10;i )
{
s1[i].ID=i 64;
}
s2=s1 3;
s3=(B)s2;
printf(“%d/n”,s3->b);
return 0;
}
篇2:C语言面试编程题
从字符串数组和指针字符串在内存中的分配情况考查指针的使用。
#include
#include
#include
char *GetMemory(char *p)
{
p = (char *)malloc(100);
return p;
}//当调用此函数时,会在栈里分配一个空间存储p, p指向堆当中的一块内存区,当函数调用结束后,若函数没有返回值,
//系统自动释放栈中的P
void Test(void)
{
char *str = NULL;
str=GetMemory(str);
strcpy(str, “test”);
printf(“%s/n”,str);
}
char *GetMemory1(void)
{
char *p = “Test1”;
return p;
}//若换成char p=“hello world”; 就会在函数调用结束后,释放掉为“Test1”的拷贝分配的空间,返回的P只是一个野指针
void Test
{
char *str = “”;
str=GetMemory1;
printf(“%s/n”,str);
//str=GetMemory;
}
void GetMemory2(char p, int num)
{
*p = (char *)malloc(num);
}//当调用此函数时,会在栈里分配一个空间存储p, p指向栈中的一变量str,在此函数中为str在堆当中分配了一段内存空间
//函数调用结束后,会释放p, 但str所在的函数Test2还没运行完,所以str此时还在栈里.
void Test2(void)
{
char *str = NULL;
GetMemory2(&str, 100);
strcpy(str, “hello”);
printf(“%s/n”,str);
}
void Test3(void)
{
char *str=(char *)malloc(100);
strcpy(str, “hello”);//此时的str指向的是拷贝到栈里的“hello”,所以当释放掉str指向的堆空间时,str指向的栈里的值还是不变
free(str);
if(str != NULL)
{
strcpy(str, “world”);
printf(“%s/n”,str);
}
}
int main
{
Test;
Test1;
Test2;
Test3;
}
篇3:C语言面试编程题
C语言中sizeof的用法
void fun(char s[10])
{
printf(“%s/n”,s);
printf(“%d/n”,sizeof(s));//引用的大小
}
int main
{
char str={“sasdasdes”};
printf(“%d/n”,sizeof(str));//字符串数组的大小10(包含了字符'/0')
printf(“%d/n”,strlen(str)));//字符串的长度9
char *p=str;
printf(“%d/n”,sizeof(p));//指针的大小4
printf(“%d/n”,strlen(p));//字符串的长度9
fun(str);
void *h=malloc(100);
char ss[100]=“abcd”;
printf(“%d/n”,sizeof(ss));//字符串数组的大小100
printf(“%d/n”,strlen(ss));//字符串的长度4
printf(“%d/n”,sizeof(h));//指针的大小4
}
篇4:c语言面试编程题
c语言面试编程题
1、读文件 file1.txt 的'内容(例如):
12
34
56
输出到 file2.txt:
56
34
12
#include
#include
int main(void)
{
int MAX = 10;
int *a = (int *)malloc(MAX * sizeof(int));
int *b;
FILE *fp1;
FILE *fp2;
fp1 = fopen(“a.txt”,“r”);
if(fp1 == NULL)
{printf(“error1”);
exit(-1);
}
fp2 = fopen(“b.txt”,“w”);
if(fp2 == NULL)
{printf(“error2”);
exit(-1);
}
int i = 0;
int j = 0;
while(fscanf(fp1,“%d”,&a[i]) != EOF)
{
i ;
j ;
if(i >= MAX)
{
MAX = 2 * MAX;
b = (int*)realloc(a,MAX * sizeof(int));
if(b == NULL)
{
printf(“error3”);
exit(-1);
}
a = b;
}
}
for(;--j >= 0;)
fprintf(fp2,“%dn”,a[j]);
fclose(fp1);
fclose(fp2);
return 0;
}
2、写一段程序,找出数组中5:C语言编程题
C语言编程题
1)读文件file1.txt的'内容(例如):
12
34
56
输出到file2.txt:
56
34
12
(逆序)
第一题,注意可增长数组的应用.
#include
#include
int main(void)
{
int MAX = 10;
int *a = (int *)malloc(MAX * sizeof(int));
int *b;
FILE *fp1;
FILE *fp2;
fp1 = fopen(“a.txt”,”r”);
if(fp1 == NULL)
{printf(“error1″);
exit(-1);
}
fp2 = fopen(“b.txt”,”w”);
if(fp2 == NULL)
{printf(“error2″);
exit(-1);
}
int i = 0;
int j = 0;
while(fscanf(fp1,”%d”,&a[i]) != EOF)
{
i ;
j ;
if(i >= MAX)
{
MAX = 2 * MAX;
b = (int*)realloc(a,MAX * sizeof(int));
if(b == NULL)
{
printf(“error3″);
exit(-1);
}
a = b;
}
}
for(;–j >= 0;)
fprintf(fp2,”%dn”,a[j]);
fclose(fp1);
fclose(fp2);
return 0;
}
可谓是反序的经典例程.
void inverse(char *p)
{
if( *p = = ‘′ )
return;
inverse( p 1 );
printf( “%c”, *p );
}
int main(int argc, char *argv[])
{
inverse(“abc″);
return 0;
}
借签了楼上的“递规反向输出”
#include
void test(FILE *fread, FILE *fwrite)
{
char buf[1024] = {0};
if (!fgets(buf, sizeof(buf), fread))
return;
test( fread, fwrite );
fputs(buf, fwrite);
}
int main(int argc, char *argv[])
{
FILE *fr = NULL;
FILE *fw = NULL;
fr = fopen(“data”, “rb”);
fw = fopen(“dataout”, “wb”);
test(fr, fw);
fclose(fr);
fclose(fw);
return 0;
}
在对齐为4的情况下
struct BBB
{
long num;
char *name;
short int data;
char ha;
short ba[5];
}*p;
p=0×1000000;
p 0×200=____;
(Ulong)p 0×200=____;
(char*)p 0×200=____;
希望各位达人给出答案和原因,谢谢拉
解答:假设在32位CPU上,
sizeof(long) = 4 bytes
sizeof(char *) = 4 bytes
sizeof(short int) = sizeof(short) = 2 bytes
sizeof(char) = 1 bytes