0

In this function i am having problems with pointers, syntax wise this would not work ex (fstats +1)++; In this function i use fstats floater array to hold float values, now as in the functions i use
fstats[index value]++, now i'm just using pointers how would i do this??
i thought *(fstats +1)++ would increment the second position of fstats array because fstats points to the first postion but syntax wise this does not work. I am currently placing the certain index value accrodingly, kinda like cheating. How can i do this with just pointers like
fstats +1...fstats+5 and so on, want to increment the value by one in each index accordingly

void classify(char *cInput ,float *fstats)
{
	int j;
	int iResult =0;
	float * pstatbeg;
	char * pInputbeg;

	pInputbeg = cInput;
	pstatbeg = fstats;

	for (j=0; j<=6; j++)
		fstats[j]=0;
	
	for ( ; *cInput !='\0'; cInput = cInput + 1)
	{

			if (isalpha(*cInput) != 0)
			{
				if (isupper(*cInput) !=0)
					fstats [3]++;
				else
					fstats[4]++;
			}

			if(isdigit(*cInput) !=0)
			{
				if( (*cInput%2) == 0)
				{
					fstats[2]++;
					iResult = *cInput - '0';
					*(fstats +6) = (*(fstats + 6) + iResult);
				}

				else 
				{
					fstats[1]++;
					iResult = *cInput - '0';
					*(fstats + 5) = (*(fstats + 5) + iResult);
				}
			}

			fstats[0]++;
			fstats = pstatbeg; //point back to beginning
	}

	if (*(fstats +2) ==0)
		*(fstats +6) = 0;
	else 
		*(fstats + 6) = *(fstats+6)/(*(fstats +2));

	if (*(fstats + 1) ==0)
		*(fstats + 5) =0;
	else
		*(fstats + 5) = *(fstats + 5) /(*(fstats +1));
}
2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by Ancient Dragon
0

fstats[index value]++, now i'm just using pointers how would i do this??

I thought *(fstats +1)++ would increment the second position of fstats array because fstats points to the first postion but syntax wise this does not work. I am currently placing the certain index value accrodingly, kinda like cheating. How can i do this with just pointers like fstats +1...fstats+5 and so on, want to increment the value by one in each index accordingly

I do it the same way you did before -- just because the array is allocated dynamically doesn't mean you have to learn different pointer operations

fstats[index value]++;

But if you really want to do it differently, here is one way

int main(int argc, char* argv[])
{
    int i;
    float* array = (float*)malloc(10 * sizeof(float));
    for(i = 0; i < 10; i++)
        array[i] = 0;

    for(i = 0; i < 10; i++)
    {
        for(int j = 0; j <= i; j++)
        {
            (*(array+i))++;
        }
    }

    for(i = 0; i < 10; i++)
        printf("array[%d] = %f\n",i, *(array+i));

    return 0;
}

Edited by __avd: fixed formatting.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.