Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

EST 102 - Programming in C Lecture notes

Rating
-
Sold
-
Pages
22
Uploaded on
23-04-2022
Written in
2021/2022

EST 102 - Programming in C Lecture notes

Institution
Course

Content preview

EST102 Programming in C


Module V
Pointers and Files-Basics of Pointer: declaring pointers, accessing data though pointers, NULL
pointer,array access using pointers, pass by reference effect. File Operations: open, close, read,
write, append Sequential access and random access to files: In built file handling functions
(rewind() ,fseek(), ftell(),feof(), fread(), fwrite()), simple programs covering pointers and files.


POINTERS
The pointer in C language is a variable which stores the address of another variable.
This variable can be of type int, char, array, function, or any other pointer. The size of the
pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is
2 byte.
Consider the following example to define a pointer which stores the address of an
integer variable.
int n = 10;
int *p = &n; // Variable p of type pointer is pointing to the address of the variable n of type
integer.
Declaration of Pointer Variable
The pointer in C language can be declared using * (asterisk symbol). It is also known
as indirection operator used to dereference a pointer.
datatype *ptrvar;
where ptrvar is any identifier
int *a; //pointer to integer
char *c; //pointer to char

Initialization of Pointer Variable
The process of assigning the address of a variable to a pointer variable is known as
initialization. All uninitialized pointers have some garbage value so it should be initialized.
Example
int a;
int *p; //declaration
p=&a; //initialization




IIPE 1

, EST102 Programming in C


Accessing Data through Pointers
A variable can be accessed through its pointer using unary operator ’*’. Also known
as indirection operator or dereferencing operator.
Consider the following example:

#include<stdio.h>
void main()
{
int n = 10;
int *p;
p = &n;
printf("Address of n=%u\n",&n);
printf("Value of n=%d\n",n);
printf("Address of p=%u\n",&p);
printf("Value of p=%u\n",p);
printf("Value of *p=%d\n",*p); // *p = value of n;

}
OUTPUT
Address of n=6487628
Value of n=10
Address of p=6487616
Value of p=6487628
Value of *p=10

Each variable has two properties: address and value. Address of a variable is the
memory location allocated for that variable. Value of a variable is the value stored in the
memory location allocated to them. The address of a variable can be derived using address
of (&) operator. In the example above, the address of n is 6487628 and value is 10.


Variable Name Address Value
n 6487628 10
When p is assigned with &n, p stores the address of n. Thus p points to n. In order to
retrieve the value of n, we can make use of *p.
Pointer Name Address of p Value of p Asterisk Value(*p)
6487628 10
p 6487616
Address of variable n Value of n


IIPE 2

, EST102 Programming in C


NULL Pointer
A pointer that is not assigned any value but NULL is known as the NULL pointer. If
you don't have any address to be specified in the pointer at the time of declaration, you can
assign NULL value. It will provide a better approach.
int *p=NULL;
OPERATIONS ON POINTERS
 Address of a variable can be assigned to a pointer variable.
 One pointer variable can be assigned to another pointer variable provided both
points to the items of same datatype.
 A NULL value can be assigned to a pointer variable.
 An integer quantity can be added to or subtracted from a pointer variable. The
result will be a pointer.
 When we increment a pointer, its value is increased by the length of its data type.
 One pointer variable can be subtracted from another pointer variable provided both
points to the elements of the same array. The result will be an integer value.
 Two pointer variables can be compared provided both points to the items of same
datatype.
 A pointer variable can be compared with NULL values.
Write a program to find the sum of two numbers using pointers
#include<stdio.h>
void main()
{
int x,y,sum,*xp,*yp;
printf("Enter value of x:");
scanf("%d",&x);
printf("Enter value of y:");
scanf("%d",&y);
xp=&x;
yp=&y;
sum=*xp+*yp;
printf(" Sum is %d \n",sum);
}
OUTPUT
Enter value of x:10

IIPE 3

Written for

Institution
Course

Document information

Uploaded on
April 23, 2022
Number of pages
22
Written in
2021/2022
Type
Class notes
Professor(s)
Sajitha m
Contains
All classes

Subjects

$7.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
ashiqrahman

Get to know the seller

Seller avatar
ashiqrahman APJ Abdul Kalam Technological University
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
4 year
Number of followers
0
Documents
6
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions