c语言经典编程题abc C语言面试编程题篇1
考查的是结构体和数组的内存布局情况。
#include
#include
typedeftructarray1{
intID;
tructarray1某ne某t;
}A;
typedeftructarray2{
intID;
inta;
intb;
intc;
}某B;
intmain()
{
A1[15];
A某2;
B3;
for(inti=0;i<10;i++)
{
1[i].ID=i+64;
}
2=1+3;
3=(B)2;
printf("%d/n",3->b);
return0;
}
C语言面试编程题篇2
从字符串数组和指针字符串在内存中的分配情况考查指针的使用。
#include
#include
#include
char某GetMemory(char某p)
{
c编程必背100题
p=(char某)malloc(100);
returnp;
}//当调用此函数时,会在栈里分配一个空间存储p,p指向堆当中的一块内存区,当函数调用结束后,若函数没有返回值,
//系统自动释放栈中的P
voidTet(void)
{
char某tr=NULL;
tr=GetMemory(tr);
trcpy(tr,"tet");
printf("%/n",tr);
}
char某GetMemory1(void)
{
char某p="Tet1";
returnp;
}//若换成charp[]="helloworld";就会在函数调用结束后,释放掉为"Tet1"的拷贝分配的空间,返回的P只是一个野指针
voidTet1()
{
char某tr="";
tr=GetMemory1();
printf("%/n",tr);
//tr=GetMemory();
}
voidGetMemory2(char某某p,intnum)
{
某p=(char某)malloc(num);
}//当调用此函数时,会在栈里分配一个空间存储p,p指向栈中的一变量tr,在此函数中为tr在堆当中分配了一段内存空间
//函数调用结束后,会释放p,但tr所在的函数Tet2还没运行完,所以tr此时还在栈里.
voidTet2(void)
{
char某tr=NULL;
GetMemory2(&tr,100);
trcpy(tr,"hello");
printf("%/n",tr);
}
voidTet3(void)
{
char某tr=(char某)malloc(100);
trcpy(tr,"hello");//此时的tr指向的是拷贝到栈里的"hello",所以当释放掉tr指向的堆空间时,tr指向的栈里的值还是不变free(tr);
if(tr!=NULL)
{
trcpy(tr,"world");
printf("%/n",tr);
}
}
intmain()
{
Tet();
Tet1();
Tet2();
Tet3();
}
C语言面试编程题篇3