English 中文(简体)
C中点数和变数之间的差别?
原标题:Differences between pointers and variables in C?
  • 时间:2012-05-06 15:47:31
  •  标签:
  • c
  • pointers
#include <stdio.h>
int inc1(int x)  { return x++;    }
int inc2(int *x) { return (*x)++; }

int
main(void)
{
    int a;
    a = 3;
    printf("%d
", inc1(a) + a);
    printf("%d
", inc2(a) + a);
    return 0;
}

我通过一份过去的文件开展工作,其中一个问题就是跟踪对第6行至第9行之间变化的情况。 我有这样的理解点(指记忆点),但如果有人只是通过对整个法典的修改而与我交谈,那将是巨大的。

问题回答

I ll explain this nearly identical code that doesn t contain the error in your post:

#include <stdio.h>

int inc1(int x)  { return x++;    }
int inc2(int *x) { return (*x)++; }

int main(void) {
    int a;
    a = 3;
    printf("%d
", inc1(a) + a);
    printf("%d
", inc2(&a) + a);
    return 0;
} 

。 这意味着,返还的实际价值仍为3。

Next, the address of a is passed to inc2(). That means that what happens to the value in x happens to a. Again, post-increment is used, so what inc2() returns is 3, but after the call, a is 4.

然而,汇编者可随意评价各种表述,例如<代码>a或inc2(&a),在 >之间的任何顺序中。 这意味着a on the right of inc2(&a) + a may be 2-3 or 4 (取决于a?

电话线1(1)将印刷6(3+3)[第3条来自(c)1,因为员额加固操作员没有退还加固值]。 此外,由于按价值计算,在职能之外不会影响1元的值。

现在,如果文件中的“inc2()

printf("%d
", inc2(a) + a);

然后,你的代码将汇编(希望你在一栏中储存一个记忆点),但视其价值(如果是一 = 3)而定,你将获得“Fault”,因为你正在试图参照记忆地点3,而这个地点不应由你的方案进入。

或者,如果声明如下:

printf("%d
", inc2(&a) + a);

然后,第2(c)(二)项将提高通过点人(地址)的价值,但将退还仍然由于使用员额加固操作员而留下的3人的旧价值,但会增加一条路,随后获得新价值。 下一个指标将具有价值4,因为评价从左派到右边进行,因此将印刷7(3+4)。

我希望它澄清了C点和变量之间的区别。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...