C Pointers
C Pointers
Address in C
If you have a variable var in your program, &var will give its address in
the memory.
We have used address numerous times while using the scanf() function.
scanf("%d", &var);
Here, the value entered by the user is stored in the address of var
variable.
#include <stdio.h>
int main()
{
intvar = 5;
printf("value: %d\n", var);
Pointers in C
Declaring a pointer.
int* p;
int *p1;
int * p2;
int* pc;
int c;
c = 5;
pc = &c;
To get the value of the thing pointed by the pointers, we use the *
operator. For example:
int* pc, c;
c = 5;
pc = &c;
printf("%d", *pc); // Output: 5
Note: In the above example, pc is a pointer, not *pc. You cannot and should
not do something like *pc = &c;
Example.
int* pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c); // Output: 1
printf("%d", *pc); // Ouptut: 1
Another example.
int* pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("%d", *pc); // Ouptut: 1
printf("%d", c); // Output: 1
Then, we changed *pc to 1 using *pc = 1;. Since pc and the address of c is
the same, c will be equal to 1.
int* pc, c, d;
c = 5;
d = -15;
Pointer operators
The & operator is used to display the address of a variable using ampersand
sign.
double a =10;
double*p;
p =&a;
*p would give us the value of the variable a. The following statement would
display 10 as output.
printf("%d",*p);
It would change the value of variable a. The statement above will change
the value of a from 10 to 200.
#include <stdio.h>
int main()
{
int* pc, c;
c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22
pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22
c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11
*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
Output
Address of c: 2686784
Value of c: 22
Address of c: 2686784
Value of c: 2
int* pc, c;
Here, a pointer pc and a normal variable c, both of type int, is created.
Since pc and c are not initialized at initially, pointer pc points to
either no address or a random address. And, variable c has an address but
contains random garbage value.
c = 22;
pc = &c;
This assigns the address of variable c to the pointer pc.
c = 11;
*pc = 2;
This change the value at the memory location pointed by the pointer pc to
2.
int c, *pc;
#include <stdio.h>
int main() {
int c = 5;
int *p = &c;
printf("%d", *p); // 5
return 0;
}
Why didn't we get an error when using int *p = &c;?
It's because
int *p = &c;
is equivalent to
int *p:
p = &c;
In both cases, we are creating a pointer p (not *p) and assigning &c to it.
int* p = &c;
#include <stdio.h>
int main() {
int x[4];
inti;
return 0;
}
Output
&x[0] = 1450734448
&x[1] = 1450734452
&x[2] = 1450734456
&x[3] = 1450734460
Address of array x: 1450734448
Notice that, the address of &x[0] and x is the same. It's because the
variable name x points to the first element of the array.
From the above example, it is clear that &x[0] is equivalent to x. And,
x[0] is equivalent to *x.
Similarly,
#include <stdio.h>
int main() {
inti, x[6], sum = 0;
printf("Enter 6 numbers: \n");
for(i = 0; i< 6; ++i) {
// Equivalent to scanf("%d", &x[i]);
scanf("%d", x+i);
In most contexts, array names are converted to pointers. That's the reason
why you can use pointers to access elements of arrays. However, pointers
and arrays are not the same.
#include <stdio.h>
int main() {
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
return 0;
}
For example:
#include <stdio.h>
void swap(int *n1, int *n2);
int main()
{
int num1 = 5, num2 = 10;
The address of num1 and num2 are passed to the swap() function using
swap(&num1, &num2);.
Pointers n1 and n2 accept these arguments in the function definition.
When *n1 and *n2 are changed inside the swap() function, num1 and num2
inside the main() function are also changed.
Inside the swap() function, *n1 and *n2 swapped. Hence, num1 and num2 are
also swapped.
Notice that, swap() is not returning anything; its return type is void.
#include <stdio.h>
int main()
{
int* p, i = 10;
p = &i;
addOne(p);
printf("%d", *p); // 11
return 0;
}
We then passed the pointer p to the addOne() function. The ptr pointer gets
this address in the addOne() function.
Inside the function, we increased the value stored at ptr by 1 using (*ptr)
++;. Since ptr and p pointers both have the same address, *p inside main()
is also 11.