Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char ** read_text(char buf[], char * addr[], int size_buf, int size_addr) {
  6.     int lines_read = 0, c, i;
  7.     char line[size_buf];
  8.     while (lines_read < size_addr) { // read while there is space
  9.       addr[lines_read] = malloc(size_buf);
  10.       for (; i < size_buf && ( (c = getchar()) != '\n'); i++ ) { // put character in buffer
  11.         buf[i] = c;
  12.       }
  13.       buf[i] = '\0'; i = 0;
  14.       addr[lines_read] = realloc (buf, size_buf);
  15.       lines_read += 1;
  16.     }
  17.     return addr;
  18. }
  19.  
  20. int main() {
  21.   char buf[10]; // x chars.
  22.   char * addr[5]; // 5 lines;
  23.   read_text(buf, addr, 10, 5);
  24.   printf("%s\n", addr[1]);
  25.   return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement