USING
POINTERS
This example program shows a
pointer being used to swap between two floating point values. The actual
manipulation of the floating point values is done within the function get_value, which doesn't have any parameters. I decided that I
would do it by reassigning a pointer within the main program.
#include
<stdio.h>
/*
The pointer has to be declared here so that */
/*
the function get_value can get access to it. */
float
*my_point;
/*
The floating point variables have to be */
/*
declared here so that function display_values */
/*
can access them.*/
float
x = 36.3, y = -100.44;
int
main ()
{ display_values();
my_point = &x; // Points to x
get_value();
my_point = &y; // Points to y
get_value();
display_values();
return 0;
}
void
get_value ()
{ float decimal_number;
printf(\n "Please enter a number : \n");
scanf(“%f”,decimal_number);
*my_point = decimal_number;
}
void
display_values ()
{ printf("\nThe value of x is %f \n",x);
printf("\nThe value of y is %f \n",y);
}
The
value of x is 36.3
The
value of y is -100.44
Please
enter a number : 2.1
Please
enter a number : 3.1
The
value of x is 2.1
The
value of y is 3.1
_____________________________________________________________________________________________
I thought it might be interesting to see what the address of a variable is. Here I have declared three unsigned long values together with a pointer that moves between them. In each case, the address that the pointer is pointed to is displayed.
I thought it might be interesting to see what the address of a variable is. Here I have declared three unsigned long values together with a pointer that moves between them. In each case, the address that the pointer is pointed to is displayed.
#include
<stdio.h>
int
main ()
{ unsigned long a = 1, b = 2, c = 3, *p;
p = &a;
printf("\nThe address of a is %X\n",p);
printf("\nThe value of a is %d\n",*p);
p = &b;
printf("\nThe address of b is %X\n",p);
printf("\nThe value of b is %d\n",*p);
p = &c;
printf("\nThe address of c is %X\n",p);
printf("\nThe value of c is %d\n",*p);
return 0;
}
The
address of a is 0x3c27245e
The
value of a is 1
The
address of b is 0x3c27245a
The
value of b is 2
The
address of c is 0x3c272456
The
value of c is 3
____________________________________________________________________________________________
This function is used to sort
arrays of integers. The array itself is specified as a pointer. However, the
routine doesn't know in advance how many elements the array will have, so that
must also be passed across as an (ordinary) parameter.
void
sort (*int array, int num_elements)
{ int i, j;
int *temp;
/* Used in swopping array values */
for (i = 0; i < num_elements; i++)
for (j = 0; j < num_elements-1; j++)
if (*(array+j) > *(array+j+1))
{ temp = *(array+j);
*(array+j) = *(array+j+1);
*(array+j+1) = temp;
}
}
You will see that the pointer to the array is simply
declared as a pointer to an integer. You would have no way of knowing just by
looking at the first line of the function that the pointer indicated an array
rather than a simple integer (other than the fact that the pointer is called array, of course, which is something of a giveaway!)
The array elements are referred to using the pointer. *(array+j) refers to element array[j] etc.
Check out the program
without functions:
Sorting an array would
involve swapping the elements as necessary, until they are all in ascending or
descending order. There are different sorting methods (called algorithms)
and this worked example demonstrates what is probably the simplest sorting
algorithm - Bubblesort.
|
Bubblesort is so-called as
the largest elements of the array move gradually to the end like bubbles
rising to the surface in a glass of fizzy drink. It is usually the slowest
sorting algorithm (although it can take advantage of array elements which
have been partially sorted already). The algorithm goes as follows:
|
![]() |
Assume
N is the number of elements in the array.
Go
through the elements from 1 to N-1
Look at the current element and the next
one.
Are they out of order? If so, swop them.
At the end of the run, the largest element
will have
"bubbled" to the end of the
array.
Repeat
this process N times.
Here is the code. Go through it matching it
line-by-line with the algorithm above.
#include
<stdio.h>
int
main ()
{ int x[11]; /* Set up an array of 11 values */
x[0] = 74;
x[1] = 23; x[2] = 91; x[3] = 58;
x[4] = 52;
x[5] = 60; x[6] = 30; x[7] = 19;
x[8] = 28;
x[9] = 46; x[10] = 37;
int i,j;
/* These are used as loop variables */
int
temp; /* Used in swopping the values */
printf("\nBefore the sort, the values
are \n");
for (i = 0; i <= 10; i++)
printf(“x[%d] = %d\n”,i,x[i]);
for (i = 0; i < 11; i++) /* Go through 10 times */
for (j = 0; j < 10; j++) /* Check all elements */
if (x[j] > x[j+1]) /* except last */
{ temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
}
printf("\n\nAfter the sort, the values
are \n");
for (i = 0; i <= 10; i++)
printf(“x[%d]
= %d\n”,i,x[i]);
return 0;
}
Before
the sort, the values are
74
23 91 58 52 60 30 19 28 46 37
After
the sort, the values are
19
23 28 30 37 46 52 58 60 74 91

No comments:
Post a Comment