Writing into a malloc string

Viewed 54

I'm new to C programming language, and malloc precisely. So I created this program to take arguments from argv and put them all in a single string, separated by spaces(on line 32). But when I run it, everything works out, but there seems to be no spaces inserted as desired in the code below.

When I tried commenting, line 28: buffer[t] = argv[str][i]; the spaces appear.

I also tried casting: malloc(buffSize) to (char*) before assigning it to buffer to just see if anything changes¿¿. But nothing.

Please I'd be glad if someone can clear me out on what's going on. Thanks in advance

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

int main()
{
    int str = 0, i = 0, t = 0, letterCount = 0, buffSize;
    char *buffer;
    char *argv[] = {"hello", "me", "too"};
    int argc = 3;

    while (str < argc)
    {
        while (argv[str][i++])
            letterCount++;
        str++;
        i = 0;
    }
    buffSize = letterCount + argc + 1;
    str = 0;
    buffer = (char*) malloc(buffSize);
    if (!buffer)
        return (NULL);
    while (str < argc)
    {
        while (argv[str][i] != '\0')
        {
            buffer[t] = argv[str][i];
            t++;
            i++;
        }
        buffer[t] = ' ';
        str++;
        i = 0;
    }
    printf("%s", buffer);
    return (0);
}
2 Answers

But when I run it, everything works out, but there seems to be no spaces inserted as desired in the code below.

It's unlikely that you run the code presented in the question, as was pointed out in comments, because it is not valid C.

But if you ran a program similar to that and got the result you describe then have a careful look at these two pieces of code:

            buffer[t] = argv[str][i];
            t++;
            i++;

and

        buffer[t] = ' ';
        str++;
        i = 0;

Do you see the important difference between these that explains the issue? What does the former do that the latter fails to do? Did you notice that although your program does not print spaces between words, it does (I expect) print one after the last word?

That's not the only problem with your code, but do look there for the answer to your immediate problem.

actually , you didn't mention in your question what does ac and av stands for , so I deleted them from the code and replaced them with their equivalent numbers.

you are doing a lot of exhaustive programming , like instead of using functions like strlen() and strcat() , you are implementing it manually , but either way , it's your choice.

  1. your problem with in this line buffer[t] = ' '; as you put the character ' ' in the new array but you didn't actually increment the index used in placing characters which is t , that's why the space character is always overridden by the next char so , you should do buffer[t++] = ' ';
  2. in the end , you are printing in line printf("%s", buffer); , but actually the variable called buffer was never terminated by null char '\0' , so at the end you should do buffer[t] = '\0';

that's all what I edited in your code , but your code actually needs to be rewritten again and this is the edited code with these only 2 modifications :

    #include <stdio.h>
#include <stdlib.h>
#define NUM_OF_SPACES    3

int main()
{
    int str = 0, i = 0, t = 0, letterCount = 0, buffSize;
    char *buffer;
    char *argv[] = {"hello", "me", "too"};
    int argc = 3;

    while (str < argc)
    {
        while (argv[str][i++])
            letterCount++;
        str++;
        i = 0;
    }

    buffSize = letterCount + NUM_OF_SPACES + 1;
    str = 0;
    buffer = (char*) malloc(buffSize);
    if (!buffer)
        return (0);
    while (str < argc)
    {
        while (argv[str][i] != '\0')
        {
            buffer[t] = argv[str][i];
            t++;
            i++;
        }
        buffer[t++] = ' ';
        str++;
        i = 0;
    }
    buffer[t] = '\0';
    printf("%s", buffer);
    return (0);
}

and this is the output :

hello me too
Related