LabSheet8
LabSheet8
Pointers
#include<stdio.h> main()
{
int a=10, *b;
b= &a;
printf(" \n a=%d\tb=%d\t*b=%d\n",a,&a,*b);
}
2. Write a program which contains one regular integer variable x and one pointer to integer
ip. Try out the following basic uses of pointers:
• Assign ip the address of x.
• Using the pointer as the argument to scanf(), read a value into the location ip points
at ( i.e. x) and then print out the value of x.
• Assign a new value to *ip, and print out x.
• Assign a value to x and print out *ip.
3. Write and test the following function: void CharSwap( char *cp1, char *cp2 ); The function
must swap the characters in the memory locations stored by its two pointer arguments.
4. Write a function which finds both the minimum and maximum elements in an array of
doubles. The function will need four arguments: the array, the length of the array, and two
pointers to double through which the function can return the minimum and maximum
values. Here is the prototype for the function:
void MinMax( double *data, int length, double *retmin, double *retmax); Write a main( )
to test the function.
5. Declare an array of 5 integers. Print out the address of each element of the array. To print
the address you can use %p printf() format which usually prints pointers in hexadecimal.
Use the %u (unsigned) printf() format if you would rather not deal with hexadecimal
notation. Notice how the difference between each successive address is sizeof(int).
6. Create an array of characters and fill it with a string. Define a character pointer and assign
it the base address of the array. Use pointer arithmetic to traverse the array and print out
the characters one at a time.
1
7. Write a function which takes a char* as it’s only argument and prints out each character in
the array one at a time. Use pointer arithmetic to step through the array.
8. Write your own version of the library function strcpy(). strcpy() takes two arguments,
dest and source. It copies source onto dest. Use pointer arithmetic to manipulate the
arrays. Declare the function like this: void StringCopy( char *dest, const char *source);
9. Write your own version of the library function strcat(). Recall that strcat() takes two
arguments, dest and src, and appends src onto the end of dest. strcat() must first find the
end of the dest string, and then copy src onto the end of dest. Declare the function like
this:
void StringCat( char *dest, char *src);
10. Write a program to read and print an array of n numbers, then find out the smallest number.
Also print the position of the smallest number.
Use the following functions in the program.
void read_array(int *array, int n); void
print_array(int *array , int n);
void find_small(int *array, int n, int *small, int *pos);
12. Write a program to multiply two matrices of order mXn and pXq respectively using
pointers. Allocate memory dynamically.