in a few examples i noticed that pointers are declared in functions parameter list.


#include <stdio.h>

void SwapEm (char *p_grade1, char *p_grade2);

int main ()
	char grade1= 'D', grade2 = 'A';
	printf ("At the beginning grade1 is %c and grade2 is %c\n", grade1, grade2);	
	SwapEm (&grade1, &grade2);		
	printf ("At the end grade1 is %c and grade2 is %c\n", grade1, grade2);

void SwapEm (char *p_grade1, char *p_grade2)
	char temp;
	temp = *p_grade1;
	*p_grade1 = *p_grade2;
	*p_grade2 = temp;

why *p_grade1 and *p_grade2 werent officially declared within the function but were declared in parameter list, can you always declare them in parameter list??

any help would be greatly appreciated.,


They are declared in the parameter list so that you can pass address to the function. if you declare them as local variables inside the function how will you pass the address of the variables to that function?

mainly it depends on the requirement where and how you use pointers.

You declare a pointer within a function if you're gonna use it only within the function. If you wanna pass arguments to the function whose value you want processed within the function then you pass their addresses. In your ex, you're passing grade1 and grade2's addresses since you're processing them and using the processed value again in the main function. Check out some online tutorials on pointers for more clarifications.