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

ex

#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.,

thanx!!!

Dani AI

Generated

Brief answer for : a parameter declaration like char *p_grade1 actually declares a local variable named p_grade1 whose type is "pointer to char." That declaration appears in the parameter list so the caller can pass an address (for example with &grade1) when calling the function. The * in the parameter list is part of the type, not a runtime dereference; inside the function *p_grade1 means "the char stored at the address in p_grade1."

Building on and : pointer parameters receive a copy of the address you pass. Dereferencing the parameter (*p_grade1) reads or writes the caller's memory, so the function can change the caller's variable. Assigning to the parameter itself (for example p_grade1 = someOtherAddress) only changes the local copy and does not affect the caller. If you need to modify the caller's pointer variable (not just the pointed-to value), use a pointer-to-pointer (char **) parameter.

Practical notes and common pitfalls: char *a, b; declares one pointer and one plain char — the * binds to the name, not the "base" type; prefer char *a, *b; or separate declarations for clarity. Use const char * for parameters you do not intend to modify. Never return the address of a local automatic variable (it will become a dangling pointer), and avoid passing addresses of temporaries or literals.

If you want to swap two pointer variables in the caller (different from swapping the chars they point to), this is the typical pattern:

void SwapPtrs(char **pp1, char **pp2)
{
    char *tmp = *pp1;
    *pp1 = *pp2;
    *pp2 = tmp;
}

Call it with SwapPtrs(&p1, &p2).

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.