















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















A pointer is a variable that stores the address of another variable. Instead of storing a value directly, it stores the memory address of a variable.
int n = 10; int *p = &n;
n = normal variable p = pointer variable &n = address of n *p = value at address (value of n)
datatype *pointer_name;
int *p; // pointer to integer char *c; // pointer to character
Assigning address to a pointer:
int a; int *p;
p = &a;
*p // gives value stored at address
#include
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 stored in p = %u\n", p); printf("Value of *p = %d\n", *p); }
A NULL pointer is a pointer that does not point to any memory location.
int *p = NULL;
Pointer stores address, not value & gives address
Example:
int pointer increases by 2 or 4 bytes (depends on system)
p++; p--;
Two pointers of the same array can be subtracted to get distance between them.
int diff = p2 - p1;
Two pointers can be compared if they point to same data type.
if(p1 == p2)
A pointer can be checked whether it is NULL or not.
if(p == NULL)
v o i d m a i n ( ) { i n t x , y , s u m ; i n t * x p , * y p ; p r i n t f ( " E n t e r v a l u e o f x : " ) ; s c a n f ( " % d " , & x ) ; p r i n t f ( " E n t e r v a l u e o f y : " ) ; s c a n f ( " % d " , & y ) ; x p = & x ; y p = & y ;
s u m = * x p + * y p ; p r i n t f ( " S u m i s % d " , s u m ) ; }
KEY IDEA
xp stores address of x yp stores address of y *xp and *yp give values Addition is done using pointer values
v o i d m a i n ( ) { i n t x = 1 0 , y = 2 0 , t e m p ; i n t * a , * b ;
p r i n t f ( " B e f o r e S w a p p i n g \ n " ) ; p r i n t f ( " V a l u e o f x = % d \ n " , x ) ; p r i n t f ( " V a l u e o f y = % d \ n " , y ) ;
a = & x ; b = & y ;
t e m p = * a ;
p r i n t f ( " \ n A f t e r S w a p p i n g \ n " ) ; p r i n t f ( " V a l u e o f x = % d \ n " , x ) ; p r i n t f ( " V a l u e o f y = % d \ n " , y ) ; }
KEY IDEA
a points to x b points to y Values are swapped using *a and *b
#include
void main() { int a[10], n, i; scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d", &a[i]); for(i = 0; i < n; i++) printf("%d ", *(a + i)); }
#include
void main() { int a[10], n, i, j, temp; scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d", &a[i]); for(i = 0; i < n-1; i++) { for(j = 0; j < n-i-1; j++) { if(*(a+j) > *(a+j+1)) { temp = *(a+j); *(a+j) = *(a+j+1); *(a+j+1) = temp; } } } for(i = 0; i < n; i++) printf("%d ", *(a+i)); }
A pointer to pointer is a pointer that stores the address of another pointer variable. It is also called a double pointer.
A normal pointer stores the address of a variable. A pointer to pointer stores the address of another pointer. We use two * symbols to declare it.
datatype **ptr;
a ā normal variable p ā pointer to a q ā pointer to pointer p
v o i d m a i n ( ) { i n t a = 1 0 ; i n t * p , * * q ; p = & a ; q = & p ; p r i n t f ( " V a l u e o f a = % d \ n " , a ) ; p r i n t f ( " V a l u e o f * p = % d \ n " , * p ) ; p r i n t f ( " V a l u e o f * * q = % d \ n " , * * q ) ; }
Value of a = 10 Value of *p = 10 Value of **q = 10
p r i n t f ( " A f t e r s w a p p i n g : x = % d y = % d \ n " , x , y ) ; }
Output
Before swapping: x=10 y= After swapping: x=20 y=
Key Points
Uses pointers in function Uses & to pass address Uses * to access value Changes affect original variables
Copies of values are passed Changes do NOT affect original variables Safe method Uses normal variables
Addresses are passed Changes affect original variables Uses pointers Less safe if not handled properly
A file is a collection of related data stored on secondary storage (like hard disk). It is used to store data permanently, even after the program ends.
Data is saved permanently Data is not lost when program stops Easy to transfer data between computers Large amount of data can be stored
C uses a structure called FILE (defined in stdio.h) to handle files.
We use a file pointer to access files:
FILE *fp;
fp is a pointer to a file It is used to read/write data in files
Stores data in human-readable form Extension: .txt Easy to read and edit Less secure Takes more storage space
Stores data in 0s and 1s (binary form) Not human-readable More secure Uses less storage space Faster processing
A file must be closed after use to save data properly.
Syntax
fclose(fp);
fclose() closes the file Returns 0 if success, EOF if error
fgetc() Read character
fputc() Write character
fscanf() Read formatted data
fprintf() Write formatted data
fprintf(fp, "%d", n); fscanf(fp, "%d", &n);
fread() Read binary data
fwrite() Write binary data
fwrite(address, size, count, fp); fread(address, size, count, fp);
feof() checks whether the end of file is reached.
feof(fp);
Returns non-zero ā end of file reached Returns 0 ā not end of file
Random access means we can move the file pointer to any position in a file and read/write data from that position directly, instead of reading from the beginning.
Syntax
ftell(fp);
Returns current position in file Measured in bytes from beginning
Example Idea
long pos; pos = ftell(fp);
In random access, data can be accessed directly at any position. No need to read previous data. Used in hard disks and optical disks. Faster for retrieving specific data.
Example: directly opening page number in a book.
In sequential access, data is read in order from beginning to end. To reach a record, we must pass through all previous records. Used in tape drives. Slower for large files because searching takes time. Example: reading a book page by page.
Data accessed directly Data accessed in order
Fast access Slow for large files
Used in hard disks Used in tape drives
Can jump to any location Must start from beginning
More flexible Less flexible
Example: direct search Example: reading one by one
v o i d m a i n ( ) { F I L E * f p ; c h a r c h ; f p = f o p e n ( " t e s t. t x t " , " r " ) ; w h i l e (! f e o f ( f p ) ) { c h = f g e t c ( f p ) ; p r i n t f ( " % c " , c h ) ; } f c l o s e ( f p ) ; }
#include
void main() { FILE *fp; char ch;
#include
void main() { FILE f1,f2,*f3; char ch;
f1 = fopen("file1.txt","r"); f2 = fopen("file2.txt","r"); f3 = fopen("merge.txt","w");
while(!feof(f1)) fputc(fgetc(f1),f3);
while(!feof(f2)) fputc(fgetc(f2),f3);
printf("Merged"); }
v o i d m a i n ( ) { F I L E * f p ; i n t n , l a r g e ; f p = f o p e n ( " n u m. t x t " , " r " ) ; f s c a n f ( f p , " % d " , & l a r g e ) ; w h i l e (! f e o f ( f p ) ) { f s c a n f ( f p , " % d " , & n ) ; i f ( n > l a r g e ) l a r g e = n ; }
p r i n t f ( " L a r g e s t = % d " , l a r g e ) ; }
v o i d m a i n ( ) { F I L E * f 1 , * f 2 ; c h a r w [ 2 0 ] ;
f 1 = f o p e n ( " a. t x t " , " r " ) ; f 2 = f o p e n ( " b. t x t " , " w " ) ;
w h i l e ( f s c a n f ( f 1 , " % s " , w )! = E O F ) { i f ( s t r c m p ( w , " i s " ) = = 0 ) f p r i n t f ( f 2 , " w a s " ) ; e l s e f p r i n t f ( f 2 , " % s " , w ) ; }
p r i n t f ( " D o n e " ) ; }
#include #include
void main() { FILE f1,f2; char w[30]; int i;
f1 = fopen("a.txt","r"); f2 = fopen("b.txt","w");
while(fscanf(f1,"%s",w)!=EOF) { for(i=strlen(w)-1;i>=0;i--)