help with pointers in this fucntion

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 12
Reputation: chelo77 is an unknown quantity at this point 
Solved Threads: 0
chelo77 chelo77 is offline Offline
Newbie Poster

help with pointers in this fucntion

 
0
  #1
Apr 17th, 2006
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


  1. void classify(char *cInput ,float *fstats)
  2. {
  3. int j;
  4. int iResult =0;
  5. float * pstatbeg;
  6. char * pInputbeg;
  7.  
  8. pInputbeg = cInput;
  9. pstatbeg = fstats;
  10.  
  11. for (j=0; j<=6; j++)
  12. fstats[j]=0;
  13.  
  14. for ( ; *cInput !='\0'; cInput = cInput + 1)
  15. {
  16.  
  17. if (isalpha(*cInput) != 0)
  18. {
  19. if (isupper(*cInput) !=0)
  20. fstats [3]++;
  21. else
  22. fstats[4]++;
  23. }
  24.  
  25. if(isdigit(*cInput) !=0)
  26. {
  27. if( (*cInput%2) == 0)
  28. {
  29. fstats[2]++;
  30. iResult = *cInput - '0';
  31. *(fstats +6) = (*(fstats + 6) + iResult);
  32. }
  33.  
  34. else
  35. {
  36. fstats[1]++;
  37. iResult = *cInput - '0';
  38. *(fstats + 5) = (*(fstats + 5) + iResult);
  39. }
  40. }
  41.  
  42. fstats[0]++;
  43. fstats = pstatbeg; //point back to beginning
  44. }
  45.  
  46. if (*(fstats +2) ==0)
  47. *(fstats +6) = 0;
  48. else
  49. *(fstats + 6) = *(fstats+6)/(*(fstats +2));
  50.  
  51. if (*(fstats + 1) ==0)
  52. *(fstats + 5) =0;
  53. else
  54. *(fstats + 5) = *(fstats + 5) /(*(fstats +1));
  55. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,412
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help with pointers in this fucntion

 
0
  #2
Apr 17th, 2006
[quote]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
[/code]

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
  1. fstats[index value]++;

But if you really want to do it differently, here is one way
  1. int main(int argc, char* argv[])
  2. {
  3. int i;
  4. float* array = (float*)malloc(10 * sizeof(float));
  5. for(i = 0; i < 10; i++)
  6. array[i] = 0;
  7.  
  8. for(i = 0; i < 10; i++)
  9. {
  10. for(int j = 0; j <= i; j++)
  11. {
  12. (*(array+i))++;
  13. }
  14. }
  15.  
  16. for(i = 0; i < 10; i++)
  17. printf("array[%d] = %f\n",i, *(array+i));
  18.  
  19. return 0;
  20. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC