令人费解的计划 [英] Puzzling program
问题描述
美好的一天。
我在接受采访时被要求解释这个节目的行为。
void main()
{
char * s =" abc";
int * i =(int *)s;
printf("%x",* i);
}
问题是:为什么输出636261?我不认为我知道足够多了关于C来了解指针类型之间的转换是如何发生的。
感谢您的帮助。
RD
Good day group.
I was asked in an interview to explain the behavior of this program.
void main()
{
char *s = "abc";
int *i = (int *) s;
printf("%x", *i);
}
The question is: why is the output 636261? I don''t think I know enough
about C to understand how the conversions between pointer types are
occurring.
Thanks for any help.
RD
推荐答案
Rajeet Dalawal < rd@mailinator.com写信息
news:sl ******************** @ nospam.invalid ...
"Rajeet Dalawal" <rd@mailinator.comwrote in message
news:sl********************@nospam.invalid...
美好的一天。
我在接受采访时被要求解释这个程序的行为。
>
void main()
{
char * s =" abc";
int * i =(int * )s;
printf("%x",* i);
}
问题是:为什么是输出636261?我不认为我知道足够多了关于C来了解指针类型之间的转换是如何发生的。
感谢您的帮助。
Good day group.
I was asked in an interview to explain the behavior of this program.
void main()
{
char *s = "abc";
int *i = (int *) s;
printf("%x", *i);
}
The question is: why is the output 636261? I don''t think I know enough
about C to understand how the conversions between pointer types are
occurring.
Thanks for any help.
面试官正在寻找您了解C数据以字节形式存储在
内存中,以及玩指针你可以将
位重新解释为不同的类型。
-
免费游戏和编程好东西。
http://www.personal.leeds.ac .uk / ~bgy1mm
Rajeet Dalawal写道:
Rajeet Dalawal wrote:
Good day group。
我在接受采访时被要求解释这个程序的行为。
void main()
{
char * s =" abc";
int * i =(int *)s;
printf("%x", * i);
}
问题是:为什么输出636261?我不认为我知道足够多了关于C来了解指针类型之间的转换是如何发生的。
感谢您的帮助。
RD
Good day group.
I was asked in an interview to explain the behavior of this program.
void main()
{
char *s = "abc";
int *i = (int *) s;
printf("%x", *i);
}
The question is: why is the output 636261? I don''t think I know enough
about C to understand how the conversions between pointer types are
occurring.
Thanks for any help.
RD
请允许我..
#include< stdio.h>
int main(void){
char * s =" abc";
int * i =(int *)s;
printf("%08x",* i);
返回0;
}
我将假设英特尔CPU和ASCII字符。
你指向一个四字节的字符串,十六进制,61 62 63 00。 />
英特尔是小端的。当你将这四个字节视为int并且
将其打印为十六进制时,结果为00636261.
-
Joe Wright
所有事情都应尽可能简单,但并不简单。
---阿尔伯特爱因斯坦---
Please allow me..
#include <stdio.h>
int main(void) {
char *s = "abc";
int *i = (int *)s;
printf("%08x", *i);
return 0;
}
I''ll assume Intel CPU and ASCII characters.
You have s pointing to a four-byte string, in hex, 61 62 63 00.
Intel is little-endian. When you treat those four bytes as an int and
the print it as hex, the result is 00636261.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
" Keith Thompson" < ks *** @ mib.org写信息
news:ln ************ @ nuthaus.mib.org ...
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
这是一个非常糟糕的面试问题还是非常好的问题。 5
点用于解释行为;找到代码中所有
错误的50分(如果他认为这是一个有效的程序,则为面试官的-100分)。
It''s either a very bad interview question or a very good one. 5
points for explaining the behavior; 50 points for finding all the
errors in the code (and -100 points to the interviewer if he thinks
it''s a valid program).
您可以指出代码中的所有错误和不可移植的假设。
但您可能不会采访面试官。他正在决定是否要他,而且你会非常不确定你会得到的反应。
公司想要& ;团队成员,公司 - 说出他们不敢说的话
使用,服从。
-
免费游戏和编程好东西。
http://www.personal.leeds .ac.uk / ~bgy1mm
You could point out all the errors and nonportable assumptions in the code.
But you are not interviewing the interviewer, probably. He''s deciding
whether he wants you, and you would be very uncertain of the reaction you
would get.
Companies want "team players", corporate-speak for the word they dare not
use, obedience.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
这篇关于令人费解的计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!