-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathret_str_arr.c
44 lines (36 loc) · 1 KB
/
ret_str_arr.c
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
/* vim:ts=4:sw=4:et:so=10:
*
* ret_str_arr.c
* Example of how a function can return array wrapped in a structure.
*
* Description:
* This code is a demonstration for a potentially useful C feature, not a
* real world solution to an actual problem.
*
* Build:
* $ gcc -o ret-str-arr ret_str_arr.c
*
* Run
* $ ./ret-str-arr
*
*/
#include <stdio.h>
#include <string.h>
#define STR_ARR_SIZE 10
struct str_incl_array {
char ca[STR_ARR_SIZE];
} g_str_array;
struct str_incl_array ret_str_arr(const char *s);
/* call a function that returns a structure around array and display */
int main(int argc, char *argv[]) {
struct str_incl_array a_str_array;
a_str_array = ret_str_arr("hello");
a_str_array.ca[0] = 'H';
printf("array in structure: %s\n", a_str_array.ca);
return 0;
}
/* ret_str_arr: return array encapsulated in a structure */
struct str_incl_array ret_str_arr(const char *s) {
strncpy(g_str_array.ca, s, STR_ARR_SIZE);
return g_str_array;
}