0

I'm trying to create an array of patterns for a triangle that I'm also printing to the console. I do this by creating a 2d char array where char patterns [number_of_patterns][pattern_lengths]. I pass this to a function that takes the array patterns along with the height of the triangle I'm trying to make.

void printTriangle (int rows, char rowPatterns[][rows]) {

    int initialSpaces = rows - 1;
    int numberOfAsterisks = 1;
    int i;

    for (i = 0; i < rows; i++) {
        char temp[rows];
        int spaceCounter = 0;
        int asteriskCounter = 0;

        while (spaceCounter < initialSpaces) {
            printf(" ");
            sprintf(temp, " ");
            spaceCounter++;
        }
        while (asteriskCounter < numberOfAsterisks) {
            sprintf(temp, "*");
            printf("*");
            asteriskCounter++;
        }
        while (spaceCounter < initialSpaces) {
            spaceCounter = 0;
            sprintf(temp, " ");
            spaceCounter++;
        }


        strcpy(rowPatterns[i], temp);
        printf("\n");
        initialSpaces--;
        numberOfAsterisks+=2;
    }

}

For every row of the triangle that I'm printing, I create a string for that row called temp. At the end of the for loop that prints the row to the console and sprintf's it to the array temp, I strcpy temp into patterns[i]. Then I go back to the top of the loop, reinitialize temp to make it fresh, and loop again until I have all my rows. Except for some reason sprint won't fill in my array temp. Is this incorrect use of the function, or does it have to do w my parameter passing?

2
  • There's no need to use sprintf at all here — all the things you're appending are constant strings with no formats, so you could just as well use strcpy. And they're all single characters, so you could just as well just assign those single characters. Commented Oct 19, 2015 at 3:08
  • @hobbs: you could, but I actually personally find the API of sprintf to be nicer for building strings, since you just use the idiom ptr += sprintf(ptr, ...). Commented Oct 19, 2015 at 4:18

1 Answer 1

3

sprintf always writes to the start of the string. To append, you can maintain a pointer to the end of the string:

char *ptr = rowpatterns[i];

ptr += sprintf(ptr, "*");

You might also hear the suggestion to use strcat - avoid that function. When building strings, repeated strcat is very slow and is a common source of performance issues in string code.

Sign up to request clarification or add additional context in comments.

3 Comments

so I followed your advice and got the same result, which is strange because it makes very good sense to me. Could it have to do with how I'm assigning the pointer?
Sample code: while (spaceCounter < initialSpaces) { p += sprintf(p, " "); printf(" "); spaceCounter++; }
The only caution required with this is if sprintf() returns -1 to indicate a failure. The addition (subtraction) can lead to odd effects. Also, it is probably better to use snprintf(), but you have to adjust the buffer length as well. That means you end up capturing the return value so you can add it to the pointer and subtract if from the buffer length. But, undeniably, that complicates the code. Sadly, it means people often don't do it, and thus are buffer overflows created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.