任何人都可以解释这段代码的工作原理吗 [英] Can any one explain how this code works?

查看:96
本文介绍了任何人都可以解释这段代码的工作原理吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释这段代码是如何运作的吗?



我尝试过:



执行时打印临时值为-48,* end指定为'B',临时值为0如何??

#include< stdio.h> < br $>


void printstr(char str [])

{

printf(string is:%s, str);



}



int main()

{

char * end =A,* temp;

char * addressTable [128];



temp = addressTable [* end];

printf(%d,* temp);

返回0;

}

can any one explain how this code works??

What I have tried:

when executed it prints temp value as -48 and *end is assigned with 'B' the temp value is 0 how??
#include <stdio.h>

void printstr(char str[])
{
printf("string is: %s",str);

}

int main()
{
char *end="A",*temp;
char* addressTable[128];

temp = addressTable[*end];
printf("%d",*temp);
return 0;
}

推荐答案

简单:您没有为 addressTable 分配任何值 - 所以它获得的值将取决于您的编译器正在使用和编译程序时指定的选项。某些组合将为您提供全零,其他组合将为您提供在分配空间之前碰巧在内存中的所有随机数据。

你使用char *作为数组索引器只是愚蠢而危险。根据机器(以及编译器和选项),它可以执行以下操作之一,包括使应用程序崩溃。只使用整数来访问数组!



基本上,这段代码是一个不好的想法和不清楚的想法的大杂烩:忽略它,删除它,阅读你的课程笔记!但是不要试图从中学到任何有用的东西,根本就没有任何用处!
Simple: you don't assign any values to your addressTable - so the values it gets will depend on the compiler you are using and the option you specify when you compile your program. Some combinations will give you all zeros, others will give you all "random" data that happened to be in memory before the space was allocated.
And your use of a char* as an array indexer is just silly, and dangerous. Depending on the machine (and the compiler, and options) it could do one of several things, including crash your app. Only use integers to access arrays!

Basically, that code is a hodge-podge of bad ideas and unclear thinking: ignore it, delete it, read your course notes! But don't try to learn anything useful from it, there isn't much of any use there at all!


引用:

可以解释这段代码是如何工作的吗?

can any one explain how this code works??

由于结果取决于你的计算机和编译器,最好是让你的计算机告诉你它做了什么,使用调试器。 br />


有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。

Since the result depend on your computer ans compiler, the best is to ask your computer to show you what it does, use the debugger.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于任何人都可以解释这段代码的工作原理吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆