This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
5
Hey Guys, wanted some feedback on something. If posting this is against the rules let me know and I will take it down.
Post Flair (click to view more posts with a particular flair)
Post Body
I am only week 4 so not sure if this is a spoiler for a future problem set, (if it is let me know and I will delete) but I wanted to recreate my own get_string function because wow it was useful.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int string_memory = 1;
char *string = malloc(sizeof(char) * string_memory);
if (string == NULL)
{
printf("error 1.\n");
return 1;
}
char c;
int string_leangth = 0;
int con = 0;
printf("input: ");
while (con == 0)
{
if (string_leangth == string_memory)
{
string_memory ;
string = realloc(string, sizeof(char) * string_memory);
if (string == NULL)
{
printf("error 2.\n");
return 2;
}
}
scanf("%c", &c);
string[string_leangth] = c;
if (c == '\n')
{
string[string_leangth] = '\0';
break;
}
string_leangth ;
}
printf("output: %s\n", string);
free(string);
return 0;
}
Anyway any improvements or dumb things I may have done I would love to hear, please and thank you.
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cs50/commen...