Jump to content

C beginner: functions, parameters, character arrays

- - - - -

  • Please log in to reply
3 replies to this topic

#1
jan_olieslagers

jan_olieslagers

    Newbie

  • Members
  • Pip
  • 2 posts
This was to get some hands-on experience in the use of functions, and might eventually serve in parsing NMEA data from a GPS receiver. It seems to work as intended, because the correct number of characters are parsed. However, no output is produced. I suppose I am making some dumb beginner's error and shall be grateful for any pointer, TIA!

[screen]
First tests with commascan
Scanning at 0
Read 3 characters
Buffer as read:
After first scan:
Scanning at 4
Read 2 characters
Buffer as read:
After second scan:
Scanning at 7
Read 5 characters
Buffer as read:
After third scan:
[/screen]

/* commascan */
#include <stdio.h>
#include <string.h>
int main()
{
printf("First tests with commascan\n");
char inbuf[] = "abc,de,fGHIJ,klmNO,1,22,333,4444" ;
char outbuf[10] ;

int rd_ptr,rd_cnt ;

rd_ptr=0;

rd_cnt=commascan(rd_ptr,inbuf,outbuf);
rd_ptr=rd_ptr+rd_cnt+1;
printf("After first scan: %s\n",outbuf);

rd_cnt=commascan(rd_ptr,inbuf,outbuf);
rd_ptr=rd_ptr+rd_cnt+1;
printf("After second scan: %s\n",outbuf);

rd_cnt=commascan(rd_ptr,inbuf,outbuf);
rd_ptr=rd_ptr+rd_cnt+1;
printf("After third scan: %s\n",outbuf);

return (0);
}

/* define  function COMMASCAN  */
int commascan(scan_rd_ptr, scan_inbuf, scan_outbuf)
int scan_rd_ptr;
char scan_inbuf[],scan_outbuf[];
{
int wr_ptr=0 ;
char c;
printf ("Scanning at %d\n",scan_rd_ptr);
while (c=scan_inbuf[scan_rd_ptr++] != ',') {
        scan_outbuf[wr_ptr++]=c;
        //printf("%d ",c);
        }
scan_outbuf[wr_ptr]=0;
printf("Read %d characters\n",wr_ptr);
printf("Buffer as read: %s \n",scan_outbuf);
return(wr_ptr);
}


#2
Warrior

Warrior

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
See this snippet from the code of commascan function

while (c=scan_inbuf[scan_rd_ptr++] != ',') {
        scan_outbuf[wr_ptr++]=c;
        //printf("%d ",c);
        }

it should be:-

while ((c=scan_inbuf[scan_rd_ptr++]) != ',') {
        scan_outbuf[wr_ptr++]=c;
        // printf("%d ",c);
        }

Have you spotted the change?
The problem was there because you didn't included the parentheses so,"scan_inbuf[scan_rd_ptr++] != ','" was first executed and the result was then assigned to c (which may be 1 or 0).

I guess you were testing the program so thats why it doesn't parse the whole sentence but only till fGHIZ...?
Also you are using old style function definitions...they are defunct.

Warrior

#3
jan_olieslagers

jan_olieslagers

    Newbie

  • Members
  • Pip
  • 2 posts
Thank you, that solved it indeed. I will need to study this nice little story before proceeding any further...
You did understand correctly that the data being scanned is for test only; actually the whole main {} is a dummy. Once the function is up and running, it will be included in other programs and this little program flushed down into eternity.

The matter of "old style definitions" has been confusing me - I started from Kernighan & Ritchie, which of course is not really modern. Any pointers for a good explanation? Even if the old style is defunct, it does work, so why shouldn't I use it? What would be a more correct style, and what would be its advantages? Portability, perhaps?

Thanks again!

#4
Warrior

Warrior

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
C99 - Wikipedia, the free encyclopedia

C99 discourages the old style function prototyping.Some compilers spawn error and don't understand this type of definition.Also that creates programs harder to read and maintain especially if the program is big and numerous peoples are working on it.
Here's the right way:-
int commascan(int scan_rd_ptr, char scan_inbuf[], char scan_outbuf[]){/*code here*/}

Warrior




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users