gpt4 book ai didi

c - 将数组中的元素更改为0-C编程

转载 作者:行者123 更新时间:2023-11-30 19:25:08 24 4
gpt4 key购买 nike

我的问题是我试图将数组中的一个元素设为0,然后在另一个函数中打印先前为0的元素。问题是显示的是数字的位置0,而不是数字本身。示例:10 3 4 2 5更改为10 3 4 05。显示为先前为0的数字将是4,而不是2。

我有2个源文件。在第一个源文件中,我具有这两个函数,第一个函数应该将数组中的一个元素更改为0,而另一个函数应该显示先前为0的元素是什么。

int *shoot(int toShoot[]) {

printf(SPLIT);
printf("\n\nZeroing out one element from the list:\n\n");

int i, toZero, temp;

srand(time(NULL)); //Using time to increase randomness, since the value of time constantly changes

temp = rand() % N;
toZero = temp;
toShoot[toZero] = 0;

for (i = 0; i < N; ++i) {
printf("%d ", toShoot[i]); //Printing the array with the zeroed element
}
temp += 1;
printf(SPLIT);
return (int *) temp;
}

void target(int *iZeroed) {
printf(SPLIT);
printf("\n\nZeroed out value was: ");
printf("%d ", iZeroed); //Printing the previously zeroed element
printf(SPLIT);
}


在第二个源文件中,我有这个名称,它在其中调用要在菜单中使用的方法。
int * array,* iZeroed;在方法开始时声明。我没有发布完整的方法,因为它太长了。

            case '3':
iZeroed = shoot(array);
break;
case '4':
target(iZeroed);
break;

最佳答案

您正在单个阵列上执行所有操作。尝试制作一个副本数组,然后将该副本数组发送到shoot()函数,该函数将返回零值索引。然后,您可以使用此索引显示原始数组中的值。

关于c - 将数组中的元素更改为0-C编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59247523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com