Any memory that you allocate should be also freed and since your returning a pointer to the allocated memory, you can use that to free it.
Also, why all the comments its really distracting.
gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0
Also, why all the comments its really distracting.
Because good programmers comment their code so others can follow it.
sorry its an assignment from my university so I have to write all that comments. I hate it too so please bear with me :/
Don't apologize. Keep it up. Comments are critical if you want to make programming a profession.
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36
My point on the comments refers to commenting something like this
char character = getchar(); //get a character from stdin
getchar() is a function from the standard library. Its well documented, so I consider commenting its functionality redundant. That said, if your Prof wants comments then give him/her comments.
gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0
And your example
#include <stdio.h>
#include <stdlib.h>
char * toString(char character)
{
char *x = malloc(2 * sizeof(char));
x[0] = character;
x[1] = '\0';
return x;
}
int main(int argc, char**argv)
{
int ca;
char *ans = NULL;
fputs("Enter a character->", stdout);
ca = fgetc(stdin);
ans = toString(ca);
fprintf(stdout, "%s\n", ans);
free(ans);
ans = NULL;
return 0;
}
gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0
My point on the comments refers to commenting something like this
char character = getchar(); //get a character from stdin
getchar() is a function from the standard library. Its well documented, so I consider commenting its functionality redundant. That said, if your Prof wants comments then give him/her comments.
That may have been your point, but you included
/* Function prototypes */
void process(char input);
void update(char words[][51], int position);
char * toString(char character);
/* the global variables */
char words[100][51]; //the array of the words inside the text
int lastWordPos = 0; //position of the last word in array words
char tempWord[51] = ""; //temporary array for storing a word
char tempDigit[51] = "";//temporary array for storing a number
while(character != '0') //as long as the character from stdin is not 0, keep looping
strcpy(words[lastWordPos], tempWord); //add the word to the word list
strcpy(tempWord, ""); //reset tempWord, so that it can be used again at the next loops, if needed
lastWordPos++; //increment the position of the word list
in your remark. If you didn't mean these, you should have been more explicit. As a veteran programmer, I understood your remark as "don't use comments". How will noobs interpret it?
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36
Question Answered as of 2 Years Ago by
gerard4143,
WaltP
and
navedalam