0% found this document useful (0 votes)
21 views7 pages

PPSC Pointers

Uploaded by

nidaha7482
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

PPSC Pointers

Uploaded by

nidaha7482
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Distinguish between the following functions.

(a) getc( ) and getchar( )


(b) scanf( ) and fscanf( )
(c) printf( ) and fprintf( )
(d) feof ( ) and ferror( ).

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.

(b) Scanf( ) and fscanf()


The differences between scanf( ) and fscanf( ) are as follows.

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);

(c) Printf( ) and fprintf( )


The differences between printf( ) and fprintf( ) are as follows.
Printf( ) fprintf( )
1. Print() function is used for writing the records onto 1. fprintf( ) function is used for writing records
the output screen. into a file.
2. Syntax: 2. Syntax:
Printf(“format string”, list); fprint(file_ descriptor ,”format string”, list);
3. Example: 3. Example:
printf(“%d %s %f ”,age, name, marks); fprintf(F1,”%d %s %f, age, name, marks);
4. This function cannot handle a group of mixed data 4. This function can handle a group of mixed data
simultaneously. simultaneously.
(d) feof( ) and ferror( )
The differences between feof( ) and ferror( ) are as follows.
feof( ) ferror( )
1. feof() is library function that determines whether the 1. Ferror( ) is a library function which determines
file pointer is at the end of the file or not. the occurrence of the error performed by the
read / write operations on a file.
2. Syntax: 2. Syntax:
feof(fp); ferror(fp);
3. It takes a files pointer as its parameter. 3. It takes file pointer as its argument.
4. It returns , zero on success and non zero if EOF is 4. It returns zero as its return value.
reached.
5. Example: 5. Example:
If(feof(fp)) If(ferror(fp)!=0)
Printf(“End of data\n); Print(“There is an error in a file”);
1.Differences between ++ptr and ptr++

++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

4. Consider the following code, 4. Consider the following code,


Int main( ) Int main( )
{ {
Int ival = 15; Int ival = 15;
Int * ptrval = NULL; Int * ptrval = NULL;
ptrval = &ival; ptrval = &ival;
printf(“value of ++ptrval:”,++ptrval); printf(“value of ptrval++:”,ptrval++);
gtechar( ); gtechar( );
return 0; return 0;
} }
Then the output will be as follows, Then the output will be as follows,
Value of ++ptrval: 16 Value of ptrval++: 15
 What is a pointer? List some of the features or uses of pointers

Answer:

Pointer:

A variable that stores the address of another variable is called a ‘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’

features of using pointers :

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

 List the operations performed on pointer variable

A. The pointer arithmetic operations that are valid are


1. Assignment of pointer if they have similar datatype
2. Adding or subtracting a pointer to and from an integer
3. Subtracting or comparing two pointers with the data member of similar array
The pointer arithmetic operations that are invalid are:
1. Adding two pointers
2. Multiplying two pointers
3. Dividing two pointers
4. Type cast the pointers as float or double
5. Assigning a pointer of one datatype to a pointer of another data type without performing type
casting
6. shifting of pointer
Q. What is pointer arithmetic? Discuss the different arithmetic operations with pointers.
Answer:

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.

Arithmetic Operations with Pointers

The different arithmetic operations that can be performed on pointers are.

1. Increment (++)/Decrement (--)

2. Postfix/Prefix

3. Addition, subtraction, multiplication, division / modulo operations.

1. Increment (++)/Decrement (--) Operations

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

In postfix increment/decrement operations, the address of the pointer variable is


incremented/decremented after displaying the address values.

Example
After performing postfix increment and decrement, the value of pointer variable becomes 8058 and
8054, respectively.

 Prefix Increment/Decrement

In prefix increment/decrement operations, the address of the pointer value is


incremented/decremented first and then displayed. The result obtained after performing the prefix
increment/decrement operations is equal to the result of postfix increment/decrement operations.

3. Addition, Subtraction, Multiplication, Division/Modulo Operations

 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy