-
2 Attachment(s)
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);
}
-
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.
-
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.