phougatv
phougatv

Reputation: 1004

Use of Pointers in a separate function file

Working of Pointers

// swap.h

swap(int, int);

// swap.c

swap(int *i, int *j)
{
  int k;
  k=*i;
  *i=*j;
  *j=k;
}

// Practice.c

#include <stdio.h>
#include "swap.h"

main()
{   
  int i,j;

  printf("\nEnter I = ");
  scanf("%d",&i);
  printf("\nEnter J = ");
  scanf("%d",&j);

  swap(&i, &j);

  printf("\n I = %d",i);
  printf("\n J = %d",j);
}

When I wrote this program in one file, the program was correctly executed. Now after I divided it into 2 parts, Practice.c that has main() function and swap.c that contains the swap(int *i, int *j) function, it did not go so well. Here is the following process I used to execute the program.

gcc -c swap.c
gcc Practice.c swap.o -oPractice

As soon as I tried to execute the 2nd statement it did not compile and produced errors.

I used exactly the same process for executing another program, which had 3 files,

  1. Practice.c { main() function }
  2. Add.c { add() function }
  3. Add.h { header file }

It did not have pointers.

Please tell me where I'm making the mistake.

Upvotes: 0

Views: 90

Answers (1)

Rohan
Rohan

Reputation: 53316

In swap.h change

swap(int, int);

to

swap(int *, int *);

Your swap function takes integer pointers as parameters, but your declaration says to take integers. So compiler will give you errors.

Upvotes: 5

Related Questions