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!";
	return 0;

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


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.

Related Tutorials

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.