gpt4 book ai didi

c - 函数不改变值

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

当我调用indexs()函数并且函数完成时,值不会改变。当函数index()运行时它们被改变,我该怎么做才能更新这么多值......

void indexs(int i , char *str,int indexStart,int indexEnd,int wordlen)
{
int words = 1;
int len = strlen(str);
for (int j = 0; j < len; j++)
{
if (str[j] == ' ')
words++;
}
if (i > 0 && i <= words)
{
words = 1;
int k = 0;
while (words != i)
{
if (str[k] == ' ')
++words;
++k;
++wordlen;
if (words == i)
{
indexStart = k;
while (str[k] != ' ' && k != (len-1))
{
wordlen++;
k++;
}
indexEnd = k;
}
}
}
else
{
printf("The index dosen't exsist\n");
}
}
char delete(char *str)
{

int i, indexStart = 0, indexEnd = 0, wordlen = 0;
printf("Enter the index of the word that you want to remove: ");
scanf("%d", &i);
indexs(i, str,indexStart,indexEnd,wordlen);
......
}

最佳答案

在 C 中,如果你想从函数中传递数据,要么返回它,要么传递一个指向该变量的指针,如下所示:

void indexs(int i , char *str,int *pIndexStart,int *pIndexEnd,int wordlen)
{
...
*pIndexStart = 0; // Set the *contents* of the pointer, by putting a * before it
}

并这样调用它:

int MyVariable, MyOtherVariable;
indexs(0, "hi", &MyVariable, &MyOtherVariable, 2);

& 符号将指针传递给变量而不是变量值。

这里有一个网站可以告诉您更多相关信息:http://www.thegeekstuff.com/2011/12/c-pointers-fundamentals/

关于c - 函数不改变值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46529512/

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