1

So I save this main array in a temporal array. Then the main array I use '\0' to "delete" whats inside of it. At this point if i print the main array it will just print blank. But if then i use a for to like main array = temporal array. with x = 0 x < 4 x++, it does copy the four things from the other array but then it also prints what it had before i did the '\0'

Ive tried with a while an a counter but didnt work either. And ive used \0 before and it worked so idk why it isnt working now

for(int y = 0; y <= strlen(numeros); y++){
    numeros[y] = '\0';
}

printf("%s\n", numeros);

for(int z = 0; z <= 4; z++){
    numeros[z] = numerosTemp[z];
}
    printf("%s\n", numeros);

My expected result was just the first four letters of the numerosTemp array but it comes with everything it used to have before i did \0. But in the printf that is after the \0 it does only print blanks so i dont understand why this is happening

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Diego Esquivel
  • 182
  • 1
  • 12

2 Answers2

5

strlen figures out how far until the first '\0'. On your first loop, you insert a '\0' at the beginning of the string. Then you check your loop condition, and lo and behold, strlen(numeros) is 0 (because the very first character is now the NUL terminator), and y (now with a value 1) is greater than that, so you're done.

If you want to zero it out, you can store off the original strlen and use that for the test. Or you can just remove your loop entirely and replace it with a memset:

memset(numeros, '\0', strlen(numeros));

Or you can write your loop to zero until it hits the NUL, turning a two-pass solution into a one-pass solution:

for (int i = 0; numeros[i]; ++i) {
    numeros[i] = '\0';
}

The end condition of numeros[i] means it stops when it reaches a value that was already NUL, zeroing non-NUL values while it searches.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Thankss, that worked perfectly, i used the numeros[i] in the for. – Diego Esquivel Sep 12 '19 at 02:48
  • @DiegoEsquivel: Yeah. It's both the minimal change and the most efficient simple solution (adapting [the ridiculously convoluted code used to implement `strlen` in most standard libraries](https://stackoverflow.com/q/57650895/364696) might make it even faster, but it's 99.999999% likely to not be worth it). – ShadowRanger Sep 12 '19 at 02:53
  • this solution does not scale well with time, OP needs to store the size somehow if he wants to zero them out. – Hanif Bin Ariffin Sep 12 '19 at 03:31
  • 1
    @chux: No it doesn't; `numeros[i]` is always read/tested *before* the NUL assignment to that index, so `'abc\0'` would read `a`, write `\0` (leaving `'\0bc\0'`), increment `i`, read `b`, write '\0', (leaving `'\0\0c\0'`), increment `i`, etc. `strlen` is always scanning from the beginning, which causes the problem; testing `numeros[i]` alone is always checking the *next* character after the `NUL` you just wrote. – ShadowRanger Sep 12 '19 at 04:51
  • @HanifBinAriffin: It scales perfectly; C-style strings don't have a known length, you have to scan for the terminating `NUL`. This combines that scan with writing `NUL`s over the non-`NUL` bytes as it goes; it does precisely `O(n)` work, reading `n` times, and writing `n` times, which is the best you can do big-O-wise. – ShadowRanger Sep 12 '19 at 04:53
-3

Hint:


for(int y = 0; y <= strlen(numeros); y++){
    printf("blanking character %d\n", y);
    numeros[y] = '\0';
}

printf("%s\n", numeros);

for(int z = 0; z <= 4; z++){
    numeros[z] = numerosTemp[z];
}
    printf("%s\n", numeros);

Make a prediction about what that will print, then compare it with reality.

I'll explain later, when I have some more time.

Stobor
  • 44,246
  • 6
  • 66
  • 69
  • Well what I can see from that is it prints the length of numeros. And saves \0 in each space of the array. Then it prints this array which is blank (doesnt really print anything besides the spaces of the \n) then the other for saves the first 5 elements numerosTemp have in the first 5 spaces of numeros and then it should print only this 5 spaces that were saved. – Diego Esquivel Sep 12 '19 at 02:38
  • In reality it is printing the blank spaces as i said before. But when it is supposed to print only the 5 spaces that were saved it does print the 5 spaces of whatever numerosTemp has inside but it also prints the rest of what numeros had before doing the \0 – Diego Esquivel Sep 12 '19 at 02:39