`
weihe6666
  • 浏览: 429922 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

What is wrong with the following code

阅读更多
#include<iostream>
using namespace std;
struct S
{
	int i;
	int *p;
};

int main()
{
	//S s; // 结构体没有初始化,怎么会有地址的分配改写如下
	//s.i = 1;
	//int q = 2;
	//s.p = &q;
	//cout << "&s.i= " << &s.i << endl;
	S s;
	int *p = &s.i;  // p 指针指向s.i,相当于int *p;p = &s.i;
	cout << "p= " << p << endl;
	p[0] = 4;  // 相当于赋值给s.i;
	p[1] = 3;  // 相当于赋值给S.p,即是s.p里储存的值为3,而不是某个变量的地址
	cout << "s.i= " << s.i << endl;
	cout << "s.p= " << s.p << endl;
	cout << "&s.i= " << &s.i << endl;
	cout << "&s.p= " << &s.p << endl;
	cout << "p[0]= " << p[0] << endl;
	cout << "p[1]= " << p[1] << endl;
	s.p = p;  // 即是把00000003的值在赋给s.p,那么s.p指向的内存块的地址是00000003,	s.p[1] = 1;//s.p[1]指向的地址是00000004,直接把1赋给一个不知道的内存块会出错
	s.p[2] = 2;
	return 0;
}


输出的结果如下:
         cout << "s.i= " << s.i << endl;    4
cout << "s.p= " << s.p << endl;    00000003
cout << "&s.i= " << &s.i << endl;  0012FF5C
cout << "&s.p= " << &s.p << endl;  0012FF60
cout << "p[0]= " << p[0] << endl;  4
cout << "p[1]= " << p[1] << endl;  3
由以上输出可以看出:
               1.int *p = &s.i;是把s.i的地址赋给指针P,即:p=&s.i
             2.p[0]代表s.i;p[1]=s.p;
             3.p[1]=3;即是s.p指针值为3,指向变量的地址为3
分享到:
评论

相关推荐

    BobBuilder_app

    In this version I have done away with the b+tree and hash index in favor of my own MGIndex structure which for all intents and purposes is superior and the performance numbers speak for themselves....

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Style, also known as readability, is what we call the conventions that govern our C++ code. The term Style is a bit of a misnomer, since these conventions cover far more than just source file ...

    计算机网络第六版答案

    Since the queue grows when all the users are transmitting, the fraction of time during which the queue grows (which is equal to the probability that all three users are transmitting simultaneously)...

    VclZip pro v3.10.1

    Modified the SFX code (the code used to create the SFX stub distributed with VCLZip) so that it handles filenames that have been run through an OEM Conversion. The SFX was losing accented characters. ...

    S7A驱动720版本

    whether the driver is running in the demo mode or in the licenced mode (with a valid key). - New control tag: "!RuntimeMode:S7A". A digital (i.e. DI) or analog (i.e. AI) block with the I/O ...

    VB编程资源大全(英文源码 其它)

    MessageBoxDll.zip This is a custom message box creator.It provides the following functionalities that are not provided by the MsgBox function in VB.&lt;END&gt;&lt;br&gt;68,DragDrop.zip This is a code that ...

    Learning ServiceNow: Administration and development on the Now platform 2nd epub

    Read and write clear, effective code for the ServiceNow platform Identify and avoid common pitfalls and missteps that could seriously impact future progress and upgradeability Use debugging tools to ...

    C# Game Programming Cookbook for Unity 3D - 2014

    1. Making Games the Modular Way 1 1.1 Important Programming Concepts.....................................2 1.1.1 Manager and Controller Scripts...............................2 1.1.2 Script ...

    一个win32下的ARM开源编译器

    pre-UAL syntax is selected with the directive CODE16. UAL syntax is selected with the directive THUMB. pre-UAL syntax does not specify the "s" (flag update) in the opcodes for arithmetic operations ...

    WordPress 3 Search Engine Optimization.pdf

    Pursuing the wrong keywords 246 Poor internal anchor text 247 Flash, JavaScript, image links, and other non-readable content 248 Poor site architecture 250 Table of Contents [ viii ] Expert site ...

    ImpREC 1.7c

    - "Select Code Section(s)" to precise where is the code in the target - Fixed bug when loading imports file which contains Exact Call with ordinal - Debug stub scheme added (for getting API from an...

    一个跨平台的CString源码

    // fixes had inadvertently broken the DLL-export code (which is // normally commented out. I had to move it up higher. Also // this helped me catch a bug in ssicoll that would prevent // ...

    apktool documentation

    As you probably know, Android apps utilize code and resources that are found on the Android OS itself. These are known as framework resources and Apktool relies on these to properly decode and build ...

    .htaccess

    Although using .htaccess on your server is extremely unlikely to cause you any problems (if something is wrong it simply won't work), you should be wary if you are using the Microsoft FrontPage ...

    Kwin Scripts

    ("M" here stands for what is usually the windows key, "S" stands for shift) M-PgUp / M-PgDown: Switch layout for current desktop M-S-F11: Deactivate tiling on current desktop M-f: Toggle between ...

Global site tag (gtag.js) - Google Analytics