Pointers - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Pointers

Description:

Pointers Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type like char, int, float, double ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 25
Provided by: Adm9443
Category:
Tags: pointer | pointers

less

Transcript and Presenter's Notes

Title: Pointers


1
  • Pointers
  • Pointer is a user defined data type which
    creates special types of variables which can hold
    the address of primitive data type like char,
    int, float, double or user defined data type like
    function, pointer etc. or derived data type like
    array, structure, union, enum.
  • Examples int ptr
  • int (ptr)()
  • int (ptr)2
  • In c programming every variable keeps two type of
    value.
  • 1. Contain of variable or value of variable.
  • 2. Address of variable where it has stored in the
    memory.
  • (1) Meaning of following simple pointer
    declaration and definition
  • int a5
  • int ptr
  • ptra

2
  • Explanation
  • About variable a
  • 1. Name of variable a
  • 2. Value of variable which it keeps 5
  • 3. Address where it has stored in memory 1025
    (assume)
  • About variable ptr4. Name of variable ptr
  • 5. Value of variable which it keeps 1025
  • 6. Address where it has stored in memory 5000
    (assume)
  • Note A variable where it will be stored in
    memory is decided by operating system. We cannot
    guess at which location a particular variable
    will be stored in memory.

3
  • Pictorial representation

4
  • Example
  • includeltstdio.hgt
  • includeltconio.hgt
  • void main()
  •     int i9
  •     clrscr()
  •   
  •     printf("Value of i d\n",i)
  •     printf("Address of i u\n",i)
  •     printf("Value at address of i d",(i))
  •     getch()
  • Output
  • The values of i9
  • Address of i65524
  • Value at address of i9

5
  • Passing pointers to function
  • C provides a special feature of  pointer to a
    function. Every function defined in C language
    have a base address attached to it. This base
    address acts as an entering point into that
    function.
  • This address can be stored in a pointer known as
    function pointer. The pointer to a function is
    the declaration of pointer that holds the base
    address of the function.
  • Syntax return_type ( pointer_name) (
    variable1_type variable1_name  , variable2_type
    variable2_name , variable3_type variable3_name
    .................)  

6
  • Example
  • int (function_pointer) (int,int,int) 
  • Program Coding
  • include ltstdio.hgt
  • include ltconio.hgt
  • int mul(int a, int b, int c) 
  •   return abc
  • void main() 
  •   int (function_pointer)(int, int, int)  func
    tion_pointer  mul  printf("The product of three
     numbers isd",  function_pointer(2, 3, 4))  g
    etch()
  • Output
  • The product of three numbers is24

7
  • Operations in Pointers
  • Eight basic operations that can be performed
    with pointer variables. In addition to these
    operations, you can use the relational operators
    to compare pointers.
  • pointer reference by an arithmetic operation. For
    example
  • int x 5, ip x
  • ip
  • Array of Pointers
  • A pointer is a variable that contains the
    memory location of another variable. The values
    you assign to the pointers are memory addresses
    of other variables (or other pointers). A running
    program gets a certain space in the main memory.
  • Syntax of declaring a pointer  data_type_name
    variable name

8
  • Pointer Operators- and
  • C provides two special operators known as
    pointer operators. They are and .They are
    and . stands for Address of and it is used to
    retrieve the address of a variable. stands for
    value at address and it is used to access the
    value at a location by means of its address.
  • Syntax
  • data-type variable-name
  • Example
  • int ip
  • where,ip is declared to be a pointer variable of
    int type
  • Example
  • includeltstdio.hgt
  • includeltconio.hgt
  • void main()
  • int i10,ip

9
  • float f3.4 fp
  • char ca cp
  • clrscr()
  • printf(id \n,i)
  • printf(ff \n,f)
  • printf(cc \n,c)
  • ipi
  • printf(\n Address of iu \n,ip)
  • printf(Value of id \n,ip)
  • fpf
  • printf(\n Address of fu \n,fp)
  • printf(Value of ff \n,fp)
  • cpc
  • printf(\n Address of cu \n,cp)
  • printf(Value of cc \n,cp)
  • getch()

10
  • Pointers and Arrays
  • Elements of an array are accessed through
    pointers internally.
  • 1.Pointers and One-Dimensional Arrays
  • Consider one-dimensional arrays and pointers.
    Suppose a is a one-dimensional array of int type
    and of size 10.
  • int a10
  • Since a is the address of a0,a is the
    element stored in the first location.(a1) gives
    the address of the second element a1 of the
    array.(a1) gives the element itself stored at
    a1.Similary,(a2) is the address of a2 and
    a(a2) is the value at a2.
  • For Ex
  • a(a0)a0
  • (a1)a1
  • (a2)a2
  • (a3)a3
  • .
  • (ai)ai

11
  • includeltstdio.hgt
  • void main()
  • int a10,n,I
  • clrscr()
  • printf(Enter no. of elements \n)
  • scanf(d,n)
  • printf(Enter d elements \n,n)
  • for(i0iltni)
  • scanf(d,ai)
  • printf(The list if elements \n)
  • for(i0iltni)
  • printf(d \n,(ai))
  • getch()
  • Enter no. of elements
  • 5
  • Enter 5 elements
  • 1 2 3 4 5
  • The list of items
  • 1 2 3 4 5

12
  • Pointers and Two-dimensional Arrays
  • a is an array of two dimensions of int
    type,which is declared as
  • int a23
  • The array name a gives its base address,i.e.,
    the address of its first element.
  • For example,
  • ((a1)0) gives the first element in the
    1st array a00
  • ((a1)1) gives the first element in the
    1st array a01
  • ((a1)2) gives the first element in the
    1st array a02
  • In general,aij can be written as ((ai)j)

13
  • Example
  • includeltstdio.hgt
  • includeltconio.hgt
  • void main()
  • int a33,m,n,I,j
  • clrscr()
  • printf(Enter the order of the matrix a \n)
  • scanf(dd,m,n)
  • printf(Enter elements of the matrix a of
    order d d \n,m,n)
  • for(i0iltmi)
  • for(j0jltnj)
  • scanf(d,(ai)j)
  • printf(Matrix a \n)
  • for(i0iltmi)
  • for(j0jltnj)
  • printf(4d,(a(a1)j))
  • printf(\n)
  • getch()
  • Output
  • Enter the order of the matrix a
  • 2
  • Enter elements of the matrix a of order 22
  • 2 3 4
  • Matrix a
  • 1 2
  • 3 4

14
  • Arrays of Pointers
  • Specify the data type of data stored in the
    location, which is to identified by the pointer.
    The asterisk tells the compiler that you are
    creating a pointer variable. Then specify the
    name of variable.
  • we have created an array of pointers of maximum
    size 3. Then we have assigned the objects of
    array pointer.
  • The address of the variable. array0
    xarray1 yarray2 z Here is the
    code

15
  • Example
  • void main()
  •    clrscr()  int array3  int x  10, y  
    20, z  30  int i  array0  x  array1 
     y  array2  z  for (i0 ilt 3 i)   p
    rintf("The value of d d ,address is u\t \n", i
    , (arrayi),  arrayi)    getch() 

16
  • Pointers and Structure
  • The concept of pointers can be used in two
    ways
  • 1.Address of a structure variable can be
    retrieved using operator.
  • 2.We use operator to retrieve the address of a
    structure variable.
  • Example
  • struct temp
  • int i
  • float f
  • char c
  • struct temp t,tp
  • Where,
  • t is declared to be variable of struct temp
    type.tp is declared to be a pointer to struct
    temp type.

17
  • Example
  • includeltstdio.hgt
  • includeltconio.hgt
  • struct temp
  • int i
  • float f
  • char c
  • void main()
  • struct temp t123,34.56,p,tp
  • clrscr()
  • tpt
  • printf(t.id \n,tp-gti)
  • printf(t.ff \n,tp-gtf)
  • printf(t.cc \n,tp-gtc)
  • getch()

18
  • Structures containing Pointers
  • struct temp
  • int i
  • int ip
  • The structure definition includes ip,a pointer
    to int type as one of its members.
  • Example
  • includeltstdio.hgt
  • includeltconio..hgt
  • Struct temp
  • int i
  • int ip
  • void main()
  • struct temp t
  • int a10
  • clrscr()
  • t.ia
  • t.ipa
  • printf(Value of ad,t.i)
  • printf(Address of au \n,t.ip)
  • getch()

19
  • Files
  • Files can be classified into many types depending
    upon the contents of the file.
  • Key terms used in file processing
  • FieldA field is an elementary data item that is
    used to store a single unit of data.
  • Examplename
  • Record A group of related fields constitute a
    record.
  • Examplerollnumber,name,mark1,etc.
  • FileA group of related records form a
    file.Generally,this type of file is called a data
    file.
  • ExampleEmployee data file
  • StreamInput and output operations are performed
    through streams. In C files are streams of
    characters.The stream of characters are decoded
    by various Input and output functions like
    scanf(),getc(),etc.

20
  • Types of streams
  • There are two types of streamstext and
    binary
  • text and binaryA text stream consists of ASCII
    characters. A binary stream consists of any type
    of data. The major difference between two stream
    permits character translation while transporting
    data.
  • Opening and/or Saving Files
  • To create a new file, open an existing file, or
    save a file, you use the fopen() function
  • Syntax
  • FILE fopen(const char FileName, const char
    Mode)
  • Where,
  • The fopen() function requires two arguments.
    The first argument filename refers to the name
    of he file to be opened. The second argument
    mode refers to the file mode, which specifies
    the purpose for which the file is opened.

21
  • Example
  • fpopen(salary.txt,w)
  • if(fpNULL)
  • printf(\n Unable to open file.. Quitting
    process..\n)
  • getch()
  • exit(0)

22
  • The following table illustrates the different
    modes of file operation

File Type Meaning
r Opens an existing file for reading.
w Opens a new file for writing.
a Opens an existing file for appending.
rb Opens a binary file for reading.
wb Creates and opens a new binary file for writing.
ab Opes a binary file for appending
r Opens fn existing file for reading and writing
w Creates and opens a new file for reading and writing.
a Open an existing file for reading and writing in the append mode.
rb or rb Opens a binary file for read and write operations
wb or wb Creates and opens a binary file for read and write operations
ab or ab Opens a binary for for read and write operations.
23
  • For example, consider the statements
  • FILE fp1,fp2
  • ..
  • ..
  • fp1fopen(emp.dat,r)
  • fp2fopen(salary.txt,w)
  • Closing a file
  • A file is closed when all the input/output
    operations are completed on it. The fclose is
    used to close an opened file.
  • Syntax
  • int fclose(FILE fp)
  • Where,
  • The file pointer fp is closed.

24
  • Operations on Files
  • The following are various functions that are used
    on files.
  • 1.putc() function This function is similar to
    the putchar().
  • Syntax
  • int putc(int ch,FILE fp)
  • 2.getc() functionThis function is similar to the
    getchar() function.
  • Syntax
  • int getc(int ch,FILE fp)
  • 3.putw() functionThis function is used to write
    an integer value onto the file pointed by the
    pointer.
  • Syntax
  • int putc(int w,FILE fp)
  • 4.fprintf() functionThis function is similar to
    the scanf() function.
  • Syntax
  • int fprintf(FILE fp,format specifications,list
    of variables)
  • 5.fscanf() functionThis function is similar to
    the scanf() function.
  • Syntax
  • int fscanf(FILE fp,format specifications,list
    of variables)
  • 6.fread() functionThis function reads data from
    stream file or file of any data type using binary
    representation..
  • Syntax
Write a Comment
User Comments (0)
About PowerShow.com