| | |
help with pointers in this fucntion
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2006
Posts: 12
Reputation:
Solved Threads: 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
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
C Syntax (Toggle Plain Text)
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)); }
[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
But if you really want to do it differently, here is one way
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
C Syntax (Toggle Plain Text)
fstats[index value]++;
But if you really want to do it differently, here is one way
C Syntax (Toggle Plain Text)
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; }
![]() |
Similar Threads
- Parameter lists for fucntion pointers?? (C)
- Linked List using pointers (C++ ADT) (C++)
- Why can't I use Pointers to point to a Enumurated Constant (C++)
Other Threads in the C Forum
- Previous Thread: need best free music for computers
- Next Thread: templates and maps
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch char cm copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fflush fgets file fork forloop framework frequency getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research scanf scripting segmentationfault sequential shape socket socketprograming stack standard string systemcall testing threads turboc unix user voidmain() wab windows.h






