Why this mikroC PRO PIC code not working?

Status
Not open for further replies.

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,524
Helped
540
Reputation
1,078
Reaction score
524
Trophy points
1,393
Visit site
Activity points
0
Why this mikroC PRO PIC code not working?

It has to extract data between 2nd space char and 1st encounter of 'M' from there for first function call and for second function call it has to get data between 3rd space and first appearance of 'G'. It is working fine for 1st function call but dosen't work for subsequent calls.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
char teststr[] = "AF/CCT1456 12;22pp XX.XXM YY.YYG"
 
char tempBuff[20], temp[20];
 
void SplitBuff(char *to, char *from, char delimit1, unsigned char delimit1Count, char delimit2, unsigned char delimit2Count, int lenBuff, char *tmpBuff){
          unsigned delimit1Cnt = 0, delimit2Cnt = 0;
          unsigned char tmp[2];
 
          tmp[0] = delimit1;
          tmp[1] = '\0';
 
          strcpy(to, strtok(from, tmp));
          while(++delimit1Cnt != delimit1Count){
                   strcpy(to, strtok(0, tmp));
          }
          memset(to, '\0', lenBuff);
          tmp[0] = delimit2;
          strcpy(to, strtok(0, tmp));
          while(++delimit2Cnt != delimit2Count){
                  strcat(to, strtok(0, tmp));
          }
 
          strcpy(tmpBuff, to);
}
 
void main() {
 
    while(1){
 
        SplitBuff(&tempBuff, &teststr, ' ', 2, 'M', 1, 20, &temp);
        SplitBuff(&tempBuff, &teststr, ' ', 3, 'G', 1, 20, &temp);
    }
}

 
Last edited:

Try initializing teststr[]= "AF/CCT1456 12;22pp XX.XXM YY.YYG" again inside while (in main function).

I mean
Code:
void main() {
 
    while(1){
 
        SplitBuff(&tempBuff, &teststr, ' ', 2, 'M', 1, 20, &temp);
        SplitBuff(&tempBuff, &teststr, ' ', 3, 'G', 1, 20, &temp);
        teststr[]= "AF/CCT1456 12;22pp XX.XXM YY.YYG"
    }
}
 

How does it solve the problem? I mentioned it works when the function is called 1st time but subsequent calls doesn't work. Anyway mikroC doesn't allow to initialize arrays inside functions. It gives "Invalid expression" error.

Even this code behaves the same way.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
char teststr[] = "AF/CCT1456 12;22pp XX.XXM YY.YYG"
char tempBuff[20], temp[20];
 
void SplitBuffer(char *to, char *from, char delimit1, unsigned char delimit1Count, char delimit2, unsigned char delimit2Count, char *(*mystrcat)(char *, char *), char *(*mystrcpy)(char *, char *), char *(*strtokn)(char *, char *), void *(*mymemset)(void *, char, int), int lenBuff, char *tmpBuff) {
          unsigned delimit1Cnt = 0, delimit2Cnt = 0;
          unsigned char tmp[2];
 
          tmp[0] = delimit1;
          tmp[1] = '\0';
          (*mystrcpy)(to, (*strtokn)(from, tmp));
          while(++delimit1Cnt != delimit1Count){
                   (*mystrcpy)(to, (*strtokn)(0, tmp));
          }
          (*mymemset)(to, '\0', lenBuff);
          tmp[0] = delimit2;
          (*mystrcpy)(to, (*strtokn)(0, tmp));
          while(++delimit2Cnt != delimit2Count){
                  (*mystrcat)(to, (*strtokn)(0, tmp));
          }
 
          (*mystrcpy)(tmpBuff, to);
 
}
 
void main() {
 
    while(1){
        SplitBuffer(&tempBuff, &teststr, ' ', 2, 'M', 1, &strcat, &strcpy, &strtok, &memset, 20, &temp);
                SplitBuffer(&tempBuff, &teststr, ' ', 3, 'G', 1, &strcat, &strcpy, &strtok, &memset, 20, &temp);
    }
}

 
Last edited:

I don't get any error for my code. I am attaching my mikroC project files. Debug and see it yourself. I will explain according to code in post #3. The 1st time the function executes the result is as expected but when the function is called 2nd time everything works fine till 11th line in post#3 code. After that no data is copied to the string pointed to by to (string).

If the 1st function call is commented out and 2nd function call is executed then result is again fine. So, function works only once.


Edit: See attached video. It shows code debugged. See what happens during 1st call and 2nd call.
 

Attachments

  • Function Test.rar
    48.9 KB · Views: 67
  • func dbg.rar
    552.4 KB · Views: 53
Last edited:

When passing or storing a pointer to the first element of an array, the name/symbol of an array contains the address of the first element.

Therefore, use of the address operator (&) is not required:

Code:
char a[20] = "Testing";

char * pstr;

pstr = a;

// pstr[3] now contains  't', pstr[0] now contains to 'T'


BigDog
 

Post #5 is updated. Video of debug attached.

@bigdogguru

What am I doing wrong? Should I remove & in then function call arguments and try?
 

Double check your string array sizes, the receiving string array must be large enough to contain the resulting string.

See your other thread.

BigDog
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…