在C語言中,輸入和輸出是經由標準庫中的一組函式來實現的。在ANSI/ISO C中,這些函式被定義在頭檔案;中。下面就由本站小編為大家介紹一下C語言面試程式設計題的文章,歡迎閱讀。
C語言面試程式設計題篇1
考查的是結構體和陣列的記憶體佈局情況。
#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]=i+64;
}
s2=s1+3;
s3=(B)s2;
printf("%d/n",s3->b);
return 0;
}
C語言面試程式設計題篇2
從字串陣列和指標字串在記憶體中的分配情況考查指標的使用。
#include
#include
#include
char *GetMemory(char *p)
{
p = (char *)malloc(100);
return 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 Test1
{
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;
}
C語言面試程式設計題篇3
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
}