Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic notes on pointers in C

Similar presentations


Presentation on theme: "Basic notes on pointers in C"— Presentation transcript:

1 Basic notes on pointers in C
int *p, m, s; // p is a pointer pointing at a variable of type int. // m and s are variables of type int. m = 5; // assigns integer 5 to variable m p = &m; // places the address of variable m into // variable p (that is a pointer of type integer). // As a result, the memory address that p is // pointing at, now contains integer 5. s = *p; // the content of memory being addressed by p, is // copied into s. now s = 5.

2 Basic notes on pointers in C
float x,y; int *p; x = 5.95; p = &x; y = *p; printf (“\n y = %f “, y); // What is printed for y is not Because, p = &x, // puts the address of the first byte of variable x that is of type float into a pointer of type int. y = *p; // puts the content of the first 2 bytes of memory being addressed // by p into y. But y is of type float that needs 4 bytes. // Thus the next 2 bytes of y contains irrelevant data. Also its first 2 bytes are not // in the float representation format. Therefore, y holds meaningless data.

3 Basic notes on pointers in C
int x, *p1, *p2; // p1 and p2 are pointers pointing to memory // addresses containing int data type. float y; x = 1350; // int variable x will contain integer y = ; // y is of type float. It will contain float value p1 = &x; // the memory address pointed by p1 will contain the // integer p1 and x point to the same memory location. p2 = &y; // the memory address pointed by pointer p2 represents // 2 bytes. It is expected the data being inserted in this // memory to have int representation format. // But, the first 2 bytes of 4 byte float variable y is copied into // 2 bytes of memory pointed by p2. // Since p2 points to memory to contain int data, // therefore, p2 will contain meaningless data.

4 ساختمان داده ليست پيونديLinked list data structure


Download ppt "Basic notes on pointers in C"

Similar presentations


Ads by Google