1#include <stdio.h>
2int main() {
3 int c = 5;
4 int *p = &c;
5
6 printf("%d", *p); // 5
7 return 0;
8}
1#include<stdio.h>
2
3/*
4 '*' is the dereference operator; it will grab the value at a memory address.
5 it is also used to declare a pointer variable.
6
7 '&' is the address-of operator; it will grab the memory address of a variable.
8*/
9
10int main(int argc, char *argv[]) {
11 int x = 45; // Declare integer x to be 45.
12 int *int_ptr = &x; // int_ptr now points to the value of x. Any changes made to the value at int_ptr will also modify x.
13
14 x = 5; // Now, the value of x is 5. This means that the value at int_ptr is now 5.
15 *int_ptr = 2; // Now, the value at int_ptr is 2. This means that x is now 0.
16 int_ptr = NULL; // int_ptr now no longer points to anything. Make sure you never leave a dangling pointer!
17
18 return 0;
19}
1int* pc, c, d;
2c = 5;
3d = -15;
4
5pc = &c; printf("%d", *pc); // Output: 5
6pc = &d; printf("%d", *pc); // Ouptut: -15
1datatype *var;
2
3variable var actually holds the address of the data(memory where it is stored)
4*var lets you access the data stored at that address
1int c, *pc;
2
3// pc is address but c is not
4pc = c; // Error
5
6// &c is address but *pc is not
7*pc = &c; // Error
8
9// both &c and pc are addresses
10pc = &c;
11
12// both c and *pc values
13*pc = c;