an interview question(3)
最近看了些C面试题顺便复习一下C语言,现贴一些出来和大家分享。
#include <stdio.h>
void main ()
{
int arr[]={,,,,};---------
int *ptr=arr; ---------
*(ptr++)+=; ----------
printf("%d,%d\n",*ptr,*(++ptr)); ---------
}
【解析】:这段代码应该是比较简单的一个面试题,首先在主函数里,定义了一个一维数组,接着ptr被初始化为数组arr的首地址,也就是一二两句一般都没啥问题,但是第三句有可能就会产生分歧了,有人可能认为,根据书中的表达,括号的优先级应该是大于指针大于++大于+=,所以应该是先执行ptr++,然后事实并非如此,此处是需要先赋值,再自加的,也就是第三句代码是被两句代码合二为一了,可分解如下:*ptr=*ptr+123; ptr++;由数组名是数组首元素的地址可知,首元素为6+123=129,而ptr自加一,指向了7所在的地址。第四句代码为printf输出,printf输出是从右向左压栈(有兴趣的朋友可以测试下printf为什么是从右向左压栈输出的:))所以输出时先执行*(++ptr),也就是指针再加一,指向元素8所在的地址,*(++ptr)为取出元素8,此时ptr已经为指向8所在的地址,故*ptr同样为8.
使用VS2012编译如下:


an interview question(3)的更多相关文章
- an interview question(1)
声明:本文为博主原创文章,未经博主允许不得转载. 以下是英文翻译: warnning: Copyright!you can't reprint this blog when you not get b ...
- Core Java Interview Question Answer
This is a new series of sharing core Java interview question and answer on Finance domain and mostly ...
- shit LeetCode interview Question
shit LeetCode interview Question https://leetcode.com/interview/1/ 有点晕,啥意思,没太明白,到底是要按什么排序呀? 去掉 标识符 不 ...
- JavaScript interview Question - Create a Array with two papameters without using loop!
JavaScript interview Question - Create a Array with two papameters without using loop! JavaScript - ...
- An interview question from MicroStrategy
去年校招时的一道面试题,觉得蛮有意思,贴出来. Question: Spy start at a, during an interval he moves |b| to right when b &g ...
- an interview question(4)
版权声明:本文为博主原创文章,未经博主允许不得转载. 写这篇博客前请让博主先吐糟下自己的PC. i3+2G内存+开了一上午=C盘剩下0字节+打开VS2012花了半个小时+一晚上的心情不好 吐槽完PC, ...
- an interview question(2)
感觉现在好多面试题还是很注重基础的,今天面试时就遇到这题,回来一查后才知道此题是国内某著名通信公司的一道机试题:) 给定一个数组input[ ],如果数组长度n为奇数,则将数组中最大的元素放到 out ...
- Interview Question
HDS(11.16.2015): How to design an non-stop website like Google or Amazon? What design patterns are y ...
- Amazon Interview Question: Design an OO parking lot
Design an OO parking lot. What classes and functions will it have. It should say, full, empty and al ...
随机推荐
- web前端基础知识 - Django进阶
1. 路由系统 1.1 单一路由对应 url(r'^index$', views.index), 1.2 基于正则的路由 url(r'^index/(\d*)', views.index), url( ...
- 列表边框column-rule
column-rule主要是用来定义列与列之间的边框宽度.边框样式和边框颜色.简单点说,就有点类似于常用的border属性.但column-rule是不占用任何空间位置的,在列与列之间改变其宽度不会改 ...
- Windows Phone 一、XAML基础语法
XAML的命名空间 命名空间格式:语法结构为“xmlns:”+“命名空间前缀名”,默认命名空间无需定义命名空间前缀名“xmlns” 命名空间的声明 <Page x:Class="App ...
- java实现二分查找
/** * 二分查找 * @param a * @param n * @param value * @return * @date 2016-10-8 * @author shaobn */ publ ...
- 商品条形码(JBarcode)Java版(二)
下午开了一个下午的会议,其实开会我听不进去,因为今天妖都特别冷,下班在公司等待小媳妇一个钟头,然后带着她去吃饭,吃完饭回到家.她做运动,我就开始慢慢整理我自己的小博客. ——题记 先说一下,写这篇文章 ...
- Java中Map的三种遍历方法
Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历. 告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...
- CRM 2013 限制Lookup
var oTo = document.getElementById("customerid_i"); oTo.setAttribute("defaulttype" ...
- asp.net mvc adminlte第一波
首页模板选用官方DEMO中的Blank模板,这个模板相对来说是最干净的. 首页模板的分割: 官方文档是分的4个部分 Wrapper .wrapper. A div that wraps the who ...
- js爬虫
1.爬虫相关的包 (1)const request = require('superagent'); // 处理get post put delete head 请求 轻量接http请求库,模仿浏 ...
- java获取两个时间的相隔时间,包括年、月、日、时、分、秒
public static final int YEAR_RETURN = 0; public static final int MONTH_RETURN = 1 ...