Home » C programming language

void pointer as function argument in C programming



Here, we will learn how to pass a string (character pointer) in a function, where function argument is void pointer.

Consider the given example

#include <stdio.h>

//function prototype
void printString(void *ptr);

int main()
{
	char *str="Hi, there!";
	
	printString(str);
	
	return 0;
}

//function definition
void printString(void *ptr)
{
	printf("str: %s\n",ptr);
}

Output

str: Hi, there!

In this example the function parameter ptr is a void pointer and character pointer (string) str will be assigned in it and program will print the string through void pointer.






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.