Docsity

MODULE 3 C PROGRAMMING (2024 SCHEME KTU), Summaries of C programming

Toggle sidebar
bg1
POINTERS IN C
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.
Example
int n = 10;
int *p = &n;
n = normal variable
p = pointer variable
&n = address of n
*p = value at address (value of n)
Declaration of Pointer
datatype *pointer_name;
Examples:
int *p; // pointer to integer
char *c; // pointer to character
Initialization of Pointer
Assigning address to a pointer:
int a;
int *p;
p = &a;
Accessing Value using Pointer
*p // gives value stored at address
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download MODULE 3 C PROGRAMMING (2024 SCHEME KTU) and more Summaries C programming in PDF only on Docsity!

POINTERS IN C

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.

Example

int n = 10; int *p = &n;

n = normal variable p = pointer variable &n = address of n *p = value at address (value of n)

Declaration of Pointer

datatype *pointer_name;

Examples:

int *p; // pointer to integer char *c; // pointer to character

Initialization of Pointer

Assigning address to a pointer:

int a; int *p;

p = &a;

Accessing Value using Pointer

*p // gives value stored at address

Example Program

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

NULL Pointer

A NULL pointer is a pointer that does not point to any memory location.

Example

int *p = NULL;

Key Points

Pointer stores address, not value & gives address

  • gives value at address NULL pointer points to nothing Very useful for memory management

Example:

int pointer increases by 2 or 4 bytes (depends on system)

p++; p--;

6. Subtraction of Two Pointers

Two pointers of the same array can be subtracted to get distance between them.

int diff = p2 - p1;

7. Comparison of Pointers

Two pointers can be compared if they point to same data type.

if(p1 == p2)

8. Comparison with NULL

A pointer can be checked whether it is NULL or not.

if(p == NULL)

Sum of two numbers using pointers

i n c l u d e < s t d i o. h >

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

Swap two variables using pointers

i n c l u d e < s t d i o. h >

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 ;

  • a = * b ;
  • b = t e m p ;

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

1. Read and Display Array using Pointer

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

2. Sort Array using Pointer (Simple)

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

POINTER TO POINTER

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.

Syntax

datatype **ptr;

Example Idea

a → normal variable p → pointer to a q → pointer to pointer p

Program Example

i n c l u d e < s t d i o. h >

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

Output

Value of a = 10 Value of *p = 10 Value of **q = 10

Simple Explanation

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

PASS BY VALUE VS PASS BY

REFERENCE

PASS BY VALUE

Copies of values are passed Changes do NOT affect original variables Safe method Uses normal variables

PASS BY REFERENCE

Addresses are passed Changes affect original variables Uses pointers Less safe if not handled properly

FILES IN C

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.

Why Files are Used

Data is saved permanently Data is not lost when program stops Easy to transfer data between computers Large amount of data can be stored

File Handling in C

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

Types of Files

1. Text Files

Stores data in human-readable form Extension: .txt Easy to read and edit Less secure Takes more storage space

2. Binary Files

Stores data in 0s and 1s (binary form) Not human-readable More secure Uses less storage space Faster processing

2. CLOSING A FILE

A file must be closed after use to save data properly.

Syntax

fclose(fp);

Explanation

fclose() closes the file Returns 0 if success, EOF if error

3. READING AND WRITING FILES

Character Functions

Function Use

fgetc() Read character

fputc() Write character

String / Data Functions

Function Use

fscanf() Read formatted data

fprintf() Write formatted data

Example

fprintf(fp, "%d", n); fscanf(fp, "%d", &n);

4. BINARY FILE FUNCTIONS

Function Use

fread() Read binary data

fwrite() Write binary data

Syntax

fwrite(address, size, count, fp); fread(address, size, count, fp);

5. EOF FUNCTION

feof() checks whether the end of file is reached.

Syntax

feof(fp);

Returns non-zero → end of file reached Returns 0 → not end of file

RANDOM ACCESS TO A 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.

1. rewind()

Syntax

ftell(fp);

Explanation

Returns current position in file Measured in bytes from beginning

Example Idea

long pos; pos = ftell(fp);

SEQUENTIAL AND RANDOM

ACCESS

RANDOM ACCESS

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.

SEQUENTIAL ACCESS

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.

RANDOM ACCESS SEQUENTIAL ACCESS

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

1. DISPLAY FILE CONTENT

i n c l u d e < s t d i o. h >

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

2. COUNT VOWELS

#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"); }

5. FIND LARGEST NUMBER

i n c l u d e < s t d i o. h >

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

6. REPLACE WORD (is → was)

i n c l u d e < s t d i o. h >

i n c l u d e < s t r i n g. h >

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

7. REVERSE WORDS

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