tags:

views:

5242

answers:

6

I wrote this function that's supposed to do StringPadRight("Hello", 10, "0") -> "Hello00000".

char *StringPadRight(char *string, int padded_len, char *pad) {
    int len = (int) strlen(string);
    if (len >= padded_len) {
        return string;
    }
    int i;
    for (i = 0; i < padded_len - len; i++) {
        strcat(string, pad);
    }
    return string;
}

It works but has some weird side effects... some of the other variables get changed. How can I fix this?

A: 

The argument you passed "Hello" is on the constant data area. Unless you've allocated enough memory to char * string, it's overrunning to other variables.

char buffer[1024];
memset(buffer, 0, sizeof(buffer));
strncpy(buffer, "Hello", sizeof(buffer));
StringPadRight(buffer, 10, "0");

Edit: Corrected from stack to constant data area.

eed3si9n
You're still passing a string literal... :P
yjerem
I noticed that too. Funny.
eed3si9n
you are copying too much. Hello is of type char[6] , but you try to copy 1024 bytes out of it. that can only fail. change it to read sizeof "Hello" instead of the second sizeof(buffer)
Johannes Schaub - litb
strncpy(buffer, "Hello", sizeof(buffer)); already fills the entire buffer with '\0', so your memset() is redundant.
Chris Young
@litb, strncpy: If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
eed3si9n
@Chris, memset after buffer is a habitual thing. I'll leave it in there.
eed3si9n
+2  A: 

You must make sure that the input string has enough space to hold all the padding characters. Try this:

char hello[11] = "Hello";
StringPadRight(hello, 10, "0");

Note that I allocated 11 bytes for the hello string to account for the null terminator at the end.

Greg Hewgill
A: 

The function itself looks fine to me. The problem could be that you aren't allocating enough space for your string to pad that many characters onto it. You could avoid this problem in the future by passing a size_of_string argument to the function and make sure you don't pad the string when the length is about to be greater than the size.

yjerem
A: 

Oh okay, makes sense. So I did this:

    char foo[10] = "hello";
    char padded[16];
    strcpy(padded, foo);
    printf("%s", StringPadRight(padded, 15, " "));

Thanks!

A: 
Johannes Schaub - litb
+8  A: 

It might be helpful to know that printf does padding for you, using %-10s as the format string will pad the input right in a field 10 characters long

printf("|%-10s|", "Hello");

will output

|Hello     |

In this case the - symbol means "Right align", the 10 means "Ten characters in field" and the s means you are aligning a string.

Printf style formatting is available in many languages and has plenty of references on the web. Here is one of many pages explaining the formatting flags. This one has more examples. As usual WikiPedia's printf page is of help too (mostly a history lesson of how widely printf has spread).

Tom Leys