开发者

What is the difference between mymethod(i) and mymethod(&i) in C?

I was wondering what the difference in is between calling a method l开发者_开发知识库ike :

int x;
mymethod(x); 

and

mymethod(&x);


Because C always does call-by-value, if you want the function to be able to change the x inside the function itself, you have to pass the address of x.

mymethod(x);

will pass x, for example if x is 2, you could as well have written mymethod(2)

mymethod(&x)

will pass the address to x. Now the method can change the value stored at that address, so after the function is completed, the actual value of x might have been changed.

Now you can also declare a pointer:

int* y; //y is now a pointer to a memory address
y = &x; //y now points to the memory address of x;
*y = 5; will set the "value found at the address y" to 5, thus will set x to 5;


mymethod(x) passes in the integer x as a parameter. mymethod(&x) passes in the address in memory of x. If you required a pointer-to-int as an argument, then you would use the second one.


In few words, when you declare a pointer, you precede it by an asterisk:

int *ptr;

When you pass &x instead of x, you are passing the memory address.

Please, read this useful introduction to the pointers.

Regards.


In the line: int x;

you allocate a piece of memory in the size of "int". for this explanation we will assume the size of int is 4 bytes (it might not be 4 bytes).

so after the line "int x;" there are 4 bytes in the memory assigned to "x". the value of "x" is inside these 4 bytes: for example, if x=4 then it will look in the memory like this: [0, 0, 0, 4] or in binary [0000000, 00000000, 00000000, 00000010]. (In real life it could also be [4, 0, 0, 0], but I won't get into that).

so the VALUE of x is 4.

But lets say I want the address of "x", where it is placed in the memory. This is where the operator "&" comes into play, using this operator I request the address of x.

so if [0, 0, 0, 4] starts at the location "0x12341234" in the memory, &x will return that (0x12341234). Now, if I want to store the address in a variable, this variable's type is not "int", but it is something that points to the address of int which is being marked as "int*".

So:

int x = 4; // <-- allocates memory of 4 bytes and fills its value with the number 4. int* pointer_to_x = &x; // <-- pointer_to_x points to the address where x is located in the memory.

if there is a method declared like so: void mymethod(int x) than we pass THE VALUE of x, so the method is being called mymethod(x).

if there is a method declared like so: void mymethod(int* x) than we pass a POINTER to the address of x, so the method is being called mymethod(&x).

It is really the tip of the iceberg, and I really tried to keep it simple as I could, so if you have further questions, just ask!

There are also terms called "by value" and "by reference" but you still need to understand better the difference between int and int* and than "by value" and "by reference" will be quite natural.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜