-1

I'm totally new in C and I want to write a function :

    #include <unistd.h> //import write...
    void ft_putchar(char str[20]) {            
       write(1, &str, 19);
   }
    
    char main() {       
        char str2[20] = "GeeksforGeeks";
        ft_putchar(str2[20]);
        return(0);
    }
Kaherdin
  • 2,055
  • 3
  • 23
  • 34
  • 1
    You don't put the size of the string when using it as an argument. Just `ft_putchar(str2)` – Barmar Sep 02 '21 at 16:35
  • 1
    In an argument, `[20]` is an index, not the size. – Barmar Sep 02 '21 at 16:35
  • Silly question: I've noticed that lots of questions where they re-implement built-in functions use the "ft_" prefix. Where does that come from? – Barmar Sep 02 '21 at 16:36
  • 4
    Kaherdin, Tip: save time, be more productive. Enable all compiler warnings. Then rapidly see that `ft_putchar(str2[20]);` is a problem. Faster than posting on Stackoverflow. – chux - Reinstate Monica Sep 02 '21 at 16:40
  • See [What should `main()` return in C and C++?](https://stackoverflow.com/questions/204476/) — hint: `char` is not one of the acceptable return types except on systems where `sizeof(char) == sizeof(int)` (which are vanishingly rare; I believe the Cray supercomputers fell into this category at one time). – Jonathan Leffler Sep 02 '21 at 16:54
  • Note that printing 19 characters from `str2` means that you print 13 letters and then 6 null bytes to standard output. This is probably not a good idea, though (at one level) it doesn't do much harm. It would be better to use `strlen(str2)` instead of 19. – Jonathan Leffler Sep 02 '21 at 16:56
  • 1
    @Barmar: I've seen it too (the earliest reference I found on SO was from [2013-12-01](https://stackoverflow/q/20307902/15168), though it doesn't cover "where does the name come from"). See [Reddit](https://www.reddit.com/r/C_Programming/comments/edae17/what_does_the_ft_in_ft_putchar_mean/) and [GitHub](https://github.com/mariolamon/42/blob/master/piscine/C/day06/ex00/ft_putchar.c) and so on. The Google search I used was "ft_putchar -site:stackoverflow.com". You could try "ft_putchar 42", possibly with the "-site:stackoverflow.com" term too. The `ft_` prefix is, apparently, for "forty-two". – Jonathan Leffler Sep 02 '21 at 17:22
  • @JonathanLeffler Hmm, I wonder if 42 is the code number for a CS class somewhere, similar to Harvard's CS50. – Barmar Sep 02 '21 at 17:26

1 Answers1

1

Hope this will help you a bit :)

#include <unistd.h> //import write...

void ft_putchar(char *str) {   // the size of the buffer is not required         
   write(1, str, strlen(str)); // 2nd argument is a char*, not a char **
                               // 3rd one is the actual length of your                
                               // string, not the size of the buffer
}

int main() {       
    char str2[20] = "GeeksforGeeks";
    ft_putchar(str2); // the size of the buffer is not required
    return(0);
}
  • Missing ``. Not known, but likely the idea is for the students to re-implement standard library functionality without use of the standard library. Similar questions pop up here from time to time, and `ft_strlen` can be seen in some of them. – Oka Sep 02 '21 at 17:27