递增指针并给出值 [英] Increment pointer and give value

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

问题描述

我想为递增的指针赋值。

所以我有以下代码:



  #include   <   iostream  >  
使用 命名空间标准;

int main()
{
int * ptr,number1 = 1 ;
ptr =& number1;
ptr ++;
* ptr = 3 ;
cout<< * ptr;
}







我的尝试:



我希望结果为3,但程序停止工作并终止。我的代码有什么问题。

解决方案

你看起来很困惑。

要学习C / C ++基础知识,请阅读本书

https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf [ ^ ]

http://www.ime.usp.br /~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf [ ^ ]

C编程语言 - 维基百科,免费的百科全书 [ ^ ]



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

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

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

I want to give value to an incremented pointer.
So I have this following code:


#include<iostream>
using namespace std;

int main()
{
	int *ptr,number1=1;
	ptr=&number1;
	ptr++;
	*ptr=3;
	cout<<*ptr;
}




What I have tried:

I expect the result to be 3 but instead the program stops working and terminates. What is wrong with my code.

解决方案

You look confused with pointers.
To learn C/C++ basics read this book
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]
The C Programming Language - Wikipedia, the free encyclopedia[^]

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]


这篇关于递增指针并给出值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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