-
Notifications
You must be signed in to change notification settings - Fork 2
/
2d_array_string_dma.cpp
97 lines (73 loc) · 1.78 KB
/
2d_array_string_dma.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/****************************************************************************
File name: 2d_array_string_dma.cpp
Author: babajr
*****************************************************************************/
/*
Program to allocate memory dynamically to 2D array and store the strings as a row in
that array.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define COL_SIZE 10
#define ROW_SIZE 3
/*
Helper API to read the strings.
*/
void read_string(char c[])
{
int i = 0;
while(i < COL_SIZE)
{
c[i] = getchar();
if(c[i] == '\n')
break;
i++;
}
c[++i] = '\0';
}
/*
Helper API to get the data from the user through command line.
*/
// void get_data_user(char c[][COL_SIZE])
void get_data_user(char **c)
{
for(int i = 0; i < ROW_SIZE; i++)
{
printf("Enter String: ");
read_string(c[i]);
}
}
/*
Helper API to print the data of 2D array.
*/
// void print_data(char c[][COL_SIZE])
void print_data(char **c)
{
for(int i = 0; i < ROW_SIZE; i++)
printf("%s", c[i]);
// printf("%s\n", c[i]);
}
/*
Create 3 * 10 2D array. COL_SIZE = 10, ROW_SIZE = 3
*/
int main(void)
{
char **c = NULL;
c = (char **)malloc(sizeof(char) * ROW_SIZE);
// c[0] = (char *)malloc(sizeof(char) * COL_SIZE);
// c[1] = (char *)malloc(sizeof(char) * COL_SIZE);
// c[2] = (char *)malloc(sizeof(char) * COL_SIZE);
for(int i = 0; i < ROW_SIZE; i++)
{
c[i] = (char *)malloc(sizeof(char) * COL_SIZE);
}
// specifying intput directly
// strncpy(c[0], "hello", sizeof("hello"));
// strncpy(c[1], "hello", sizeof("hello"));
// strncpy(c[2], "hello", sizeof("hello"));
// get the input directly from the ser.
get_data_user(c);
print_data(c);
return 0;
}