-2

when i call the indexs() function and when the function is done the values are not changed. when the function index() run they are changed, what can i do to update so many values...

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);
......
}
Rob
  • 14,746
  • 28
  • 47
  • 65
asaf
  • 119
  • 2
  • 10
  • 1
    What values? What is this supposed to do? How do you call it? – Eugene Sh. Oct 02 '17 at 16:15
  • @WendingPeng: C does not have "pass by reference" (like C++ has), only "pass by value". However, OP could pass the address of some variable. – Basile Starynkevitch Oct 02 '17 at 16:23
  • Possible duplicate of [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – Andrew Henle Oct 02 '17 at 16:52

2 Answers2

1

In C if you want to pass data out of a function either return it or pass a pointer to that variable in, like this:

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
}

and call it like this:

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

The & symbol passes the pointer to the variable in instead of the variable value.

Here's a website that tells you more about it: http://www.thegeekstuff.com/2011/12/c-pointers-fundamentals/

noelicus
  • 14,468
  • 3
  • 92
  • 111
0

As you are calling your function by Call By value thus values are getting updated only in function indexs() not in calling function delete(). In order to reflect changes in calling function you need to pass those parameters by passing pointer (Call by reference).

Arun Pal
  • 687
  • 7
  • 28