笔试训练1 知识点整理

static关键字的作用
1设置局部变量的存储域,static局部变量的作用范围为该函数体,该变量的内存只被分配一次,因此其值在下次调用时仍维持上次的值;

 (只执行一次,延长局部变量的声明周期直至程序结束)


2限制全局变量的作用域,static全局变量可以被模块内所用函数访问,但不能被模块外其它函数访问;


3限制函数的作用域,static函数只可被这一模块内的其它函数调用,这个函数的使用范围被限制在声明它的模块内;


局部变量和全局变量是否可以重名:

 , 局部会屏蔽全局. 要用全局变量, 需要使用"::"

程序的内存分配:

代码段,数据段(数据区、bss),堆,栈

注:数据区:初始化的全局变量

 bss:静态变量和未初始化的全局变量/初始化为0

堆和栈的区别:

一、堆栈空间分配区别:

 1、栈空间:由操作系统自动分配释放 ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈;
2、堆空间: 一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收,分配方式倒是类似于链表。
二、堆栈缓存方式区别:
 1、栈使用的是一级缓存, 他们通常都是被调用时处于存储空间中,调用完毕立即释放;
 2、堆是存放在二级缓存中,生命周期由虚拟机的垃圾回收算法来决定(并不是一旦成为孤儿对象就能被回收)。所以调用 这些对象的速度要相对来得低一些。
三、堆栈数据结构区别:
  堆(数据结构):堆可以被看成是一棵树,如:堆排序;
  栈(数据结构):一种先进后出的数据结构。

程序阅读题:

#include<stdio.h>
#include<string.h>
int inc(int a)
{
return(++a);
}

int multi(int*a, int *b, int*c)
{
return(*c = (*a) * (*b));
}

typedef int(FUNC1)(int in);
typedef int(FUNC2) (int*, int*, int*);

void show(FUNC2(fun), int arg1, int*arg2)
{
FUNC1 *p = &inc;
int temp = p(arg1);
fun(&temp, &arg1, arg2);
printf("%d\n",*arg2);
}

int main()
{
int a;
show(multi, 10, &a);
return 0;
}

答案:110

#include<stdio.h>
#include<string.h>
void test2()
{
char string[10], str1[10];
int i;
for(i = 0; i < 10; i++)//i < 9
{
str1 = 'a';//错误:赋值时类型不兼容
}

//string[10] = '\0';
strcpy(string, str1);
printf("%s\n",string);
}
int main()
{
test2();
return 0;
}

修改的方法:

红色标识处


编程题:

写一个函数,判断单链表是否存在环:

struct  listtype  

{  

    int data;  

    struct listtype * next;  

}list;  

int find_cicle(list *head)  

{  

     list *pFast=head;  

     list *pSlow=head;  

     if (pFast==NULL)  

     {  

          printf(“链表是空的\n”);

 return -1;

      }  

     while(pFast && pFast->next)  

    {  

        pFast=pFast->next->next;  

        pSlow=pSlow->next;  

        if (pFast==pSlow)  

       {  

  Printf(“链表存在环!~\n”);

           return 1;  

        }  

     }  

 Printf(“链表不存在环!~\n”);

     return 0  

N个人排成一圈,顺序排好,从第一个开始报数(1-3),凡是报的3的退出圈子,问最后剩下哪个?

#include <stdio.h>


int main()
{
int a;
int i,j=0;
int count = 0, x = 0, all = 1;
printf("请输入人数:\n");
scanf("%d",&a);

x = a;
int b[x][2];


for(i = 0; i < x; i++)
{
b[i][0] = i+1;
b[i][1] = 1;
}//初始化


while(1)
{
if(b[j%x][1] == 0)
{
j++;
continue;
}

count++;
if(count%3 == 0)
{
b[j%x][1] = 0;
count = 0;
++all;
}
j++;
if(all == x)
{
break;
}
}//去除死亡者


for(i = 0; i < x; i++)
{
if(b[i][1] == 1)
{
printf("幸存者是%d ",i+1);
}
}
printf("\n");//输出幸存者
return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_38313246/article/details/79163314
今日推荐