Selasa, 24 November 2015

Pointer & Reference in C

Hello readers!

i want to show you about Pointer&Reference in C.

Pointer

  • A variable that contains an adress from another variable

Pointer, based on it's name, used to point. What is appointed? the answer is a data. Pointer is can be imagined as a normal variable, but it doesn't save data, it saves an adress of data. That's why Pointer is used to point.

Example of Pointer: 



Relation between Pointer and Array

An array name is a constant pointer to the first element of the array.

Example of Array Pointer:



Relation between Pointer and Function

C programming allows passing a pointer to a function. To do so, simply declare the function parameter as a pointer type.

There is an example where we pass an unsigned long pointer to a function and change the value inside the function which reflects back in the calling function 




Reference

Actually, a reference is not really like a pointer.
A compiler keeps "references" to variables, associating a name with a memory address; that's its job to translate any variable name to a memory address when compiling.
When you create a reference, you only tell the compiler that you assign another name to the pointer variable; that's why references cannot "point to null", because a variable cannot be, and not be.
Pointers are variables, they contain the address of some other variable, or can be null. The important thing is that a pointer has a value, while a reference only has a variable that it is referencing.
Now some explanation of real code:
int x = 0;
int& y = x;
That's the Pointer & Reference in C. Hope this will help you. Thanks! :D

nb: Some source that really helps me very much (recommended):
http://www.tutorialspoint.com/cprogramming/c_pointers.htm
http://www.tutorialspoint.com/cprogramming/c_passing_pointers_to_functions.htm
http://www.cprogramming.com/tutorial/function-pointers.html
http://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in


Tidak ada komentar:

Posting Komentar