在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指向堆當中的一塊內存區,當函數調用結束後,若函數沒有返回值,
//系統自動釋放棧中的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
}