为什么C/C++语言使用指针

这是参加面试时,面试官问的一道开放性题目。

问题是:为什么C/C++语言使用指针?

这个问题一问出来,直接被面试官秒杀了,面试官大神,你怎么不按套路出牌啊?

说好的malloc和new的区别呢?说好的const和#define有什么优缺点呢?说好的进程和线程有什么区别和联系呢?说好的进程间通信有哪些方式呢?说好的%¥%#……@……*&()#!@#*……“……#%#%#呢?

说好的这些面试题,统统都没有。一上来就来这么一个问题。加上本身语言表达能力不够好,当时的心情就是这样的:不淡定中带点伤感!

说好的奥斯卡影帝呢?

现在想想,这个问题就是一个大坑。

首先,这个题目理解起来就有点猫腻。言外之意好像是想让你说C/C++中有指针,而C#或者Java等语言中没有指针。将这些编程语言做一下对比。

假装沉思了3秒钟,然后我就想当然的,顺着这么个思路,就开始顺口开河了。C#是高级语言,没有指针啥啥的就开始了。反反复复那么几句话,怎么扯也扯不出个清晰的逻辑出来。说出来的答案连自己都觉得是bullshit。

现在回头想想,当时的理解和答案是大错特错了。这本身就就是一个错误的问题。或者说,面试官就是故意将你往沟里带,等着你中套。

答案是:每一种编程语言都使用指针。不止C/C++使用指针。

为什么这样说?

因为后来在网上搜索答案时,在Quora上找到了一些大神们的解答。

“Everything uses pointers. C++ just exposes them rather than hiding them,”

It’s easier to give someone an address to your home than to give a copy of your home to everyone.

每一种编程语言都使用指针。C++只是将指针暴露给了用户(程序员),而Java和C#等语言择是将指针给隐藏起来了。

但糟糕的是,有些语言试着将指针隐藏起来,却露出了尾巴,有时候让人非常费解。

下面是30年老程序员Marcus Geduld举的栗子。引用如下:

Take, for instance, Javascript:

function foo( bar ) {
bar++;
}

var x = 5;

foo( x );

console.log( x );

Now, what is the value of x at then end of this code? 5 or 6?

Even though, in the function, the value of x gets assigned to bar and then incremented from 5 to 6, the log statement at the end will print 5. Why? because x’s value will be copied in to the function. In other words, bar won’t be pointing at the value of x, even though I wrote foo( x ). It will be pointing at a copy of that value.

Now, let’s say I wrote this:

function foo2( anArray ) {
anArray[ 0 ]++;
}

var myArray = [ 10, 20, 30 ];

foo2( myArray );

console.log( myArray );

In this case, the log will read [ 11, 20, 30 ]. So in the first case, the value was untouched. In this case, it’s been changed. Why, because foo2 didn’t get passed a copy of a value. Rather, it got passed a pointer—to the same array that myArray pointed to. So, in the first case, x and bar pointed to different values, whereas in the second case, myArray and anArray pointe to the same value, a pointer to [10, 20, 30 ].

Put more simply, this is a variable set to a value …

var a = 10;

Whereas this is a variable set to a pointer:

var a = [ 10 ];

But since nothing makes this explicit, you just have to learn some weird rules. And since many beginners don’t, they get hopelessly muddled. And they wind up accidentally changing values they didn’t intend to change and accidentallynot-changing values they did intend to change. Ugh!

Just in case this is unclear, compare this …

function foo( bar ) { bar++; console.log( bar ) };
var x = 5;
foo( x );
console.log( x );

output:65

… with this :

function foo2( bar ) { bar[ 0 ]++; console.log( bar[ 0 ] ); }
var x = [ 5 ];
foo2( x );
console.log( x[ 0 ] );

output:66

WARNING:面试时要时刻保持头脑的清醒,淡定,千万不要被面试官给引到沟里去了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值