首页 > 个人简历 > 求职简历 > 笔试题目 > C语言面试编程题

C语言面试编程题

发布时间:2021-04-10

C语言面试编程题

  在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].ID=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

  }