帮助指针! ! [英] Help with pointers ! !

查看:84
本文介绍了帮助指针! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I understood most of the code but I am unclear on what the line ptr=-2 does to the overall code
    I mean does it change address of ptr or something ?

#include <iostream>

using namespace std;

void processptr(int *ptr)
{
    for(int i=4;i>2;i--)
    {
        (*ptr)*=i;
        ptr-=2;
    }
}

int main()
{
    int numbers[]={2,4,8,10};
    int *ptr=numbers+3;
    cout<<*ptr;
    cout<<'\n';
    processptr(ptr);
    cout<<'\n';
    for(int i=0;i<4;i++)
    {
    cout<<numbers[i]<<"@";
    cout<<"\n"<<i;
    }

    return 0;
}





我的尝试:



我尝试运行代码并查看输出但我仍然无法完全理解代码



What I have tried:

I tried Running the code and seeing the output but I still can't fully understand the code

推荐答案

ptr = -2 什么都不是,但这不是你的代码,你的代码是 ptr- = 2

使用调试器来查看你的代码在做什么。它允许你逐行执行第1行并在执行时检查变量。



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

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



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

调试器中没有魔法,它没有找到错误,它只是帮助你。如果代码没有达到预期的效果,那么你就接近了一个bug。
ptr=-2 means nothing, but this is not your code, your code is ptr-=2.
Use the debugger to see what your code is doing. It 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[^]

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天全站免登陆