无法获得这几行代码 [英] Cant getting this few lines of code

查看:72
本文介绍了无法获得这几行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法理解第3行的代码输出?请告诉我,我是初学者。



我尝试过:



cant understand this code output from 3rd line? explin me please, I am beginner.

What I have tried:

#include<stdio.h>
#include<conio.h>

int main()
{
  int i=4,*j,*k;
  printf("\ni=%d",i);
  printf("    &i=%u",&i);

  j=&i;
  printf("\n\ni=%d",i);
  printf("   j=%d",j);
  printf("    *j=%u",*j);
  printf("    &j=%u",&j);

  j=j+1;
  printf("\n\ni=%d",i);
  printf("    j=%d",j);	
  printf("    *j=%u",*j);
  printf("    &j=%u",&j);

  j=j+9;
  printf("\n\ni=%d",i);
  printf("     j=%d",j);
  printf("    *j=%u",*j);
  printf("    &j=%u",&j);

  k=j+3;
  printf("\n\ni=%d",i);
  printf("    j=%d",j);
  printf("    *j=%u",*j);
  printf("    k=%d",k);
  printf("    *k=%u",*k);
  printf("    &j=%u",&j);
  printf("    &k=%u",&k);
}

推荐答案

这是关于指针及其含义的所有内容: j k 被声明为整数指针变量,这意味着它们不包含与相同的整数值我,它们包含一个告诉你实际整数值在哪里的值。

如果你想到汽车,这辆车,那辆车,我的汽车,你的汽车都是指向汽车的价值观:他们没有识别特定的汽车,他们间接地识别它 - 你可以用手指指着不同的汽车并使用相同的术语这辆车或那辆车,或者你可以出售你现在的福特并买一辆奔驰车,而且我的车仍然有效,并确定你想开车到商店的当前车辆。



使用C(和其他语言)中的指针指针不包含整数,它将整数值保存在内存中 - 所有内存位置都有一个un的地址ique数字告诉硬件在哪里找到值。您可以通过在变量名前加上和&符号前缀来访问该地址:

This is all about pointers and what they are: j and k are declared as "pointer to integer" variables, which means that they don't contain an integer value in the same way as i does, they contain a value which tells you where the actual integer value is.
If you think about cars, "this car", "that car", "my car", "your car" are all "pointer to Car" values: they don't identify a specific vehicle, they identify it indirectly - you can point you finger at a different car and use the same term "this car" or "that car", or you could sell your current Ford and buy a Mercedes, and "my car" would still work and identify the current vehicle you want to drive to the shops.

With pointers in C (and other languages) the pointer doesn't contain the integer, it holds the address in memory of the integer value - all memory locations have an address which is a unique number which tells the hardware where to find the value. You access the address by prefixing the variable name with and ampersand:
j=&i;

表示获取变量的地址 i 并输入变量 j

当您打印 j 你得到一个号码,这是的地址我

Says "Take the address of the variable i and put in in the variable j"
When you print j you get a number, which is the address of i:

printf("   j=%d",j);



要访问整数值本身,您必须使用星号运算符'*'取消引用指针:


To access the integer value itself, you have to "dereference" the pointer, with the star operator '*':

printf("    *j=%u",*j)

使用<中的地址code> j 访问它指向的整数,并打印整数值的值。

从此时起,您的代码变得危险,并且你不应该做的事情 - 特别是如果你希望你的应用程序运行而不会崩溃,并且可靠和可维护。

That uses the address in j to access the integer it points to, and prints the value of the integer value.
From this point on, your code becomes dangerous, and does things you should not do - particularly if you want your application to run without crashing, and be reliable and maintainable.

j=j+1;

显然,增加 j 的值,但它会增加它的大小它指向什么 - 一个整数。这意味着在此之后,它指向 i 以外的其他东西(事实上它在C或C ++的大多数实现中都指向它)并且这使它变得危险 - 你不要知道如果使用它会发生什么。大多数情况下,它会做一些事情(虽然结果并不总是可预测的)但是如果你开始写值,你可以很容易地让你的程序崩溃。

这样做:

Increase the value of j, obviously, but it increases it by the size of what it points to - an integer. Which means after this, that it points at something other than i (it in fact points at itself in most implementations of C or C++) and that makes it dangerous - you don't know what will happen if you use it. Most times, it will do something (although the results aren't always predictable) but if you start writing values, you can very easily make your program crash.
Doing this:

j=j+9;
...
k=j+3;

更糟糕的是,因为它将指针移动到你的函数应该使用的内存之外,这增加了你的可能性得到问题。

不要这样做!许多代码错误是因为指针包含它们不应该包含的值 - 在您的内存值之外的单位化值,指向已经重用的数据的指针 - 因此请务必确保在更改指针时将其保留在内存中应该访问!

Is even worse, because it moves the pointer way outside the memory that your function should be using and that increases the likelihood that you will get problems.
Don't do it! Many code errors are because pointers contain values they shouldn't - unitialized values, outside your memory values, pointers to data that has been reused - so take great care to make sure that when you change a pointer that you keep it within the memory it is supposed to access!


这篇关于无法获得这几行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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