M
M
MrBman2020-01-27 19:46:14
linux
MrBman, 2020-01-27 19:46:14

How to pass a string that ends with: !x as a command line argument?

Basically, I wrote this code:

Program source

#include <stdio.h>
#include <stdlib.h>

size_t str_copy2buf(size_t n, char buf[n], const char str[])
{
    size_t i;

    for (i = 0; i < n; i++) {
        if ((buf[i] = str[i]) == '\0')
            break;
    }
    return i;
}

size_t str_append2buf(size_t n, char buf[n], const char str[])
{
    size_t i, j;

    for (i = 0; i < n; i++) {
        if (buf[i] == '\0')
            break;
    }
    for (j = 0; i < n; i++) {
        if ((buf[i] = str[j]) == '\0')
            break;
        j++;
    }
    return i;
}

int main(int argc, char *argv[argc+1])
{
    char buf[15];
    size_t index;

    index = str_copy2buf(sizeof(buf), buf, argv[1]);
    if (index == sizeof(buf)) {
        fprintf(stderr, "error: copy failed!\n");
        return EXIT_FAILURE;
    }

    index = str_append2buf(sizeof(buf), buf, argv[2]);
    if (index == sizeof(buf)) {
        fprintf(stderr, "error: append failed!\n");
        return EXIT_FAILURE;
    }

    printf("%s\n", buf);
    return EXIT_SUCCESS;
}


Then I do this:
Terminal screenshot
5e2f125ec45a1670641687.png

How to make it work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2020-01-27
@MrBman

Try passing in single quotes, not double quotes.
Why are you not satisfied with the standard functions for working with strings?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question