0% found this document useful (0 votes)
37 views3 pages

ARRAY Pointer C

1. The document discusses various ways of performing operations on arrays and pointers in C including reading arrays in functions, passing arrays to functions by reference using pointers, returning arrays from functions, and accessing 2D arrays through pointers. 2. Key concepts covered include using pointers to reference array elements instead of indexes, passing addresses of arrays/pointers to functions using pointers, declaring static arrays inside functions to avoid scope issues, and creating arrays of pointers to access 2D arrays. 3. Examples demonstrate functions to read arrays, find maximum values, sort arrays, and display arrays using pointers for pass by reference.

Uploaded by

rkukg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views3 pages

ARRAY Pointer C

1. The document discusses various ways of performing operations on arrays and pointers in C including reading arrays in functions, passing arrays to functions by reference using pointers, returning arrays from functions, and accessing 2D arrays through pointers. 2. Key concepts covered include using pointers to reference array elements instead of indexes, passing addresses of arrays/pointers to functions using pointers, declaring static arrays inside functions to avoid scope issues, and creating arrays of pointers to access 2D arrays. 3. Examples demonstrate functions to read arrays, find maximum values, sort arrays, and display arrays using pointers for pass by reference.

Uploaded by

rkukg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

1

ARRAY / POINTER OPERATIONS int i,size;


float big,x[50];
ARRAY READING IN FUNCTION AND char c;
DISPLAYING IN MAIN i = 0;
printf ("enter elements\n");
#include <stdio.h>
do{
#include <ctype.h>
scanf("%f%c",&x[i],&c);
int main(int argc, char *argv[])
& required for &x[i],since its an
{
array, not a pointer to array
int size=0,i;
i++;
float x[50];
}while(c !='\n');
printf("Enter elements in the
size = i;
array?\n");
big = max(x,&size);
//scanf("%d",&size);
printf("maximum is %f",big);
read(&size,x); /* x is address of array
getchar();
x[ ], so & not required
return 0;
printf("the elements are\n");
}
for (i=0;i<size;i++)
printf("%.2f\n",x[i]);
float max(float *x, int *s)
system("pause");
{
return(0);
int i=0;
}
float max1 = -1.00E38;
for(i=0;i<*s;i++)
read (int *s,float *data)
{
{
if(*(x+i)>max1)
int i= 0;
max1 = *(x+i);
char c;
}
do
return (max1);
{
}
scanf("%f%c",(data+i),&c);
/& not required for (data+i) since its
a pointer to an array/ FINDING LARGEST IN AN
i++; ARRAY USING
}while (c!='\n'); READ(),SORT(),SWAP(),DISPLAY(
*s = i; ) FUNCTIONS WITH POINTERS
}
int main()
{
FINDING THE LARGEST IN AN
int size=0;
ARRAY USING POINTER
float x[50];
read(x,&size);
#include <stdio.h>
sort(x,&size);
#include <ctype.h>
disp(x,&size);
float max(float *x, int *s); -prototype
getch();
int main(int argc, char *argv[])
return(0);
{
2

} int size;
float *ptr;
read (float *x, int *size) printf("enter the numbers\n");
{ ptr = read(&size); /*calling
int i=0; function*/
char c; for (i=0;i<size;i++)
printf ("enter the elements of printf("%f\n",*(ptr+i));
array\n"); }
do {
scanf("%f%c",(x+i),&c); float *read(int *size)
i++; * before function name required to
}while (c!='\n'); indicate to compiler that it will
*size=i; return a value of type float
} {
sort(float *x, int *size) int i=0;
{ char c;
int i,j; static float x[50];
for(i=0;i<*size-1;i++) do{
for (j=i+1;j<*size;j++) scanf("%f%c",&x[i],&c);
if(*(x+i)>*(x+j)) i++;
swap(i,j,x); }while (c!='\n');
} *size = i;
disp(float *x,int *size) return(x);
{ }
int i ; Static required else it variable will be
for(i=0;i<*size;i++) auto and auto variables will be
printf("%f\n",*(x+i)); erased when compiler comes to end
getchar(); of function, and attempt to acces
} such data will give garbage.
swap(int i,int j,float *x)
{
float p,q;
p = *(x+i);
q = *(x+j);
*(x+j) = p;
*(x+i) = q;
}

ACCESSING 2-D ELEMENTS


THROUGH POINTERS
FUNCTION RETURNING ARRAY int main(int argc, char *argv[])
main () {
{ int i,j,nr,nc;
int i; float x[10][10];
3

float **xx[10];//array of pointers


printf ("enter number of rows\n");
scanf("%d",&nr);
printf ("enter number of
columns\n");
scanf("%d",&nc);
for (i=0;i<nr;i++)
for (j=0;j<nc;j++)
scanf("%f",&x[i][j]);
for (i=0;i<nr;i++)
xx[i] = x[i]; creating array of
pointers , add = add of first element
of row
display(nr,nc,xx);
getch();
return 0;
}

display(int nr,int nc, float **u)


{
int i,j;
printf ("the matrix entered is \n");
for (i=0;i<nr;i++)
{
for (j=0;j<nc;j++)
printf ("%f ",*(*(u+i)+j));
printf("\n");
}
}

You might also like