简体   繁体   English

用C中的Last覆盖的数组元素

[英]Array Elements Overwritten with Last in C

I'm trying to create a program which takes in a set number of strings (the user is asked to put in the number of strings they will enter), once it has these strings, they are placed in an array, using dynamic memory. 我正在尝试创建一个程序,该程序采用一定数量的字符串(要求用户输入要输入的字符串数),一旦有了这些字符串,就可以使用动态内存将它们放入数组中。

The ouput would be something like this: 输出将是这样的:

# of Strings: 3
Cat
Dog
Elephant 

Cat
Dog
Elephant

Heres a snippet of my code, after I have the number of strings. 在获得字符串数之后,这是我的代码的一部分。

sptr=malloc(sizeof(char*)*nStrings);

for(i=0;i<nStrings;i++)
{
    scanf("%s",string);
    length=strlen(string);
    sptr[i]=malloc(sizeof(char)*length);
    sptr[i]=string;
}

Where sptr is the array I'll access to output the strings. 其中sptr是数组,我将访问该数组以输出字符串。 So it's an array of pointers which then point to individual strings (or other arrays of characters, if you'd prefer to think of it that way). 因此,它是一个指针数组,然后指向单个字符串(或其他字符数组,如果您更喜欢这样考虑的话)。

Lets say there are two strings. 可以说有两个字符串。 I allocate memory for two pointers, Then in the first pointer, i scan in a string, i find the length of the string, i allocate memory the size of the string and i equal the pointer to the string. 我为两个指针分配了内存,然后在第一个指针中,我扫描了一个字符串,找到了字符串的长度,我为内存分配了字符串的大小,并且我等于该字符串的指针。 This all works dandy, and if I were to put a printf() right after that last line, it will work. 这一切都很好,如果我将printf()放在最后一行的后面,它将起作用。 The problem i face is, if lets say there are 3 strings, each time through sptr[i] is assigned correctly, but then outside of that block, all of the indicies of sptr are = to the last string i put in, and I have no idea why. 我面临的问题是,如果说有3个字符串,则每次通过sptr [i]都正确分配了一个字符串,但是在该块之外,所有sptr的索引都等于=到我输入的最后一个字符串,而我不知道为什么。

If you could help me out I'd appreciate it. 如果您能帮助我,我将不胜感激。 Thanks. 谢谢。

sptr=malloc(sizeof(char*)*nStrings);

for(i=0;i<nStrings;i++)
{
    scanf("%s",string);
    sptr[i]=strdup(string);
}

I assume the variable string has enough memory to keep the read strings. 我假设变量字符串具有足够的内存来保留读取的字符串。

The error occured because you set the pointer to point to the string variable. 发生错误是因为您将指针设置为指向字符串变量。

You need to allocate 1 character extra for the null terminator: 您需要为空终止符额外分配1个字符:

sptr[i]=malloc(sizeof(char)*(length+1));

Also, you need to copy the string into the newly allocated memory: 另外,您需要将字符串复制到新分配的内存中:

strcpy(sptr[i], string);

There are 2 problems in your code: you don't allocate enough memory. 您的代码中有两个问题:您没有分配足够的内存。 Should be length + 1 because of the ending \\0 in a string. 由于字符串的结尾\\ 0,因此应为长度+ 1。 Secondly you should use strcpy to copy the string to your allocated memory. 其次,您应该使用strcpy将字符串复制到分配的内存中。 Lookup the strdup function that handles both. 查找同时处理两者的strdup函数。

strlen doesn't account for the zero termination, you need to add one. strlen不能说明零终止,您需要添加一个。 But mainly you need to copy the string into the memory you allocate. 但是主要是您需要将字符串复制到分配的内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM