PPSC Pointers
PPSC Pointers
Answer :
(a) getc( ) and getchar( )
The differences between getc( ) and getchar( ) are as follows.
getc( ) getchar( )
1. This is a function , that reads the data from any 1. It is a function , that reads the data from only
input stream. standard output.
2. Syntax : getc(file pointer); 2. Syntax: getchar(void);
3. Example 3. Example:
Int getc(FILE *FP); Int main(){
getchar(“%c”,getchar());
return0;
}
4. It may (or) may not have returns values. 4. It has atleast one return value.
Scanf( ) fscanf( )
1. This function is used for reading the input from the 1. This function is used for reading records from
standard input stream. the file stream.
2. Syantax: 2. Syntax:
Int…. Int….
Scanf(“constant chas *format”…..); fscanf(“FILE *stream ,constant char *format, ……….);
3. Scanf function contains “conversion specifiers”. 3. fscanf( ) functions may or may not contains
“conversion specifiers”.
4. Example 4. Example:
Scanf(“%d %s %f”, &age, &name, &price); fscanf1,%d%s%f”,&age,&name,&price);
++ptr ptr++
1. It is a value which is pointing in the particular 1. It is a value which is pointing in the particular
location and is incremented by one, which is equal address location and is incremented by one, which
to pre-increment operations. is equal to post-increment operations.
2. Consider that the address of the integer value is 2. Consider that the address of the integer value is
5000 i.e. 5000 i.e.
Address – 5000 Address – 5000
The ++ptr is evaluated as follows, The ptr++ is evaluated as follows,
++ptr = ++ptr Ptr++ = ptr++
= ++(5000) = (5000)++
= ++(value of address 500) = (value of address 500)++
= ++15 = 15++
++ptr =16 ptr =15++
3. The pictorial representation of the above scenario 3. The pictorial representation of the above scenario
is as follows, is as follows,
Address Address
location location
5000 5000
Ptr 15 value of Ptr 15 value of
Address location Address location
Address Address
location location
5000 5000
++Ptr value of Ptr++ value of
15+I= address location 15 address location
16
Answer:
Pointer:
Example:
Int x=2;
This statement will allow the system to locate integer variable ‘x’ and store ‘2’ in the
location. If ‘x’ is stored at location ‘1201’ by the system, then address of the variable ‘x’ will be
‘1210’
Let ‘y’ be the another variable stored at location ‘1315’ and ‘1210’ be stored in ‘y’. Since, y
store the address of x, it is the pointer. It is as follows.
Int *y;
y=1210;
Or
y=&x;
as address of ‘x’ is stored in ‘y’, the value of ‘x’ can be accessed using ‘y’ and is thus said
to be a pointer to ‘x’
1. Pointers are used in indirect addressing which is most oftenly used in assembly language
2. Pointer are used in dynamic memory management
3. A pointer Is used to access a location in the memory storage is allocated dynamically. This
dynamic allocation storage is called heap
4. Variable that are dynamically allocated from the heap are called heap dynamic variables. The
heap dynamic variable is not have the associated identifier. Hence, they can be referenced
only by pointer and reference type variable
5. Pointers are make used to the coding process simple and easy
6. Pointers are used to create recursive type that are important in data structure and algorithms
7. Pointers are used to enhance the language’s writeability
Distinguish between malloc() and calloc() functions used in dynamic memory allocations in c
A. MALLOC () CALLOC ()
1. malloc () is used to allocate memory of 1. calloc() is used to allocate the memory for an
required size of byte and return pointer array of elements which initializes them to zero
to the first byte of memory and return pointer to first byte memory
syntax: ptr=(cast_type ) malloc (size) Syntax: ptr=(cast_type)calloc (number of
elements,size )
2. it allocates memory in terms of byte 2. It allocates memory in terms of blocks
3. it takes single argument which 3. It takes two arguments which are number of
determine memory required in byte elements to allocate memory and size in byte of
a single variable
4. malloc(s) return a pointer with sufficient 4. Calloc(n,s) returns a pointer with sufficient
storage space for an entity of size ‘s’ successive storage space for ‘n’ objects each of
byte size ‘s’ bytes`
5. It allocates memory as a single 5. It allocates memory which may/may not be
contiguous block containing
6. It does not initialize the allocated 6. It initialize the allocated memory to x=zero
memory
Pointer Arithmetic
The arithmetic operations that can be performed on pointers are called pointer arithmetic. Pointer
arithmetic is considered as powerful method for performing array manipulation. It makes the task of
moving an element from one array to another array easier.
2. Postfix/Prefix
Increment/decrement operation increases/decreases the pointer value depending on the size of the
data object referred by the pointer.
Example
Consider a pointer variable 'P' whose data value is 8 and address is 8056. After performing
increment operation, the address of the pointer becomes 8058 (since integers occupies two bytes)
and after decrement operation, the address of pointer becomes 8054.
2. Postfix/Prefix Operation
Postfix Increment/Decrement
Example
After performing postfix increment and decrement, the value of pointer variable becomes 8058 and
8054, respectively.
Prefix Increment/Decrement
Addition Operation
It is possible to perform addition between a variable of pointer and a number or between two
numbers using pointers. However, it is not possible to perform addition between two pointers.
Example
Consider two variables p=40, q = 20 and two pointers ptr1, ptr2. The statement "*ptrl+q" specifies
that the value of q is added to pointer of p. Therefore, the result p+q = 60.
Subtraction Operation
It is possible to perform subtraction between a pointer variable and a number or between two
numbers using pointers.
Example
The statement "*ptrl-q" specifies that the value of q is subtracted from the pointer value of p.
Therefore, the result of p-q=20.
Multiplication Operation
It is possible to perform multiplication between two pointers or between a number and a pointer
variable. However, it is not possible to perform multiplication of address.
Example
The statement “*ptrl **ptr2" specifies that the pointer value of p is multiplied by the pointer value
of q. Therefore, the result of p*q = 800.
Division/modulo Operation
It is possible to perform division and modulo operation between two pointers or between a number
and a pointer variable. However, it is not possible to perform division of address with a constant.
Example
The statement “*ptr 1/*ptr2" specifies that the pointer value of p is divided by the pointer value of
q. Therefore the result of p/q = 2.