C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

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.









COMMENTS