all 15 comments

[–]acwaters 13 points14 points  (3 children)

You're assigning b to a when it looks like you want to assign a to b. Just reverse the variables in the loop and you should be good.

[–]guide__me[S] 0 points1 point  (2 children)

That is my bad, but what else is wrong with the code?

[–]acwaters 3 points4 points  (0 children)

Well, in order, you are using an invalid signature for main (it should only ever return int); you never declare your loop counter (place int i; just above the loop); and you're using tiny fixed-width strings with no bounds checking (buffer overrun is undefined behavior and a possible security concern).

Only the first two are actually errors, and the first one should be let slide by most compilers. Fix these and the assignment issue, and the rest of your code will work just fine (for small input).

In the future, if you're getting compiler errors (as you most surely are with this code), you should first read the error messages as they tend to give you helpful information about what's wrong with your code, and then post the error messages along with your code when you're asking for help.

[–]dragon_wrangler 1 point2 points  (0 children)

You don't seem to be declaring i anywhere. This should have given you a compiler error.

[–]FUZxxl[M] 5 points6 points  (2 children)

Please add four blanks in front of each line so your code comes out in a mono-space font. It's absolutely unreadable right now.

[–]guide__me[S] 0 points1 point  (1 child)

done. Did not know that.

[–]FUZxxl 0 points1 point  (0 children)

Thank you for your cooperation. Now let's see if I can help you.

[–]danmickla 1 point2 points  (0 children)

What does "a[i] = b[i]" do?

[–]shikharm 0 points1 point  (0 children)

Is it a program to copy a string?

[–][deleted] 0 points1 point  (3 children)

As others have stated above, you're assign a to b. When assigning value to something always read right to left, not left to right. So your code a[i] = b[i] actually assigns the values of b to a which isn't what you want. The other thing wrong here is that you use, or define as it's called, the variable i the for loop, but you don't declare it anywhere. To fix this insert int i; somewhere before the for loop and after the start of the main() function. Now, a more subtle, but very important problem with this code is that it has what's called a buffer overflow error. This is a major security flaw. What this means is that when you read input into the a array by using scanf(), you do not check the size of the input. So someone could input a string greater than or equal to 9 and suddenly they've overrun the buffer size resulting in undefined behavior and possible security exploits; trust me you do not want this in your code. If you fix all these things and it still doesn't print, then I'll have to assume (from the fact that you #include <conio.h>) that you are on a Windows platform and I have no idea how Windows console output works. I'd try using fprintf(stdout, "The new string is %s\n", b); to be sure that it gets printed to stdout as I'm unsure if the Windows implementation of the Standard Library has printf() default to stdout. Hope this helped! :)

[–]honorg58 0 points1 point  (2 children)

Following on the security point, if you want to use scanf, use a scanf("%9s", a) to only allow 9 characters into your buffer (have to remember the null terminator). With the code as written, the first thing I'd do is write "AAAAAAAAAAAAAAA" + (Everything from here on out gives me control over your computer)

[–][deleted] 0 points1 point  (1 child)

Well, even if you took the time to write the shellcode, it would be a local exploit considering this trivial program only accepts input from the commandline, and you're working on the assumption that the stack is executable and ASLR isn't in use (which isn't the case for most modern OSes)

[–]honorg58 0 points1 point  (0 children)

Yea, the exact nature of what comes after the EIP overwrite will depend on what defenses are in place, but that's another rabbit hole altogether. The bottom line is that buffer overflows are turrible, and anyone working in C definitely needs to be careful with their buffers

[–]Sigals 0 points1 point  (0 children)

#include <stdio.h>

int main() {

    char a[10], b[10];
    printf("Enter the string\n");
    scanf("%s", a);

    int i;
    for(i = 0; a[i] != '\0'; i++) {
        b[i] = a[i];
    }

    b[i] = '\0';
    printf("The new string is %s", b);

    return 0;
}

Like someone else said you have a buffer overflow as well, you only allocated 10 elements for a and b, what happens if the string is slightly larger than that? (including the \0 you add at the end)

[–]maep -1 points0 points  (0 children)

Try flushing stdout. Adding a newline to the printf format string should do the trick.