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?
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.
Related
I'm trying to do a program that get number of names from the user, then it get the names from the user and save them in array in strings. After it, it sort the names in the array by abc and then print the names ordered. The program work good, but the problem is when I try to free the dynamic memory I defined.
Here is the code:
#include <stdio.h>
#include <string.h>
#define STR_LEN 51
void myFgets(char str[], int n);
void sortString(char** arr, int numberOfStrings);
int main(void)
{
int i = 0, numberOfFriends = 0, sizeOfMemory = 0;
char name[STR_LEN] = { 0 };
char** arrOfNames = (char*)malloc(sizeof(int) * sizeOfMemory);
printf("Enter number of friends: ");
scanf("%d", &numberOfFriends);
getchar();
for (i = 0; i < numberOfFriends; i++) // In this loop we save the names into the array.
{
printf("Enter name of friend %d: ", i + 1);
myFgets(name, STR_LEN); // Get the name from the user.
sizeOfMemory += 1;
arrOfNames = (char*)realloc(arrOfNames, sizeof(int) * sizeOfMemory); // Change the size of the memory to more place to pointer from the last time.
arrOfNames[i] = (char*)malloc(sizeof(char) * strlen(name) + 1); // Set dynamic size to the name.
*(arrOfNames[i]) = '\0'; // We remove the string in the currnet name.
strncat(arrOfNames[i], name, strlen(name) + 1); // Then, we save the name of the user into the string.
}
sortString(arrOfNames, numberOfFriends); // We use this function to sort the array.
for (i = 0; i < numberOfFriends; i++)
{
printf("Friend %d: %s\n", i + 1, arrOfNames[i]);
}
for (i = 0; i < numberOfFriends; i++)
{
free(arrOfNames[i]);
}
free(arrOfNames);
getchar();
return 0;
}
/*
Function will perform the fgets command and also remove the newline
that might be at the end of the string - a known issue with fgets.
input: the buffer to read into, the number of chars to read
*/
void myFgets(char str[], int n)
{
fgets(str, n, stdin);
str[strcspn(str, "\n")] = 0;
}
/*In this function we get array of strings and sort the array by abc.
Input: The array and the long.
Output: None*/
void sortString(char** arr, int numberOfStrings)
{
int i = 0, x = 0;
char tmp[STR_LEN] = { 0 };
for (i = 0; i < numberOfStrings; i++) // In this loop we run on all the indexes of the array. From the first string to the last.
{
for (x = i + 1; x < numberOfStrings; x++) // In this loop we run on the next indexes and check if is there smaller string than the currnet.
{
if (strcmp(arr[i], arr[x]) > 0) // If the original string is bigger than the currnet string.
{
strncat(tmp, arr[i], strlen(arr[i])); // Save the original string to temp string.
// Switch between the orginal to the smaller string.
arr[i][0] = '\0';
strncat(arr[i], arr[x], strlen(arr[x]));
arr[x][0] = '\0';
strncat(arr[x], tmp, strlen(tmp));
tmp[0] = '\0';
}
}
}
}
After the print of the names, when I want to free the names and the array, in the first try to free, I get an error of: "HEAP CORRUPTION DETECTED: after normal block(#87)". By the way, I get this error only when I enter 4 or more players. If I enter 3 or less players, the program work properly.
Why does that happen and what I should do to fix it?
First of all remove the unnecessary (and partly wrong) casts of the return value of malloc and realloc. In other words: replace (char*)malloc(... with malloc(..., and the same for realloc.
Then there is a big problem here: realloc(arrOfNames, sizeof(int) * sizeOfMemory) : you want to allocate an array of pointers not an array of int and the size of a pointer may or may not be the same as the size of an int. You need sizeof(char**) or rather the less error prone sizeof(*arrOfNames) here.
Furthermore this in too convoluted (but not actually wrong):
*(arrOfNames[i]) = '\0';
strncat(arrOfNames[i], name, strlen(name) + 1);
instead you can simply use this:
strcpy(arrOfNames[i], name);
Same thing in the sort function.
Keep your code simple.
But actually there are more problems in your sort function. You naively swap the contents of the strings (which by the way is inefficient), but the real problem is that if you copy a longer string, say "Walter" into a shorter one, say "Joe", you'll write beyond the end of the allocated memory for "Joe".
Instead of swapping the content of the strings just swap the pointers.
I suggest you take a pencil and a piece of paper and draw the pointers and the memory they point to.
helloeveryone. I am fairly new to programming and currently trying to learn C programming to advance further in any of my projects. I've just learned how to use malloc and realloc, and all seemed good until I attempted to use strcat to combine two given strings from multidimensional array.
I am supposed to get combination of two strings based on the user inputs, and strangely, the first character is either missing or replaced by other characters...
I'll include the source code as well as the output below. I'd really appreciate you help. Thanks in advance!! ( don't mind the Korean at the end... I am korean :P)
enter code here
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
int i, j;
const int row = 3;
char *pstr[3];
char temp[100];
int k,p = 0;
printf("Type in any three characters\n");
for (i = 0; i < row; i++)
{
pstr[i] = (char *)malloc(strlen(temp) + 1); //initialize the lenght of the elements in 2 part of 2D array of char b[ROW] via length of the given string
}
for (i = 0; i < row; i++)
{
scanf("%s", temp);
strcpy(pstr[i], temp);
}
printf("\n");
for (i = 0; i < row; i++)
{
printf("%s\n", pstr[i]);
}
scanf("%d", &p);
scanf("%d", &k);
printf("%s\n", pstr[p]);
printf("%s\n", pstr[k]);
*pstr[k] = (char *)realloc(pstr[k], strlen(pstr[p])+100);
strcat(pstr[k], pstr[p]);
printf("%s", pstr[k]);
for (i = 0; i < row; i++)
{
free(pstr[i]);
}
return 0;
}
\
output::LINK IS AN INTERNATIONAL SIGN FOR , IMAGE OVER HERE!!!
Two major problems:
You use temp before it have been initialized, when its contents is indeterminate and that will lead to undefined behavior.
When you do *pstr[k] = realloc(...) you dereference the pointer in pstr[k] and gets it's first element, which is a single character. You then assign the result of the realloc call to this char element. So you basically lose the actual pointer and pstr[k] will still point to the same memory (which might now be invalid).
There are other problems, but these two are the worst.
I found these in your code
1) if k or p is greater than 2 it will give runtime error
2) *pstr[k] = (char *)realloc(pstr[k], strlen(pstr[p])+100);
but this line can give error in compile time also(mac at least) - as they are not same
so you may change like this -
*pstr[k] = *(char *)realloc(pstr[k], strlen(pstr[p])+100);
3) After realloc you will get exception in free. see this - How free memory after of realloc
I am working on a problem for CS50 in which I must create a pyramid out of #'s based on user input of a height. Here is what I have so far, but for height of 8, it only iterates once. For height of 7, I get about four lines of just a mess of #'s.
//Create GetPosInt() function
int GetPosInt(void) {
int n = GetInt();
while (n <= 0) {
printf("That won't work...\nRetry: ");
n = GetInt();
}
return n;
}
int main(void) {
printf("How high should Mario's pyramid be?\nHeight: ");
int h = GetPosInt();
while (h > 23) {
printf("Try something smaller!\nHeight: ");
h = GetPosInt();
}
char str[] = "##";
char strad[] = "#";
int l = h + 1;
for (int i = 0; i < h; i++) {
printf("%*s\n", l, str);
strcat(str, strad);
return 0;
}
}
This is my first attempt with the string.h library.
Please only tips on fixing my code - I'm certain there are other ways about it, but if it's possible this way, I'd like to keep it so for the class!
Your str array/C-String has no room to concatenate other chars than 2 chars.
As a little change you could do:
char str[128] = "";
strcat(str, "##");
Your return 0; is inside the loop, hence why. Edit as so:
for (int i = 0; i < h; i++) {
printf("%*s\n", l, str);
strcat(str, strad);
}
return 0;
In the C programming language you have to manage memory yourself. These memory allocations will not expand when you use strcat. Instead you will override memory beyond the memory allocated for str. I strongly suggest you first do an introduction into programming in C and how to manage memory manually.
You should return after the loop. You are leaving the function as soon as the first iteration finishes.
Also you should try defining str as :
char * str;
str = malloc( sizeof(char)* 2);
str = strcat(str, "##");
And using strcat as : str = strcat(str,strad);
I'm trying to recursively modify a 2D char array (array of strings), but after the first recursive call the array returns blank even though the changes register in the base case
int main(int argc, const char * argv[]) {
int height;
printf("Enter the height of your triangle.\n");
scanf("%d", &height);
printf("Lets see if you were successful!\n");
fractalTriangle(height, 1);
}
void fractalTriangle (int height, int fractalLevel) {
//has to make the array and do the printing all in the same function
char trianglePattern [height][2 * height - 1];
if (fractalLevel == 0) {
int rowSize = 2 * height - 1;
char rowString[rowSize]; //string to store in pattern's array
int asteriskCount = 1; //number of asterisks printed in each row
int spaces = (rowSize - asteriskCount) / 2; //how many spaces need to be printed in this current row
int rowCount;
for (rowCount = 0; rowCount < height; rowCount++) {
char *ptr = trianglePattern[rowCount];
int counter = 0;
int astCounter = 0;
int spCounter = 0;
while (spCounter < spaces) {
if (counter == 0) {
strcpy(rowString, " ");
}
else {
strcat(rowString, " ");
}
counter++;
spCounter++;
}
while (astCounter < asteriskCount) {
if (counter == 0) {
strcpy(rowString, "*");
}
else {
strcat(rowString, "*");
}
counter++;
astCounter++;
}
spCounter = 0;
while (spCounter < spaces) {
strcat(rowString, " ");
spCounter++;
}
asteriskCount+=2;
spaces--;
strcpy(ptr, rowString);
//printf("%s\n", trianglePattern[rowCount]);
//printf("%s\n", rowString);
}
}
else {
fractalTriangle(height/2, fractalLevel - 1);
printf("%s\n", trianglePattern[0]);
printf("%s\n", trianglePattern[1]);
printf("%s\n", trianglePattern[2]);
printf("%s\n", trianglePattern[3]);
}
}
Why would the array reset? I can't imagine it'd be a scope issue since the array itself is declared within the function. The purpose is to print a fractal triangle so there's no need to pass a 2D array to the function, I just want to create the pattern recursively. I'm doing this bit by bit (recursion still incomplete)- right now I'm just testing to see whether the array holds from call-to-call.
Since you define the array inside the function scope, each function creates a new 2D character array on the stack. The function will operate on that instance of the 2D character array only. By the time the recursion exists, the original un-modified 2D character array is printed.
You need to define a character array outside of the recursive function, then pass that character array (or rather a pointer to it) into the recursive function.
I don't know what your code is supposed to do, but here's what it does:
Your first (outer) call to the recursive function (from main) is:
fractalTriangle(height, 1);
Inside the function, you skip the first if statement since fractalLevel == 1 and in the else part you call the function again, recursively, with fractalLevel == 0.
That call won't go into another recursive call, and won't print anything.
Once the recursive call returns, back in the first level (the one called from main), execution continues with:
printf("%s\n", trianglePattern[0]);
printf("%s\n", trianglePattern[1]);
printf("%s\n", trianglePattern[2]);
printf("%s\n", trianglePattern[3]);
but trianglePattern was not initialized at all (immediately went into the else part), so you print garbage.
Trying to use as basic C as I can to build a list of numbers from 1-52 in a random order (deck of cards). Everything works, but all of my attempts to concat the strings and get a result end in failure. Any suggestions? NOTE: This is not homework it's something I'm using to create a game.
// Locals
char result[200] = ""; // Result
int card[52]; // Array of cards
srand(time(0)); // Initialize seed "randomly"
// Build
for (int i=0; i<52; i++) {
card[i] = i; // fill the array in order
}
// Shuffle cards
for (int i=0; i<(52-1); i++) {
int r = i + (rand() % (52-i));
int temp = card[i]; card[i] = card[r]; card[r] = temp;
}
// Build result
for (int c=0; c<52; c++) {
// Build
sprintf(result, "%s%d", result, card[c]);
// Comma?
if ( c < 51 )
{
sprintf(result, "%s%s", result, ",");
}
}
My end result is always garbled text. Thanks for the help.
You keep writing to the same position of "result".
sprintf is not going to do the appending for you.
You may consider, after each sprintf, get the return value (which is the number of char written), and increment the pointer to result buffer. i.e. something like:
(psuedo code):
char result[200];
char * outputPtr = result;
for (int c=0; c<52; c++) {
// Build
int n = sprintf(outputPtr, "%d%s", card[c], (c<51 ? "," : ""));
outputPtr += n;
}
Are we writing C++ or C? In C++, concat-ing a string is just:
string_out = string_a + string_b
…since you'd be using std::string.
Furthermore, if this is C++, the STL has a std::shuffle function.
If this is C, note that all your sprintfs aren't concatenating strings, they're just overwriting the old value.
I think, if memory serves, that sprintf will always write into the buffer starting at byte 0. This means that you would be writing the first couple of bytes over and over again with a number, then a comma, then a number. Check if your first bytes are ",[0-9]" - if so, that's your issue.
This would add a comma between each number in the result string:
// Get a pointer to the result string
char* ptr = &result[0];
for (int c = 0; c < 52; c++) {
// Add each cards number and increment the pointer to next position
ptr += sprintf(ptr, "%d", card[c]);
// Add a separator between each number
if (c < 51) {
*ptr++ = ',';
}
}
// Make sure the result string is null-terminated
*ptr = 0;