-1

Hello guys, i have this code:

#include <stdio.h>
    int main()
    {
    int numberPosition=8;
    char senha[1000]="01000hello";
    printf("%i\n", numberPosition);
    senha[numberPosition]="";
    printf("\n%s\n", senha);
    
    return 0;
    }

When I executes my code my return is: 01000heo.

However if I delete the line "printf("%d\n", numberPosition);" my return is: 01000helo

Why printf deletes an element from my array?

dumb163
  • 21
  • 5
  • 6
    `senha[numberPosition]="";` What do you intend that to do? Why are you assigning a string to a `char` variable? – kaylum Sep 18 '22 at 05:54
  • 3
    1. It doesn't. 2. `senha[numberPosition]="";` is a bad idea & messes things up. Adjust your warning settings on your compiler so that you get one there & then fix. Don't know what you intend by that line, but it makes no sense. – Avi Berger Sep 18 '22 at 05:58
  • It's an exercise in encryption. So the encryption gives an RNG in binary where it adds a random character in the String[BinariePosition] so to decrypt I need to delete this char String[BinariePosition] from my string – dumb163 Sep 18 '22 at 05:58
  • 2
    Does this answer your question? [How to remove the character at a given index from a string in C?](https://stackoverflow.com/questions/5457608/how-to-remove-the-character-at-a-given-index-from-a-string-in-c) – ATP Sep 18 '22 at 06:00
  • 1
    The _accident_ happened before `printf()` was called. Don't blame proven to be reliable library functions when your code doesn't do what you want it to do. Sorry... The title and question should be, "What have **I** done wrong in this code?" – Fe2O3 Sep 18 '22 at 06:06
  • What you tried doesn't remove a character, it replaces one with a semi-random value. That value may not necessarily be a printable character. – Avi Berger Sep 18 '22 at 06:08
  • Did you get any compiler warnings? Please read https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings – n. m. could be an AI Sep 18 '22 at 07:06
  • Basic novice tips for this code: 1. Enable compiler warnings, `-Wall -Wextra` for _gcc_ and _clang_, `/W4` for _MSVC_. Then fix warnings in your own code! 2. Use `{}`, even when you don't have to, and use auto-indent or auto-format/beautify on your code. – hyde Sep 18 '22 at 09:41
  • @Fe2O3 Well, while I overall agree with that, that "proven to be reliable" may be [a bit too optimistic](https://stackoverflow.com/questions/66741172/alternatives-to-printf-for-misra-c-2004-compliant-code). – Bob__ Sep 18 '22 at 10:19
  • @Bob__ So, as the Golgifrinchans(?) (Hitchhiker's Guide to the Galaxy) said about why they hadn't yet invented the wheel, "Well you tell us what colour it should be!!!" I don't know the standard described at that link, but... I'll stick with & Co. over and above what most _boutique_ alternatives might offer. "No one ever got fired for buying IBM!!" `:-)` – Fe2O3 Sep 18 '22 at 10:32

1 Answers1

2

senha[numberPosition]=""; is problematic as the left side is expecting a char but the right side is a char * which is then implicitly cast to an integer. This is often an error and gcc will generate a warning. Here is the explicit cast:

senha[numberPosition]=(unsigned long) "";

This will convert the address where the string "" is stored to an integer. It happens to evaluate to 8 which is backspace \b:

./a.out | od -a
0000000   8  nl  nl   0   1   0   0   0   h   e   l  bs   o  nl
0000016

what you want, what you really, really want is:

senha[numberPosition]='\0';

which will print:

8

01000hel

You clarified you wanted the output "01000helo" and either of these produce that output for me:

#include <stdio.h>

int main() {
    int numberPosition=8;
    char senha[1000]="01000hello";
    senha[numberPosition] = 'o';
    senha[numberPosition+1] = '\0';
    printf("%s\n", senha);
}

or:

#include <stdio.h>
#include <string.h>

int main() {
    int numberPosition=8;
    char senha[1000]="01000hello";
    strcpy(senha + numberPosition, "o");
    printf("%s\n", senha);
}

It would be a good idea to add boundary checks.

Your comment below talks about removing characters which is a different problem that you initially described. You want to check out memmove() (which permit overlap of dst, src, unlike strcpy(), memcpy()).

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • Didn't actually click the link, but saw "...youtube..."... Are you (quite understandably!) _losing it_ (after all these sorts of questions?) `:-)` **Strongly** advise a bit of an intermission if this is getting to you... `:-)` – Fe2O3 Sep 18 '22 at 09:59
  • thanks for the explanation about string/char*, however I need the output `01000helo` as the output. I will try the 'remove char from string' as mentioned a little while ago – dumb163 Sep 18 '22 at 16:08
  • Then you do `senha[numberPosition]='o'; senha[numberPosition+1]='\0'` or `strcpy(senha + numberPosition, "o")`. – Allan Wind Sep 18 '22 at 17:25
  • doesn't worked for me. that method return `01000heloo` instead `01000helo` Im using `void removeChar(char *str, unsigned int index) { char *src; for (src = str+index; *src != '\0'; *src = *(src+1),++src) ; *src = '\0';}` and its working 100% for me – dumb163 Sep 18 '22 at 17:54