Chapter 5 Pointers and Arrays

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

The C Programming Language

Chapter 5 Pointers and Arrays

Chapter 5 Pointers and


Arrays
The size of an array must be given when
define it. (fixed-sized array)
Using a pointer can create dynamic space.
Pointer
What is a pointer
Address and pointer

Do you have any Yes, the double room


number is: 201,322,509,
rooms available? which one do you want?
Address and pointer
Variables and pointers
Variables and pointers

p i

*
p
&i 5
Pointer variable
Pointer variable

grammar 类 Type description * variable name

int *p
Pointer variable

General & Variable name;


form
Two
methods of Define pointer int a;
variables and assign
assignment them at the same int *p=&a;
time
of pointer
variable Note the difference between
the two assignment
Define pointer int a; statements. If you assign
variables before
assigning values
int *p; after defining the pointer
variable, do not add "*".
p=&a;
指针变量的引用

grammar * Pointer
variable

例示 *p
& and * operator

e.g. p=&i;

e.g. q=*p;
e.g. int a;
p=&a;

The
difference
between
“&*" and
"* &;"

What?
The following two statements are used to analyze the
difference between “&*" and "* &;". The operators
of "&;" and "*" have the same priority and are
combined from right to left. So "& * P" performs "*"
operation first, and "* P" is equivalent to variable a;
then "&;" operation, and "& * P" is equivalent to
taking the address of variable a. "* & a" performs
"&" operation first, and "&a" is to take the address of
variable a, and then "*" operation is to take the value
of the address of variable a, which is actually variable
a
指针的自增自减
指针自加自减运算

什么情况
指针自加自减运算
一维数组与指针
一维数组与指针

int *p,a[10];
例如 p=a;

int *p,a[10];
例如 p=&a[0];
一维数组与指针
一维数组与指针
Chapter 5 Pointers and Arrays
5.1 Pointers and Addresses

5.1 Pointers and Addresses

A pointer is a variable that contains the


address of a variable.
 Pointers and arrays are closely
related.
Pointers easily create problems. This
is certainly true when they are used
carelessly.
p: c:
… … …
Chapter 5 Pointers and Arrays
5.1 Pointers and Addresses

 Declaration of a point:
type *namep;
 Operate (two unary operators)
&: give the address of an object:
* (indirection or dereferencing operator): access
object
the  A the pointer is constrained
pointer points to. to point to a
particular kind of object (variables, and array
Example:
elements). It cannot be applied to expressions,
int x=1,y=2,z[10];
constants, or register variables. A pointer to
intis
void *ip; /*ip isany
used to hold a poiter
type to
of int */
pointer.
ip=&x; /*ip now points to x */
y=*ip; /* y is now 1 */
*ip=0; /* x is now 0 */
ip=&z[0]; /*ip now points to z[0] */
Chapter 5 Pointers and Arrays
5.1 Pointers and Addresses

int x=20, y=1, z=155;


int *p;
p=&x;
memory
address unit variable address memory unit variable

1000 20 x 1000 20 x
1002 1 y 1002 1 y
1004 155 z 1004 155 z

2000 2000 1000 p


Chapter 5 Pointers and Arrays
5.1 Pointers and Addresses
PRECEDENCE AND ASSOCIATIVITY OF OPERATORS
OPERATORS ASSOCIATIVITY
() [ ] -> . left to right
! ~ ++ - - + -  & (type) sizeof (unary Op.) right to left
 / % left to right
+ - left to right
<< >> left to right
< <= > >= left to right
== != left to right
& (Bitwise AND) left to right
^ (Bitwise exclusive OR) left to right
| (Bitwise inclusive OR) left to right
&& left to right
|| left to right
?: right to left
= += -= *= /= %= &= ^= |= <<= >>= right to left
, left to right
Chapter 5 Pointers and Arrays
5.1 Pointers and Addresses

Example:
 what are the values
that the variables contain?
int x=1, y=2; #include<stdio.h>
int main()
int *ip, *iq; {
ip=&x, iq=&y; int x=1, y=2;
int *ip, *iq;
*ip=*ip+10; ip=&x, iq=&y;
*ip=*ip+10;
y=*ip+1;
y=*ip+1;
++*ip; ++*ip;
(*ip)++;
(*ip)++; //*ip++;
*ip++; iq=ip;
printf("x=%d, y=%d\n", x,y);
iq=ip; printf("*ip=%d, *iq=%d", *ip, *iq);
return 0;
}
Chapter 5 Pointers and Arrays
5.2 Pointers and Function Arguments

5.2 Pointers and Function


Arguments
#include <stdio.h>
void swap(int *x,
x, int
inty);
*y); void swap(int *x,
x, int
inty)
*y)
main() {
{ int temp;
int a=10, b=200; temp = *x;x;
swap(&a,&b);
swap(a,b); x ==y;*y;
*x
printf(“a=%d\tb=%d\n", a,b); y ==temp;
*y temp;
} }

a:
x: 
b:
in caller:
y: 

in swap:
Chapter 5 Pointers and Arrays
5.2 Pointers and Function Arguments

#include <ctype.h>
int getch(void);
void ungetch(int);
int getint(int *pn)
{ int c, sign;
while(isspace(c=getch())) /*skip white space */
;
if( !isdigit(c) && c!= EOF && c!=‘+’ && c!=‘-’) {
ungetch(c); /* it’s not number */
return 0;
}
sign = (c == ‘-’) ? -1:1;
if( c== ‘+’ || c==‘-’) c=getch();
for(*pn =0; isdigit( c); c=getch())
*pn=10* *pn +(c-’0’);
*pn *=sign;
if(c != EOF) ungetch(c);
return c;
}
Chapter 5 Pointers and Arrays
5.3 Pointers and Arrays

5.3 Pointers and Arrays


 Any operation that can be achieved by array
subscripting can also be done with pointers.
Example:
int a[10];

a:
a[0 a[1 a[2 a[9
pa:
] ] ] ]

int *pa;
pa=&a[0];
Chapter 5 Pointers and Arrays
5.3 Pointers and Arrays

int a[10]={10,9,8,7,6,5,4,3,2,1};
int *pa, x;
pa=&a[0]; /* or pa=a; */
x=*pa;
x=*(pa+2);
 the name of an array is a
pa=&a[8]; synonym for the location of the initial
x=*pa; element, the assignment pa=&a[8] can
also be written as pa=a+8. 
a: 10 9 8 7 6 5 4 3 2 1
a[0 a[1 a[2 a[9
pa:pa:  
] ] ] ]
 A pointer is a variable.1028 pa=a and
x
:

pa++ are legal. An array name is not a


variable. a=pa and a++ are illegal . 
Chapter 5 Pointers and Arrays
5.3 Pointers and Arrays

Equivalent form:
int strlen(char *s) int strlen(char s[ ])
{
int n;
for(n=0;*s!=‘\0’;s++)
n++;
return n;
}
strlen(“hello, world”); /* string constant */
strlen(array); /* char array[100]; */
strlen(ptr); /* char *ptr; */

Advantage:
can avoid large array’s pass.
Chapter 5 Pointers and Arrays
5.3 Pointers and Arrays

#include<stdio.h>
int strlen( char * );
int main( )
{
printf( "%d\n",strlen("hello, world") );
char array[100] = "Hello, World!!!";
printf("%d\n",strlen(array));
char * ptr = array;
printf( "%d\n", strlen(ptr) );
return 0;
}
int strlen( char *s )
{ int n;
for( n = 0; *s != '\0'; s++)
n++;
return n;
}
Chapter 5 Pointers and Arrays
5.4 Address Arithmetic

5.4 Address Arithmetic


If p is a pointer to some element of an array
 Pointer adds a number or subtracts a number
p++; /* increments p to point to the next element */
p--; /* decrements p to point to the previous element */
p += i; /* increment it to point i element beyond it points now */
 Subtraction between pointers
int strlen(char *s)
{ char *p = s;
while(*p != ‘\0’)
p++;
return p-s;
}
 Comparison between pointers
If p and q point to members of the same array, then relations
like ==, !=, <, >, >=, etc. work properly.
p < q is true if p points an earlier member of the array than q
does.
Chapter 5 Pointers and Arrays
5.4 Address Arithmetic

 The storage managed by alloc and afree is a


Example:
stack. The standard library provides analogous
about malloc
functions, a rudimentary storage
and free, allocator.
that have no suchThere are
two rountines.
restrictions.
 alloc(n) returns a pointer to n consecutive
character positions.
 afree(p) release the storage thus acquired so it
can be re_used later.

before call to alloc: allocp:

allocbuf:

in use free

after call to alloc: allocp:

allocbuf:
in use free
Chapter 5 Pointers and Arrays
5.4 Address Arithmetic

#define ALLOSIZE 10000 /* size of available space */


static char allocbuf[ALLOCSIZE]; /* storage for alloc */
static char *allocp = allocbuf; /* next free position */
char *alloc(int n) /* return pointer to n characters */
{
if(allocbuf + ALLOSIZE – allocp >= n) { /* it fits */
allocp += n;
return allocp – n; /* old p */
}
else return 0; /* not enough room */
}

void afree(char *p) /* free storage pointed to by p */


{
if( p >= allocbuf && p < allocbuf + ALLOCSIZE)
allocp = p;
}
Chapter 5 Pointers and Arrays
5.4 Address Arithmetic

#include<stdio.h>
#include<stdlib.h>  void *malloc(unsigned size)
int main() if((p=(int *)malloc(n*sizeof(int))){…
{ The main difference between malloc and
int n, sum, i, *p; calloc is that calloc will initialize the n
printf("Enter n:");
acquired memory units.
scanf("%d",&n);
if((p=(int *)calloc(n,sizeof(int))) == NULL){
printf("Not able to allocate memory.\n");
exit(1);
}
printf("Enter %d integers:", n);
for(i=0; i<n; i++)
scanf("%d",p+i);
sum=0;
for(i=0; i<n; i++)
sum=sum + *(p+i);
printf("The sum is %d \n",sum);
free(p);
return 0;
}
Chapter 5 Pointers and Arrays
5.4 Address Arithmetic


The behavior is undefined for arithmetic or
comparisons with pointer that do not point to
members of the same array .

It is not legal to add two pointers, or to multiply


or divide or shift or mask them, or to add float
or double to them, or even, except for void *, to
assign a pointer of one type to a pointer of
another type without a cast.
Chapter 5 Pointers and Arrays
5.5 Character Pointers and Functions

5.5 Character Pointers and


Functions
 A string constant is accessed by a pointer to its
first element.
printf( “hello, world\n” );
/* printf received a pointer which point to “hello, world\n” */
char *pmessage;
pmessage = “now is the time”;
/*pmessage is assigned by a pointer to the character array */
 Difference between character array and character
pointer.
char amessage[] = “now is the time”;
char *pmessage = “now is the time”;
pmessage:  now is the time\0
amessage: now is the time\0
Chapter 5 Pointers and Arrays
5.5 Character Pointers and Functions

Example 1: strcpy(s,t) copies the string t tocan it string


the performs.
the strcpy(s,t) using
/* strcpy: array subscript version */ statement s=t?
void strcpy(char *s, char *t)
{ /* strcpy: pointer version 1 */
int i; void strcpy(char *s, char *t)
i=0; {
while((s[i]=t[i]) != ‘\0’) while((*s = *t) != ‘\0’) {
i++; s++; i++;
} }
}
/* strcpy: pointer version 2 */
void strcpy(char *s, char *t) /* strcpy: pointer version 3 */
{ void strcpy(char *s, char *t)
while((*s++ = *t++) != ‘\0’) {
; while(*s++ = *t++)
} ;
}
Chapter 5 Pointers and Arrays
5.5 Character Pointers and Functions

Example 2: strcmp(s,t) compares the strings s and t.


/* strcmp: return <0 if s<t, 0 if s==t, >0 if s>t */
int strcmp ( char *s, char *t )
{
int i;
for( i=0; s[i] == t[i]; i++ )
if( s[i] == ‘\0’ )
return 0;
return s[i] - t[i];
}
/* strcmp: return <0 if s<t, 0 if s==t, >0 if s>t */
void strcmp ( char *s, char *t )
{
for( ; *s == *t; s++, t++ )
if( *s == ‘\0’ )
return 0;
return *s - *t;
}
Chapter 5 Pointers and Arrays
5.6 Pointer Arrays: Pointer to Pointer

5.6 Pointer Arrays: Pointer to


Pointer
 Pointer array is an array which stores pointers.

 
 defghi  defghi

 jklmnopqrst  jklmnopqrst
abc abc

Example: There are five balls in an nontransparent bag.


Each has its own color (red, blue, yellow, green, purple),
but they have the same size. What colors balls may be
caught when two bales are caught each time?
Chapter 5 Pointers and Arrays
5.6 Pointer Arrays: Pointer to Pointer

# include<stdio.h>
int main(void)
{
char *color[5]={"red", "blue", "yellow", "green",
"purple"};
int count=0, i, j;

for(i=0; i<=4; i++)


for(j=i+1; j<=4; j++){
if(i == j) continue;
count++;
printf("%6d", count);
printf("%10s %10s\n", color[i],
color[j]);
}
return 0;
}
Chapter 5 Pointers and Arrays
5.6 Pointer Arrays: Pointer to Pointer

char *temp;
temp = color[0];
color[0] = color[4];
color
color[4]=temp;
color[0] red\0
color 
color[1] blue\0
color[2] yellow\0 color[0] red\0

color[3] green\0 color[1] blue\0

color[4] purple\0 color[2] yellow\0


color[3] green\0
color[4] purple\0

 Algorithm:
read all the lines of input
sort them
print them in order
Quicksort -- the fastest known sorting algorithm in practice
The Algorithm
void Quicksort ( ElementType A[ ], int N )
{
if ( N < 2 ) return;

pivot = pick any element in A[ ];

Partition S = { A[ ] \ pivot } into two disjoint sets:
A1={ aS | a  pivot } and A2={ aS | a  pivot };
A = Quicksort ( A1, N1)  { pivot }  Quicksort ( A2, N2);
}

13 43
81
31 57 26 65
13 81 92 75
0
92 43 65
31 57 26
0 13 26 31 43 57 65 75 81 92
75 0

0 13 26 31 43 57 65 75 81 92

1/15
Chapter 5 Pointers and Arrays
5.6 Pointer Arrays: Pointer to Pointer

# include<stdio.h>
#include <string.h>
#include<stdlib.h>
#define MAXLINES 5000
char *lineptr[MAXLINES];
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void qsort(char *lineptr[], int left, int right);
/* sort input lines */
main()
{ int nlines; /* number of input lines read */
if((nlines=readlines(lineptr,MAXLINES))>=0) {
qsort(lineptr,0,nlines-1);
writelines(lineptr,nlines);
return 0;
}
else { printf("error: input too big to sort\n");
return 1; }
}
Chapter 5 Pointers and Arrays
5.6 Pointer Arrays: Pointer to Pointer

#define MAXLINE 1000 /* max length of any input line */


int getline(char *, int);
/* readlines: read input lines */
int readlines(char *lineptr[],int maxlines)
{
int len, nlines;
char *p, line[MAXLINE];
nlines=0;
while((len = getline(line, MAXLINE)) >0)
if(nlines >= maxlines || (p=(char*)malloc(len*sizeof(char)))
==NULL)
return -1;
else {
line[len-1]='\0'; /*delete newline */
strcpy(p, line);
lineptr[nlines++]=p;
}
return nlines;
}
Chapter 5 Pointers and Arrays
5.6 Pointer Arrays: Pointer to Pointer

void writelines(char *lineptr[], int nlines)


{
int i;
for ( i=0; i<nlines; i++)
printf("%s\n",lineptr[i]);
}

Since lineptr is itself the name of an array, it can


be treated as a pointer and writelines can be
written instead as

void writelines(char *lineptr[], int nlines)


{
while(nlines-- >0)
printf("%s\n",*lineptr++);
}
Chapter 5 Pointers and Arrays
5.6 Pointer Arrays: Pointer to Pointer

/* qsort: sort v[left]…v[right] into increasing order */


void qsort(char *v[ ], int left, int right)
{
int i, last;
void swap(char *v[], int i, int j);
if(left >= right) return; /* do thing if array contains */
/* fewer than two elements */
swap(v, left, (left+right)/2);
last=left;
for(i=left+1; i<=right;i++)
if(strcmp(v[i],v[left]) <0)
swap(v, ++last, i);
swap(v,left,last);
qsort(v, left, last-1);
qsort(v, last+1, right);
}
Chapter 5 Pointers and Arrays
5.6 Pointer Arrays: Pointer to Pointer

int getline(char s[ ],int lim)


{
int c, i;
for(i=0; i<lim-1&&(c=getchar())!=EOF && c!='\n';++i)
s[i]=c;
s[i]='\0';
return i;
}

void swap(char *v[ ], int i, int j) /* interchange v[i] and v[j] */


{
char *temp;
temp=v[i];
v[i]=v[j];
v[j]=temp;
}
Chapter 5 Pointers and Arrays
5.7 Multi-Dimensional Arrays

5.7 Multi-Dimensional Arrays


/* daytab is a 2-demensional array */ The legitimate
static char daytab[2][13]= { use of char for storing
small non-character
{0,31,28,31,30,31,30,31,31,30,31,30,31},
integers. 
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};  subscript and initializer
daytab[i][j] 0 1 2 3 4 5 6 7 8 9 10 11 12
int day_of_year(int
0 0 31 28 year,31 30int31
month,
30 31int 31
day)
30 31 30 31
{ /* day_of_year:
1 0 31 set 29 day
31 of
30 year
31 from month
30 31 31 &30 day
31 */30 31
int i, leap;
leap= year%4 ==0 && year%100!= 0|| year%400==0;
for (i=1;i<month; i++)
 The logical
day += daytab[leap][i];
return day; expression for leap is
} either zero or one. 
Chapter 5 Pointers and Arrays
5.7 Multi-Dimensional Arrays

/* month_day: set month, day from day of year */

void month_day(int year, int yearday, int *pmonth, int *pday)


{
int i, leap;
leap=year%4 ==0 && year%100 != 0 || year%400 ==0 ;
for(i=1; yearday > daytab[leap][i]; i++)
yearday -= daytab[eap][i];
*pmonth = i;
*pday = yearday; month_day(1988,60,&m,&d)
} set m to 2 and d to 29

 Why both of moth_day and day_of_year can


can use the array daytable? external
Chapter 5 Pointers and Arrays
5.7 Multi-Dimensional Arrays

2-demensional array’s storage in memory

int a[3][2]={1,2,3,4,5,6}; 32 matrix


a[0][0] a[0][1]
3 rows, 2 column,6 a[1][0] a[1][1]
elements a[2][0] a[2][1]

A[0][0] 1
Elements in a tow- A[0][1] 2
dimensional array are
A[1][0] 3
stored by rows/columns
A[1][1] 4
A[2][0] 5
A[2][1] 6
Chapter 5 Pointers and Arrays
5.7 Multi-Dimensional Arrays

Example: 2-demensional array’s transpose


1 2 3 1 4 7
4 5 6 2 5 8
7 8 9 3 6 9
for(i=0; i<n; i++)
#include<stdio.h> for(j=0; j<n; j++)
int main() if(i<j){
{ temp=a[i][j];
int i,j,n,temp; a[i][j]=a[j][i];
int a[6][6]; a[j][i]=temp;
}
printf("Enter n:"); for(i=0; i<n; i++){
scanf("%d", &n); for(j=0; j<n; j++)
for(i=0; i<n; i++) printf("%4d",a[i][j]);
for(j=0; j<n; j++) printf("\n");
a[i][j]=i*n+j+1; }
return 0;
}
Chapter 5 Pointers and Arrays
5.8 Initialization of Pointer Arrays

5.8 Initialization of Pointer Arrays


 What features does a
/* month_name : return name of static
n-th month */
variable has?
char *month_name(int n)
Write a main function to output
{
static char *name[ ] = { the months’ names from 1 to
“Illegal month”, 12.
“January”, “February”, “March”,
“April”, “May”, “June”, “July”,
“August”, “September”, “October”,
“November”, “December”
};
return (n< 1 || n>12) ? name[0] : name[n];
}

The i-th string is placed somewhere, and a


pointer to it is stored in name[i]. The size of the array is
not specified, the compiler counts the initializers and
fills in the correct number.
Chapter 5 Pointers and Arrays
5.9 Pointers vs. Multi-dimensional Array

5.9 Pointers vs. Multi-


dimensional ArrayThe import
Difference between a two-dimensional
pointer array is th
array and an array of pointer: may be of differen
int a[10][20];
int *b[10];
Although a[3][4] and b[3][4] are both
syntactically legal reference to a single int,
but:
 a is a true two-dimensional array, b is only a one-
dimensional array
 a has 200 int-sized locations, b only 10 pointers.
 the rows of b may be of different lengths.
Chapter 5 Pointers and Arrays
5.9 Pointers vs. Multi-dimensional Array

Example:
 char *name[ ]={“Illegal month”, “Jan”, ”Feb”, “Mar”};
 char aname[ ][15] = {“Illegal month”, “Jan”, “Feb”, “Mar”};
name:

 Illegal month\0

 Jan\0
storage:26 bytes + 4 pointers
Feb\0

Mar\0

aname:
Illegal month\0 Jan\0 Feb\0 Mar\0
0 15 30 45

storage: 60 bytes
Chapter 5 Pointers and Arrays
5.10 Command-line Arguments

5.10 Command-line Arguments


/* echo command-line arguments: 1st version*/
main(int argc, char *argv[ ] )
{ int i;
for(i=1; i< argc; i++)
printf("%s%s", argv[i], (i<argc-1)? " ": "");
printf("\n");
return 0;  Compile and execute above
} program with command lines:
echo hello, world
echo I am a teacher
echo you are all students
 Two arguments: avgv:

0
  echo\0
argc is for argument count
and argv[ ] for argument  hello,\0
vector. argv[argc] is a null world\0
pointer.  
Chapter 5 Pointers and Arrays
5.10 Command-line Arguments

/*echo command-line arguments: 2nd version*/


main(int argc, char *argv[ ])
{
while(--argc >0)
printf("%s%s", *++argv, (argc>1) ? " ": "");
printf("\n");
return 0;
}
Chapter 5 Pointers and Arrays
5.10 Command-line Arguments

Example 2:
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
int getline(char *line, int max);
/*find: print lines that match pattern from 1st arg */
main(int argc, char *argv[])
{ char line[MAXLINE];
int found=0;
if( argc!=2) printf("Usage: find pattern\n");
else while (getline(line, MAXLINE)>0)
if(strstr(line, argv[1])!=NULL) {
printf(“**** %s ****", line);
found++;
}
return found; The pattern is specified by
} the first argument on the
command line.
Chapter 5 Pointers and Arrays
5.11 Pointers to Functions

5.11 Pointers to Functions


 pointers to functions: can be assigned, placed in
arrays, passed to functions, returned by
functions, …
Chapter 5 Pointers and Arrays
5.11 Pointers to Functions

#include <stdio.h>
#include <string.h>
#define MAXLINES 5000 /*max lines to be sorted */
char *lineptr[MAXLINES]; /*pointers to text lines */
int readlines(char *lineptr[], int nlines);
void qsort(void *lineptr[], int left, int right, int (*comp)(void *, void *));
int numcomp(char *, char *);
/* sort input lines */
main(int argc, char *argv[])
{ int nlines; /* number of input lines read */
int numeric =0;/* 1 if numeric sort */
if(argc>1 && strcmp(argv[1], "-n")==0) numeric =1;
if((nlines = readlines(lineptr,MAXLINES)) >=0) {
qsort((void **)lineptr, 0, nlines-1,
(int (*) (void*, void*))(numeric?numcmp:strcmp));
writelines(lineptr,nlines);
return 0;
}else { printf(“input too big to sort\n”);
return 1; }
}
Chapter 5 Pointers and Arrays
5.11 Pointers to Functions

/* qsort: sort v[left]…v[right] into increasing order */


void qsort(void *v[], int left, int right, int (*comp)(void *, void *))
{ int i,last;
void swap(void *v[],int int);
if(left>=right) return;  What difference is it
swap(v,left,(left+right)/2); with: int *comp(void*,void*)?
last=left;
for(i=left+1;i<=right;i++)
if((*comp)(v[i],v[left])<0) swap(v,++last,i);
swap(v,left,last);
qsort(v,left,last-1,comp); int numcmp(char *s1, char *s2)
qsort(v,last+1,right,comp); { double v1,v2;
} v1=atof(s1);
void swap(void *v[],int i,int j) v2=atof(s2);
{ void *temp; if(v1<v2) return -1;
temp=v[i]; else if (v1>v2) return 1;
v[i]=v[j]; else return 0;
v[j]=temp; }
}

You might also like