Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- char *scan_line(char *buffer, int buffer_size)
- {
- char *p = buffer;
- int count = 0;
- do {
- char c;
- scanf("%c", &c); // scan a single character
- // break on end of line, string terminating NUL, or end of file
- if (c == '\r' || c == '\n' || c == 0 || c == EOF) {
- *p = 0;
- break;
- }
- *p++ = c; // add the valid character into the buffer
- count++;
- } while (count < buffer_size - 1); // don't overrun the buffer
- // ensure the string is null terminated
- buffer[buffer_size - 1] = 0;
- return buffer;
- }
- char **scan(char **mas, int n, int len)
- {
- mas = (char **)malloc(n * sizeof (char *)); // выделение памяти под массив указателей
- for (int i = 0; i < n; i++) {
- mas[i] = (char *)malloc(len * sizeof (char) + 1); // выделение памяти под cтроку
- scan_line(mas[i], len);
- }
- return mas;
- }
- void print(char **mas, int n, int m)
- {
- printf("Массив значений:\n");
- for (int i = 0; i < n; i++) {
- printf("%s\n", mas[i]);
- }
- }
- int main()
- {
- int n, len;
- printf("Введите число строк:");
- scanf("%d", &n);
- printf("Введите длину строк:");
- scanf("%d", &len);
- char buf[len];
- scan_line(buf, 2); //считываем перенос строки
- char **mas = 0;
- mas = scan(mas, n, len);
- print(mas, n, len);
- for (int i = 0; i < n; i++) {
- free(mas[i]);
- }
- free(mas);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement