C阵列重新初始化

时间:2015-09-12 01:02:07

标签: c

我知道如何在C中初始化一个字符数组(字符串),假设数组叫做Word。我需要每次都将不同的字符串保存到数组中,我不希望显示以前的数据。无论如何我可以重新初始化阵列吗?就像我把“char Word [100];”

4 个答案:

答案 0 :(得分:0)

要将数组的所有值设置为给定值,请使用memset

memset(Word, 0, sizeof(Word));

从链接:

  

void * memset(void * s,int c,size_t n);

     

memset()函数填充内存区域的前n个字节   由s指向的常量字节c。

答案 1 :(得分:0)

定义这样的数组时:

char Word[100];

这并没有实际初始化它。此时数组的内容未定义。如果您希望数组具有某些已知内容,例如所有NUL字节,则可以使用memset

memset(Word, 0, sizeof(Word));

您可以在清除阵列时随时执行此操作。或者,您可以使用新内容覆盖当前内容。

答案 2 :(得分:0)

各种方法:

memset(Word, 0, 100); //before putting new values

OR

{ // whenever this block is entered, you get a fresh copy of 'Word'
 char Word[100] = {'\0'};
 ...
}

答案 3 :(得分:0)

继续您的评论之后,我认为将各种方法的效率快速进行10,000,000次重复测试会有所帮助,这样您就可以看到所涉及的效率。当你谈到memset代价高昂时,“代价高昂”是一个非常相对的术语。

虽然您可以担心它在设置100个char数组的所有元素(或者字符串,如果它包含以空字符结尾的一系列字符)时效率更高或更低,除非您重复任务数十亿次,否则它是在噪音。为了将所有元素设置为0,您不关心它是字符串还是字符数组。 (每次重置后,它将是一个100 0的数组或'空字符串')

让我们看一下比较:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main (void) {

    char word[100] = {0};
    char noword[100] = {0};
    char *p = word;
    double t1 = 0.0, t2 = 0.0;
    unsigned tests = 10000000;

    strcpy (word, "The string we will reset in severl ways.");
    printf ("\n word : %s\n", word);

    /* let's make word an 'empty-string' */
    *word = 0;
    printf ("\n word : %s\n", word);

    /* is the data really gone - of course not - what's left? */
    printf ("\n p + 1: %s\n", p + 1);

    /* how can we really wipe it all out? */
    memcpy (word, noword, sizeof word);
    printf ("\n p + 1: %s\n", p + 1);

    /* let's try again */
    strcpy (word, "The string we will reset in severl ways.");
    printf ("\n word : %s\n", word);
    printf (" p + 1: %s\n", p + 1);

    /* using memset */
    memset (word, 0, sizeof word);
    printf ("\n word : %s\n", word);
    printf (" p + 1: %s\n", p + 1);

    /* one more time with a loop? */
    strcpy (word, "The string we will reset in severl ways.");
    printf ("\n word : %s\n", word);
    while (*p) *p++ = 0;
    printf (" word : %s\n", word);

    /* what about efficiency, which is more efficient?
       memcpy? */
    strcpy (word, "The string we will reset in severl ways.");
    printf ("\n %u memcpy operations on word : ", tests);
    t1 = clock();
    while (tests--) memcpy (word, noword, sizeof word);
    t2 = clock();
    printf ("%lf (sec)\n", (t2 - t1)/CLOCKS_PER_SEC);

    /* memset? */
    tests = 10000000;
    strcpy (word, "The string we will reset in severl ways.");
    printf ("\n %u memset operations on word : ", tests);
    t1 = clock();
    while (tests--) memset (word, 0, sizeof word);
    t2 = clock();
    printf ("%lf (sec)\n", (t2 - t1)/CLOCKS_PER_SEC);

    /* loop? */
    tests = 10000000;
    strcpy (word, "The string we will reset in severl ways.");
    printf ("\n %u pointer operations on word: ", tests);
    t1 = clock();
    while (tests--) {
        p = word;
        while (*p) *p++ = 0;
    }
    t2 = clock();
    printf ("%lf (sec)\n", (t2 - t1)/CLOCKS_PER_SEC);

    return 0;
}

第一系列操作显示使用word作为刺痛的效果。是否只需使用0进行简单分配即可将第一个字符设置为*word = 0;,或者使用0memcpy将所有字​​符设置为memset,结果就是相同。该字符串是空字符串

在简单地将第一个字符设置为0的情况下,所有其他字符仍然存在于数组中,它们不会被视为字符串的一部分 {{1字符串以遇到的第一个word字符结束(这里是赋值后数组的第一个字符)

输出 - 对字符串的影响

null-terminating

输出 - 效率

关于效率,当然这取决于你重置字符串的次数。但是,无论使用哪种方法,您都可以在几分之一秒内轻松完成10,000,000次重置。因此,对于所有实际目的,当您谈论获取输入或向人类呈现输出时,这无关紧要。但是,当它取决于编译器请求的优化级别时,要了解各种方法的相对效率。

例如,使用默认优化($ ./bin/str_reset word : The string we will reset in severl ways. word : p + 1: he string we will reset in severl ways. p + 1: word : The string we will reset in severl ways. p + 1: he string we will reset in severl ways. word : p + 1: word : The string we will reset in severl ways. word : ),10,000,000次重置重复次数为:

-O0

10000000 memcpy operations on word : 0.175195 (sec) 10000000 memset operations on word : 0.170872 (sec) 10000000 pointer operations on word: 0.058730 (sec) memcpy在1000万次迭代中花费的时间少于1/10秒。

完全优化(memset)后,时间是:

-0fast

所以希望,这已经向您展示了可用的各种方法中涉及的相对 10000000 memcpy operations on word : 0.077134 (sec) 10000000 memset operations on word : 0.137333 (sec) 10000000 pointer operations on word: 0.026722 (sec) 。在很大程度上,除非你多次调用各种函数,否则你不必担心它。