Understanding Pointers with a Swap Program in C

I am trying to better understand pointers and references to C, and my course provided the following program as an example.

#include <stdio.h> void swap(int* a, int* b); int main(void) { int x = 1; int y = 2; swap(&x, &y); printf("x is %i\n", x); printf("y is %i\n", y); } void swap(int* a, int* b) { int tmp = *a; *a = *b; *b = tmp; } 

I have combined the following to understand if this will help me better understand what is happening, mainly regarding the need for use and against * (dereferencing). Basically, the syntax for declaring a pointer to an int (int * a) type compared to using an asterisk to "dereference" (* a = * b) is pretty confusing for me, and I was hoping someone could enlighten me. Here's another version of the foregoing, which I thought would help clarify, but actually doesn't:

 #include <stdio.h> void swap(int* a, int* b); int main(void) { int x = 1; int y = 2; int *a = &x; int *b = &y; swap(a, b); printf("x is %i\n", x); printf("y is %i\n", y); } void swap(int* a, int* b) { int tmp = *a; *a = *b; *b = tmp; } 

In short, my question is, is there a functional difference between what these two programs do? What is the difference between dereferencing ( *a = *b ) and using the & ( *a = &x ) operator. "

+7
c pointers dereference address-operator
source share
4 answers

You are misleading the expression and purpose.

*a = *b is called assignment. Note that it does not include the type name.

int *a = &x , on the other hand, is called a declaration. Note that you are initializing the pointer with address x. You are not looking for a pointer, but declaring it as a pointer to an int.

Look at this:

 int main() { int a = 5; int b = 2; int *c = &a; // c when dereferenced equals 5; **Declaration** int *d = &b; // d when dereferenced equals 2; **Declaration** int tmp = *c; // tmp equals 5 *c = *d; // c when dereferenced now equals 2 **Assignment** *d = tmp; // d when dereferenced now equals 5 **Assignment** return 0; } 

Finally, when you declare and initialize the pointer in the same expression, you assign the address of the pointer that you want to point to it. When you want to change the value that the object points to, you cast it with * . On the other hand, if you want to change what it points to, you will not find it.

+6
source share

&x returns the address x. x is of type integer and a has a type pointer to an integer. In this case (* a = & x), you assign the address x to a variable of type "pointer to an integer", which is equal to a . (* a = * b) - an assignment operation between two variables of the same type that are integers. I said an integer, because although a and b are "pointers to integers," in this operation they are dereferenced, and therefore the integer value to which they are attached is read.

Confusion, which, it seems to me, is due to the fact that (* a = & x) only makes sense when initializing the pointer.

+4
source share

If you set *a = *b , since a and b are pointer variables, the * operator will get the value cell in the memory pointed to by b , and <puts it in the cell pointed to by a .

For *a = &x operator and finds the address of the cell that is assigned to the variable x , and places it in the cell that points to it.

+1
source share

In short, my question is, is there a functional difference between what these two programs do?

No, the functional effect is exactly the same. IN

 int *a = &x; int *b = &y; swap(a, b); // swap(&a, &b) 

Type a same as &a , namely int* (pointer to int ). The only difference is that you use other variables for storage, which is not really necessary logically, but it is absolutely normal because it can help you understand the syntax.

What is the difference between dereferencing (* a = * b) and using & (* a = & x).

*a = *b assigns the value indicated by b (obtained with *b ) to those pointed to by a . To see it more clearly,

 int tmp = *b; *a = tmp; 

&(*a = &x) not a valid expression because you cannot store the address in int (in fact, you can, but it is outside the point).

-one
source share

All Articles