如何使用整数数组作为C中字符数组的索引?
因此,我试图将计算得到的位置存储到整数数组中,然后使用这些位置(整数数组的元素)作为字符数组的位置进行某种比较 以下是示例代码:如何使用整数数组作为C中字符数组的索引?,c,C,因此,我试图将计算得到的位置存储到整数数组中,然后使用这些位置(整数数组的元素)作为字符数组的位置进行某种比较 以下是示例代码: int arrayposition[10]; char arrayword[10]; char string[10]; ... . . // I want to do a strcmp to compare string and arrayword //suppose for (int i = 0;;) { if (strcmp(arrayword[i], s
int arrayposition[10];
char arrayword[10];
char string[10];
...
.
.
// I want to do a strcmp to compare string and arrayword
//suppose
for (int i = 0;;) {
if (strcmp(arrayword[i], string[arrayposition])) { //Obviously this is not possible( array within an
// array)
//do something
}
}
使用arrayposition
作为字符串索引的最佳方法是什么。
例如:
arrayposition[2]={1,3,5}代码>这可能是答案:
const char *arrayword[10] = { "word1", "word2", ... };
if(strncmp(arrayword[i], string + arrayposition, strlen[arrayword + i]))
这会将arrayword+i中的整个字符串与从arrayposition开始的字符串进行比较。字符串为char*类型,arrayposition为int*类型。我们如何对这两个函数执行二进制操作?@technextgen:arrayposition应该是int类型,或者缺少循环,应该是arryposition[j]
。