Knowee
Questions
Features
Study Tools

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 *));

🧐 Not the exact question you are looking for?Go ask a question

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:

  1. We first include the stdio.h library which allows us to use the printf function for printing.

  2. We define a function print that takes a string name and prints it.

  3. We define the function print_name that takes a string name and a function pointer f. This function calls f with name as an argument.

  4. In the main function, we define a string name and call print_name with name and the print function as arguments. This will print the name "John Doe".

This problem has been solved

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;}

1/2

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.