Pointers

Sourec Code from Notes


Example - pointers as function arguments


/* program to swap 2 integers*/
#include <stdio.h>

void swap (int *a, int *b);
main()
{
        int     a,b;

        printf("Enter two integers\n");
        scanf("%d %d",&a,&b);

        swap(&a,&b);
        printf("%d %d\n",a,b);
}
void swap (int *a, int *b)
{
        int temp;

        temp = *a;
        *a = *b;
        *b = temp;
}


Example - Pointers as function arguments

This example sends an array to the function. The function calculates both the largest and smallest value stored in the array. These maximum and minimum values stored are then returned to the main calling program as function arguments.

#include <stdio.h>
#include <math.h>
#define MAX 5

void max_min(float *max, float *min, float array[]);

main()
{
        float array1[MAX];
        float array2[MAX];
        float   max1, min1;

        float   max2, min2;
        int     i;

        for (i=0; i<MAX;i++)
        {
                printf("Enter value %d of array1\n",i);
                scanf("%f", &array1[i]);
        }
        for (i=0; i<MAX;i++)
        {
                printf("Enter value %d of array2\n",i);
                scanf("%f", &array2[i]);
        }

        max_min(&max1,&min1,array1);
        max_min(&max2,&min2,array2);

        printf("Array 1 : max = %f min = %f\n",max1,min1);
        printf("Array 2 : max = %f min = %f\n",max2,min2);
}

void max_min(float *max, float *min, float array[])
{
        int     i;

        *min = array[0];
        *max = array[0];

        for (i=1;i<MAX;i++)
        {
                if (array[i] > *max)
                        *max = array[i];
                if (array[i] < *min)
                        *min = array[i];
        }
}


Example - pointers as functions arguments

A sports timer can time an event returning the elapsed time in seconds. Write a function to convert the time in seconds to hours, minutes and seconds, returning these values as function arguments. The function should be called from a main calling program where the user is prompted to enter the elapsed time in seconds.

Pointers must be used in the function arguments for hours, minutes and seconds, as the memory addresses of the variables to change are sent to the function and the function can then alter the contents of these memory addresses. Remember & indicates address and * indicates the contents of that address.

#include <stdio.h>
#include <math.h>


/* Function Declaration*/
void change_time(int elapsed_seconds, int *hours, int *mins, int *seconds);

main()
{
        int     elapsed_time;
        int     time_hours, time_minutes, time_seconds;


        printf("Enter the time for the event\n");
        scanf("%d", &elapsed_time);

        /*Function call - send address of variables to change */
        change_time(elapsed_time, &time_hours, &time_minutes, &time_seconds);


        /* Use variables as normal */
        printf("The number of hours is %d\n",time_hours);
        printf("The number of minutes is %d\n",time_minutes);
        printf("The number of seconds is %d\n",time_seconds);

}

/* Function to convert total in seconds to hours, mins and seconds */
void change_time(int elapsed_seconds, int *hours, int *mins, int *seconds)
{
        int     tmp_minutes;

        *seconds = elapsed_seconds%60;

        tmp_minutes = elapsed_seconds/60;

        *mins = tmp_minutes%60;

        *hours =  tmp_minutes /60;
}


Example

Example program to read in two numbers from the command line and add them together.


#include <stdio.h>
#include <stdlib.h>

main(int argc,char *argv[])
{
        float           a,b,answer;

        /* ensure correct number of arguments */
        if (argc != 3)
        {
                printf("Wrong number of arguments for program\n");
                exit(1);
        }


        /* convert argument string to float */
        a = atof(argv[1]);
        b = atof(argv[2]);

        answer = a + b;
        printf("Answer = %f\n",answer);
}


Example

This example is more complex and is for a program which requires a flag to indicate what operation should be performed on the two numbers. The flags are indicated by the minus (-) sign.

#include <stdio.h>
#include <stdlib.h>

main(int argc,char *argv[])
{
        int     add_flag = 1;
        int     sub_flag = 0;
        int     mult_flag = 0;
        float   a,b;

        argc--;
        argv++;

        while ((argc > 0)&&(**argv == '-')){
                switch(*++*argv){
                        case 'a' : /* add */
                                   add_flag = 1;
                                   break;
                        case 's' : /* subtract */
                                   sub_flag = 1;
                                   add_flag = 0;
                                   break;
                        case 'm' : /*multiply */
                                   mult_flag = 1;
                                   add_flag = 0;
                                   break;
                        default  : printf("Unknown flag\n");
                                   exit(1);
                }
                argc--;
                argv++;
        }

        if (argc != 2)
        {
                printf("Wrong number of arguments for program\n");
                exit(1);
        }

        a = atof(argv[0]);
        b = atof(argv[1]);

        if (add_flag == 1)
                printf("answer = %f\n",a + b);
        else if ( mult_flag == 1)
                printf("answer = %f\n",a * b);
        else if (sub_flag ==1)
                printf("answer = %f\n",a - b);

}



Example

This example sends an array to the function. The function then calculates both the mean value and the variance of the array. The mean and variance are returned to the main calling program as function arguments. Note that pointers are used in the function. The function call sends the addresses of the variables which are to be altered in the function.


#include <stdio.h>
#include <math.h>

#define MAX 5

void stats(float *mean, float *var, float array[]);

main()
{
        float array1[MAX];
        float array2[MAX];
        float   mean1, variance1;
        float   mean2, variance2;
        int     i;

        for (i=0; i


Example

This example has a function to change complex numbers given in the form of a real and an imaginary part into the magnitude and phase representation. It also has another function to do the opposite conversion. These functions are called by the main program.


#include <stdio.h>
#include <math.h>


void change_complex(float x, float y , float *mag, float *phase);
void change_real_imag(float mag, float phase , float *x, float *y);

main()
{
        float   a,b;
        float   mag, phase;
        float   real,imag;

        printf("Enter the real and imaginary part");
        scanf ("%f %f", &a, &b);

        change_complex(a,b,&mag,&phase);

        printf("The magnitude is %f\n", mag);
        printf("The phase is %f\n", phase);

        change_real_imag(mag, phase, &real, &imag);
        printf("The real part is %f\n", real);
        printf("The imaginary part is %f\n", imag);

}
/*Function to change from real and imaginary to magnitude and phase */

void change_complex(float x, float y , float *mag, float *phase)
{
        *mag = sqrt (x*x + y*y);
        *phase = atan(y/x);
}

/*Function to change from magnitude and phase to real and imaginary */

void change_real_imag(float mag, float phase , float *x, float *y)
{
        *x = mag * cos(phase);
        *y = mag * sin(phase);
}


Back to Main Page for Lecture 7