编程(学校)的新手,我对这是什么/为什么会发生有点困惑。
我有一个循环遍历一个元素数组,对于每个元素,我都取数组的整数,使用函数 getelementsymbol 将其转换为字符,并使用 strcat 附加到我的临时数组。我遇到的问题是我的临时数组的元素包含继续它的元素的残差。这是我的代码片段。我收到的输出是这样的:
单词1
word1word2
word1word2word3
char* elementsBuildWord(const int symbols[], int nbSymbols){
/* ROLE takes a list of elements' atomic numbers and allocate a new string made
of the symbols of each of these elements
PARAMETERS symbols an array of nbSymbols int which each represent the atomic number
of an element
nbSymbols symbols array's size
RETURN VALUE NULL if the array is of size <= 0
or if one of the symbols is not found by our getElementSymbol function
other the address of a newly allocated string representing the concatenation
of the names of all symbols
*/
char s1[MAX_GENERATED_WORD_LENGTH];
int y;
char *s2;
size_t i;
for (i = 0; i < nbSymbols; i++){
y = symbols[i];
s2 = getElementSymbol(y);
strcat(s1, s2);
}
printf("%s ", s1);
}