Skip to content

Instantly share code, notes, and snippets.

@webgtx
Created January 16, 2025 14:55
Show Gist options
  • Save webgtx/03402606ff51d4d2032868589463799a to your computer and use it in GitHub Desktop.
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
#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