asaf asaf - 2 years ago 135
C Question

Function does not change values

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);
......
}

Answer Source

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/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download