Function which applies a function passed as second parameter to each index of array pointed to by pointer s

This is Glory Source

enter image description hereI have the following function:

void ft_striter(char *s, void (*f)(char *));

This function is meant to apply the function f to each index of the array pointed to by s.

I've seen the code to it but my problem is that I do not understand the function itself and how I would write a test for it. I would like to be able to understand it clearly and apply it. Can someone kindly break it down for me?

Please see the link to the function in question:



answered 1 week ago Tyker #1

considering you have a null-terminated char*

void ft_striter(char* str, void(*func)(char*)) {
    while (*str)

answered 1 week ago Scheff #2

There is still something unclear in your question but I took the text literally:

ft_striter() iterates through string s. OP didn't define a termination condition. Hence, I used the usual – 0 terminator.

do_something() is another function to demonstrate how f is called. It is important that do_something() matches the signature required for parameter f in ft_striter(). Otherwise, in best case the compiler complains, in worst case – undefined behavior.

Sample code:

#include <stdio.h>

void ft_striter(char *s, void (*f)(char*))
  for (; *s; ++s) f(s);

void do_something(char *s)
  printf("s: '%s'\n", s);

int main()
  char test[] = "Hello World.";
  ft_striter(test, &do_something);
  /* done */
  return 0;


s: 'Hello World.'
s: 'ello World.'
s: 'llo World.'
s: 'lo World.'
s: 'o World.'
s: ' World.'
s: 'World.'
s: 'orld.'
s: 'rld.'
s: 'ld.'
s: 'd.'
s: '.'

Live Demo on wandbox

comments powered by Disqus