Skip to content

Instantly share code, notes, and snippets.

@Nahiduzzaman
Last active August 18, 2018 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nahiduzzaman/3420f2a9b764736542effbee9d4ebfbd to your computer and use it in GitHub Desktop.
Save Nahiduzzaman/3420f2a9b764736542effbee9d4ebfbd to your computer and use it in GitHub Desktop.
Fact 1
int x, y, *z; // we declare 3 interger variables and z is a pointer variable
x = 40;
y = 44;
z = &x; // here "z" is holding address of x , so " *z " has the value of "x"
printf("value of x = %d", x); // 40
printf("value of *z = %d", *z); // 40
Fact 2:
**&y = ?
It will be simplied as : *(*(&y))
Example :
int x = 5, *y = &x;
(&y) is own address of y; which is ofcourse different the x's address not a big deal !
*(&y) => describes what has in y?, as y has x's address means i.e. &x . *(&y) prints address of x, (while *y prints value of x).
*(*(&y)) As *(&y) is address of x, *(address of x) gives the value of x , i.e. 5
Fact 3:
(1) if i = &j then *i has to be true i.e i is a pointer variable.
(2) *(i = &j) is equivalent of *(&j)
(3)
int *i, j = 5;
i = &j;
printing **i or *(*i) will give error (because you have not declared as double pointer variable), you can write *(&j) or *&j i.e value of j (5) or *(&i) i.e. value of i,
which the address no. of j.
Fact 4: Pointer and Array
#include <stdio.h>
int main(){
int i,*p, odd[5] = {1,3,5,7,9};
p = odd; // p = odd is equivalent of p = &odd[0] (address of first element of array).
// *p hold value of odd[0] i.e. 1 .
for (i = 0; i < 5; i++){
printf("%d \t", *p); // 1,3,5,7,9
p++; // with each iteration pointer points to next element of array.
}
}
Fact 5 :
int x = 0;
printf("%d", x++); // 0
//printf("%d", x);
because x++ evaluates like this first printed out current value of x, then increment value of x. so if we uncomment 3rd line
1 will be printed.
========================
int x = 0;
x++; // here x's value incremented by 1, so x is now 1
printf("%d", x++); // like before printf() will execute x's current value which is 1, then increment it by 1, so x will be 2 but
we will not see that on screen!
=========================
int x = 0;
x++;
printf("%d", x); // 1 but x will not get any increment.
==========================
int x = 0;
printf("%d", ++x); // here x will be incremented first than print on screen which is 1.
==========================
int x = 0;
++x;
printf("%d",++x); //here x is incremented in line 2 , now x is 1 then in line 3 , x will be incremented again by 1 then print which is 2
===========================
int x=0;
++x;
printf("%d \n", x++); // here x will be 1 for line 2 but in line 3, x will be printed as 1 , then increment by 1 , which we will can't see.
Fact 6:
int x,*p, odd[5] = {1,3,5,7,9};
p = odd;
x = *p++; // Order of evaluation *(p++) ,
printf("%d \n", x); // x has *p++ , x will be printed equal to the value of *(p) which is 1
// then p will be increment by 1, that means p's address will be updated
// by 1, p will point the next element of array. so *p has value 3 now!
// but we can't see that
// if you uncomment next line it will print 3
//printf("%d \n", *p);
===============================
int x,*p, odd[5] = {1,3,5,7,9};
p = odd;
x = *++p; // Order of evaluation *(++p) ,
printf("%d \n", x); // x has *++p , in line 3 p will be incremented by 1, i.e. it will point
// next elment of array which is address of odd[1], then *(address of odd[1])
// will give value 3, which will be saved in x, and it will be printed !
// Here no increment will happen on p after the printf().
===============================
int x,*p, odd[5] = {1,3,5,7,9};
p = odd;
x = ++*p; // Order of evaluation ++(*p) ,
printf("%d \n", x); // x has ++*p , in line 3 *p i.e. 1 will be increment first by 1 so
// x will save 2 and it will be printed !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment