Thread: BZERO recreation

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    38

    Cool BZERO recreation

    Helloo... been staring at the same screen for too long and am hoping I can get a tip or two off you cboarders!

    I have attempted to re-code the BZERO function. The function appears to do what I want it to do. I am having trouble printing out the result in my main for testing purposes. My main output keeps showing nothing..

    Regarding pointers, is it possible to go back with a pointer after having gone forward? In my example, my string is "abcdcdee". Once I have moved the pointer to "ee", can I move it back to retrieve the full string again, to then send it back in to the main?

    Function:
    Code:
    #include <stdlib.h>
    
    void ft_bzero(void *s, size_t n)
    {
        char   *sentence;
    
    
        sentence = (char*)s;
        printf("n is: %u\nsentence is: %c\n\n\n", n, *sentence);
        while(n > 0)
        {
            printf("n is: %u\nBEFORE: %c\n", n,*sentence);
            *sentence = '\0';
            printf("AFTER: %c\n\n", *sentence);
            sentence++;
            n--;
        }
        //sentence = sentence - n????;
        printf("The remainder:\n%s\n\n", sentence);
        return(0);
    }
    MAIN:
    Code:
    #include <stdio.h>#include <string.h>
    
    
    int main () {
    
    
        char str[] = "abcdcdee";
        int c = 6;
    
    
        ft_bzero(str, c);
        printf("String after first |%d| bytes changes to 0 is - |%s|\n", c, str);
    
    
       return(0);
    }
    Attached Files Attached Files

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    How do you expect to be able to still display the string since you destroy it? You fill the first "n" characters inside it with '\0'. You need to skip those characters, not try to print them.

    You can of course revert the pointer quite easily, not the string contents though.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    38
    Hi GReaper,

    Thanks for your post. I was trying to print the whole string as I thought that the remaining part would show.. "\0\0\0\0\0\0ee". I was expecting to see the ee using %s.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memcpy recreation
    By Tigertan in forum C Programming
    Replies: 6
    Last Post: 10-22-2017, 11:33 AM

Tags for this Thread