zoukankan      html  css  js  c++  java
  • 基于Visual C++2013拆解世界五百强面试题--题18-程序结果分析2-终结篇

    第二部分程序结果分析,分析流程还是写入代码注释中


    分析下面程序的输出:


    #include <stdio.h>
    
    int main()
    {
    	char *a = "hello";
    	char *b = "hello";
    	if (a == b)
    		printf("YES");
    	else
    		printf("NO");
    	//由于a、b指针指向的常量字符串相同,编译器为了节省空间,
    	//将a、b指针变量都指向了hello字符串所在的内存位置
    	return 0;
    }

    分析下面代码,输出什么结果:


    #include <stdio.h>
    
    int func(int a)
    {
    	int b;
    	switch (a)
    	{
    		case 1: 30;
    		case 2: 20;
    		case 3: 16;
    		default: 0;
    	}
    	return b;
    }
    
    int main()
    {
    	printf("%d", func(1));
    	//输出结果随机,因为b变量没有初始化,也没有赋值,直接返回了
    	return 0;
    }

    写出程序运行结果:


    #include <stdio.h>
    
    int sum(int a)
    {
    	//auto int c = 0; vs2013不能用这种方式声明auto变量
    	auto c = 0;
    	static int b = 3;
    	c += 1;//每次调用函数是c最后都等于1
    	b += 2;//因为b是静态变量,所以第一次调用函数时b等于5,以后每次调用b增加2
    	return (a + b + c);
    }
    
    int main()
    {
    	int i;
    	int a = 2;
    	for (i = 0; i < 5; i++)
    	{
    		printf("%d,", sum(a));//所以第一次调用sum结果是2+1+5=8,第二次调用2+1+7=10,以此类推,输出结果为8,10,12,14,16,
    	}
    	return 0;
    }

    写出程序输出结果:


    #include <stdio.h>
    
    void g(int **);
    
    int main()
    {
    	int line[10], i;
    	int *p = line;
    	for (int i = 0; i < 10; i++)
    	{
    		*p = i;
    		g(&p);//调用后相当于p[0]++=i,p++
    	}
    	for (int i = 0; i < 10; i++)
    		printf("%d
    ", line[i]);//所以最后输出结果为1,2,3,4,5,6,7,8,9,10
    
    	return 0;
    }
    
    void g(int **p)
    {
    	(**p)++;//让传进来的二级指针最终指向的值自增
    	(*p)++;//让传进来的指针自增
    }


    分析下面程序的输出结果:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void GetMemory(char **p, int num)
    {
    	*p = (char *)malloc(num);
    }
    
    int main()
    {
    	char *str = NULL;
    	GetMemory(&str, 100);
    	strcpy(str, "hello");
    	free(str);//str指向的内存被释放了,但是str值没变,肯定不为NULL
    	if (str != NULL)
    	{
    		strcpy(str, "world");//这里很可能奔溃,如果没有奔溃向下之行
    	}
    	printf("
     str is %s", str);//输出  str is world
    
    	return 0;
    }


    此系列题目已经结束,如果有什么问题和疑问可以在下面留言互相探讨。

    原题我已经上传到这里了http://download.csdn.net/detail/yincheng01/6461073 ,

    解压密码为 c.itcast.cn

  • 相关阅读:
    第六章:随机化(续1)
    第六章:随机化
    PAT甲组 1010 Radix (二分)
    关于我的2019年度总结
    Codeforces 567D:One-Dimensional Battle Ships(二分)
    Codeforces 567C:Geometric Progression(DP)
    Codeforces 567B:Berland National Library(模拟)
    HDU 4790:Just Random(容斥)
    Codeforces 450C:Jzzhu and Chocolate(贪心)
    Codeforces 450E:Jzzhu and Apples(构造,数学)
  • 原文地址:https://www.cnblogs.com/new0801/p/6177578.html
Copyright © 2011-2022 走看看