这是"字符串副本"不安全?

时间:2017-03-05 10:08:18

标签: c string for-loop multidimensional-array copy

提前抱歉格式。我有一个程序,它将采取用户输入字符串并按我想要的方式对它们进行排序。但是,我已经看到很多关于strncopy是否安全的问题以及strcpy.的其他变体我决定实施类似的东西,但我想知道这是不是很糟糕,或不安全的编程。为了简化这一点,我们假设用户输入了字符串"放置在坚固的表面上"到string[N]

#define N 100

char string[N], stringSort[N][N], stringCopy[N][N], wordLength[4];
int wordCount = 4; //Just going by example string

我们假设string[N]数组按此分类为stringSort[N][N]

 0. Place\0 //Note: I'm not using strtok.
 1. on\0
 2. firm\0
 3. surface\0

wordLength[4]数组会跟踪每个stringSort[N][N]索引中的字符数。

0. 6 //Note: I'm not using strnlen to determine the amount
1. 3
2. 5
3. 8

我的循环"字符串复制"如下:

for (i = 0; i < wordCount; i++)
    for (j = 0; j < wordLength[i]; j++)
        stringCopy[i][j] = stringSort[i][j];

那么我的循环有多糟糕/不安全?

0 个答案:

没有答案
相关问题