Write a function that prints a name.Prototype: void print_name(char *name, void (*f)(char *));
Question
Write a function that prints a name.Prototype: void print_name(char *name, void (*f)(char *));
Solution
Sure, here is a simple function in C that prints a name using a function pointer:
#include <stdio.h>
// Function to print name
void print(char *name) {
printf("%s\n", name);
}
// Function that takes a name and a function pointer
void print_name(char *name, void (*f)(char *)) {
f(name);
}
int main() {
char *name = "John Doe";
print_name(name, print);
return 0;
}
In this code:
-
We first include the
stdio.hlibrary which allows us to use theprintffunction for printing. -
We define a function
printthat takes a stringnameand prints it. -
We define the function
print_namethat takes a stringnameand a function pointerf. This function callsfwithnameas an argument. -
In the
mainfunction, we define a stringnameand callprint_namewithnameand theprintfunction as arguments. This will print the name "John Doe".
Similar Questions
Write a function that prints strings, followed by a new line.Prototype: void print_strings(const char *separator, const unsigned int n, ...);where separator is the string to be printed between the stringsand n is the number of strings passed to the functionYou are allowed to use printfIf separator is NULL, don’t print itIf one of the string is NULL, print (nil) insteadPrint a new line at the end of your functionjulien@ubuntu:~/0x0f. Variadic functions$ cat 2-main.c#include "variadic_functions.h"/** * main - check the code * * Return: Always 0. */int main(void){ print_strings(", ", 2, "Jay", "Django"); return (0);}julien@ubuntu:~/0x0f. Variadic functions$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-print_strings.c -o cjulien@ubuntu:~/0x0f. Variadic functions$ ./c Jay, Djangojulien@ubuntu:~/0x0f. Variadic functions$
What is the output of this C code?#include <stdio.h> struct student { char *name; }; struct student s; struct student fun(void) { s.name = "newton"; printf("%s\n", s.name); s.name = "alan"; return s; } void main() { struct student m = fun(); printf("%s\n", m.name); m.name = "turing"; printf("%s\n", s.name);
What is the output of the following code:#include <stdio.h>void main(){ char *s = "hi"; char *p = s * 3; printf("%c\t%c", *p, s[1]);}h il eCompilation failsl h
o store the address of this function:void neyo(void);to the variable f of type pointer to a function that does not take any argument and does not return anything, you would do (check all correct answers if there is more than one):*f = neyo;*f = &neyo;f = &neyo;f = neyo;
What will be the output for the following code?1234567891011121314151617181920212223242526#include <iostream>class City {public: City() { name = new char[20]; } ~City() { delete[] name; } void SetName(const char* newName) { name = new char[strlen(newName) + 1]; strcpy(name, newName); } void DisplayName() { std::cout << "City: " << name << std::endl; }private: char* name;};int main() { City city; city.SetName("Metropolis"); city.DisplayName(); return 0;}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.