Chapter 11

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

NINT

Pointers With Arrays

The address of an array element can be expressed in two


ways :
By writing the actual array element preceded by the
ambersand (&) sign.
By writing an expression in which the subscript is added to
the array name.

NINT
/* Array Using Pointers */
#include<stdio.h>
#include<conio.h>
void main()
{
int b[100],*iptr;
iptr=&b;
clrscr();
printf("The initial value of iptr is %u\n",iptr);
iptr++;
printf("Incremented value of iptr is %u\n",iptr);
getch();
}
NINT
POINTERS IN ARRAYS Example 2
#include<stdio.h>
#include<conio.h>
void main()
{ 10
int arr[]={10,20,30,40};
int *ptr,i; 20
clrscr();
ptr=arr; // same as &arr[0]; 30
printf("\n The Result of the array is ");
for(i=0;i<4;i++) 40
{
printf("%d\n",*ptr);
ptr++;
}
getch();
}

NINT
Pointers as Function Arguments
When pointers are passed to a function :

The address of the data item is passed and thus the function
can freely access the contents of that address from within the
function

In this way, function arguments permit data-items to be


altered in the calling routine and the function.

When the arguments are pointers or arrays, a call by


reference is made to the function as opposed to a call by value
for the variable arguments.
NINT
FUNCTION POINTER EXAMPLE /* Invoke the func. Addition */
#include<stdio.h> val=(function) (20,100);
#include<conio.h>
printf("\n Addition result =
int (* function) (int,int); /*function %d",val);
pointer prototype */
/* assign the function subtraction into
int addition(int a,int b) the function pointer */
{ function=subtraction;
return a+b;
} /* invoke the func. subtraction &
syntax for function pointer call */
int subtraction(int a,int b)
{ val=(function) (200,100); printf("\
return a-b; nSubtraction result =%d",val);
} getch();
void main() }
{
int val;
/* assign the func. addition into the
function pointer */ NINT
function=addition;
Pointers To Structures

Pointers to structures are allowed in C, but there are


some special aspects to structure pointers that a user of
pointers to structures must be aware of.
The following statement declares ptr as a pointer to data
of that type -

NINT
How the structure can be ptr=&st;
accessed by a pointer variable printf("\n display the details using
#include<stdio.h> structure variables");
#include<stdlib.h>
struct student printf( "%d %s %f", st.roll_no,
{ st.name, st.marks);
int roll_no;
char name[20]; printf("\n display the details using
float marks; pointer variables");
}st; printf( "%d %s %f",ptr->roll_no,
void main() ptr->name, ptr->marks);
{ }
struct student *ptr;
printf("\n \t Enter the record"); void print_rec(int r,char n[ ],float m)
printf("\n Enter the Roll Number"); {
scanf("%d",&st.roll_no); printf("\n You have Entered Following
record");
printf("\n Enter the Name"); printf("\n %d %s %f",r,n,m);
scanf("%s",st.name); }

printf("\n Enter the Marks"); NINT

scanf("%f",&st.marks);
Pointers & Strings
Using strings as pointers can be explained with the help of the

NINT

You might also like