C Control StatementsC FunctionsC ArrayC PointerC Dynamic MemoryC StringC MathC StructureC UnionC File HandlingC PreprocessorC Command LineC Misc |
c pointersWe know c is very powerful language and the real power of c lies in pointers. The use of pointers makes the code more efficient. There are some use of pointers are
(i) Returning more than one value from function About memoryso before studying pointers this is important to understand how memory is allocated or organized in computer.The memory in the computer arrange data in bytes or made up of bytes and arrange in sequential manner. And each byte is identify by a block number or index number which is called address of that byte. We know that before using any variable it is necessary to declare that variable. since compiler reserve space for that variable. so datatype also declare with the variable name so compiler can know how much space needs to be reserved. let us take example how the compiler reserve space for variable or what happens when declare variable in c. suppose we declare salary of type int so compiler has reserved bytes numbered 2530, 2531, 2532, 2533 and 2534 for the variable salary, so the Base address of variable salary is 2530 . so we assign some value to this variable salary=3000; in this example value will be stored in 4 bytes in the binary form. The number of bytes allocated depend on the size of data types of variables, such as take example if we take double data type it will allocates 8 bytes. Address Operatorafter the concept of memory organize let us understand about the address operator concept. C provides an address operator (&) which return or give the address of variable when any variable follow by (&) operator or when place & operator before the variable.This operator is called as “the address of” so &salary means address of variable salary, lets us see example this program print the address of variable salary using address operator.
OUTPUT
The Address operator can not be used with the constant and the register variable or expression. Let us take some examples
Pointer variableA pointer is a variable that stores memory location. as other variables pointer has also name, and has to be declare and consume some space in memory.it is called pointer because its point to targeted memory in location. Declaration of pointer variablelike other variables in programming, pointer variable should also be declare before used. so The general syntx of pointer declaration is
In this example pname is the name of pointer variable, which should be a valid identifier in c, the (*) precedence tell compiler that variable is declare as a pointer. and the data type is the type of data valid in c Let us take example with pointer declaration
In the above example here ptr is the pointer variable that points to variable of int type, similarly fptr and chptr are pointer variables that points to float and char type. talk about default value of pointers global and static pointer automatic initialize to NULL but when we declare an automatic pointer variable its default value is garbage. so we should assign any address before using it.
So in this example ptr contains the address of salary variable, and it points to salary variable same as fptr contains the address of f variable In this example value of pointer ptr is 7000 which is the address of salary variable. and the value of fptr is 4020 which is the address of f |