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 and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.