/tmp/cctZxVzL.o:在函数‘main’中:
6_4.c:(.text+0xfa):对‘strnlen_s’未定义的引用
6_4.c:(.text+0x128):对‘strnlen_s’未定义的引用
编写的程序如下:
代码: 全选
// Program 6.4 Joining strings
2 #define __STDC_WANT_LIB_EXT1__ 1 // Make optional cersions of functions available
3 #include <string.h> // Header for string functions
4 #include <stdio.h>
5
6 int main(void)
7 {
8 char preamble[] = "The jole is:\n\n";
9 char str[][40] = {
10 "My dog hasn\'t gor any nose.\n","How does your dog smell then?\n","My dog smells horrible.\n"};
11
12 unsigned int strCount = sizeof(str)/sizeof(str[0]);
13
14 // Find the total length of all the strings in str
15 unsigned int length = 0 ;
16 for(unsigned int i = 1;i < strCount ; ++i)
17 length += strnlen_s(str[i],sizeof(str[i]));
18
19 // Create array to hold all strings combined
20
21 char joke[length + strnlen_s(preamble,sizeof(preamble))+1];
22
23 if(strncpy_s(joke,sizeof(joke),preamble,sizeof(preamble)))
24 {
25 printf("Error copying preamble to joke.\n");
26 return 1;
27 }
28
29 // Concatenate strings in joke
30 for(unsigned int i=0;i<strCount ; ++i)
31 {
32 if(strncat_s(joke,sizeof(joke),str[i],sizeof(str[i])))
33 {
34 printf("Error copuing string str[%u].",i);
35 return 2;
36 }
37 }
38 printf("%s",joke);
39 return 0;
40 }