Created
January 16, 2025 14:55
-
-
Save webgtx/03402606ff51d4d2032868589463799a to your computer and use it in GitHub Desktop.
Prompt a string in C with no buffer limits, by using dynamic string / dynamic memory allocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <ctype.h> | |
#include <stdlib.h> | |
typedef struct node { | |
char ch; | |
struct node* next; | |
} node; | |
node* type(node* next); | |
node* print(node* current); | |
int main() { | |
printf("TYPE! TYPE! TYPE! "); | |
node* head = NULL; | |
node* message = type(head); | |
print(message); | |
} | |
node* print(node* current) { | |
if (!current) return NULL; | |
print(current->next); putchar(current->ch); | |
free(current); | |
return NULL; | |
} | |
node* type(node* next) { | |
node* block = malloc(sizeof(node)); | |
block->next = next; | |
block->ch = getchar(); | |
return (isalpha(block->ch)) ? type(block) : block; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment