A
arnuld
I wrote this program (parser ?) to extract two values from a string
separated by a comma. I extract the values without any problem. Error
checking is there with different kinds of input but in comparison to clc
folks my error checking always looks like Calvin and Hobbes plan for
saving themselves form world war 3.
As usual, any advice om improving the program ? (I wonder why the heck
the value of *pname never changes even when pointer pname is going to
next element for each loop)
/* A program that searches for 2 comma separated values inside a string:
name and number. Name can be anything while number can only be
* an integer greater than zero.
*
* VERSION 0.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
enum { SIZE_NAME = 10 };
void get_values(const char*, char*, const int, int*);
int main(void)
{
const char* arr1 = "Arnul@23,1";
const char* arr2 = "00#$%^&@23,Arnuld";
const char* arr3 = "Arnuld,";
char name[SIZE_NAME+1] = {0};
int num = -1;
printf("arr: %s\tname: %s\tnum: %d\n", arr3, name, num);
printf("\n");
get_values(arr3, name, SIZE_NAME, &num);
printf("name = %s\tnum = %d\n", name, num);
return 0;
}
void get_values(const char* p, char* pname, const int pname_size, int*
pnum)
{
int idx;
for(idx = 0; (pname_size != idx) && *p && (',' != *p); ++p, ++idx, +
+pname)
{
printf("*pname = %c, *tmp = %c\n", *pname, *p);
*pname = *p;
}
errno = 0;
*pnum = (int) strtol(p+1, NULL, 10);
if(errno)
{
perror("***ERROR***, can not convert string \"Max Phone Lines\"
into integer\n");
}
}
====================== OUTPUT ============================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
test.c: In function ‘main’:
test.c:19: warning: unused variable ‘arr2’
test.c:18: warning: unused variable ‘arr1’
[arnuld@dune programs]$ ./a.out
arr: Arnuld, name: num: -1
*pname = , *tmp = A
*pname = , *tmp = r
*pname = , *tmp = n
*pname = , *tmp = u
*pname = , *tmp = l
*pname = , *tmp = d
name = Arnuld num = 0
[arnuld@dune programs]$
separated by a comma. I extract the values without any problem. Error
checking is there with different kinds of input but in comparison to clc
folks my error checking always looks like Calvin and Hobbes plan for
saving themselves form world war 3.
As usual, any advice om improving the program ? (I wonder why the heck
the value of *pname never changes even when pointer pname is going to
next element for each loop)
/* A program that searches for 2 comma separated values inside a string:
name and number. Name can be anything while number can only be
* an integer greater than zero.
*
* VERSION 0.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
enum { SIZE_NAME = 10 };
void get_values(const char*, char*, const int, int*);
int main(void)
{
const char* arr1 = "Arnul@23,1";
const char* arr2 = "00#$%^&@23,Arnuld";
const char* arr3 = "Arnuld,";
char name[SIZE_NAME+1] = {0};
int num = -1;
printf("arr: %s\tname: %s\tnum: %d\n", arr3, name, num);
printf("\n");
get_values(arr3, name, SIZE_NAME, &num);
printf("name = %s\tnum = %d\n", name, num);
return 0;
}
void get_values(const char* p, char* pname, const int pname_size, int*
pnum)
{
int idx;
for(idx = 0; (pname_size != idx) && *p && (',' != *p); ++p, ++idx, +
+pname)
{
printf("*pname = %c, *tmp = %c\n", *pname, *p);
*pname = *p;
}
errno = 0;
*pnum = (int) strtol(p+1, NULL, 10);
if(errno)
{
perror("***ERROR***, can not convert string \"Max Phone Lines\"
into integer\n");
}
}
====================== OUTPUT ============================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
test.c: In function ‘main’:
test.c:19: warning: unused variable ‘arr2’
test.c:18: warning: unused variable ‘arr1’
[arnuld@dune programs]$ ./a.out
arr: Arnuld, name: num: -1
*pname = , *tmp = A
*pname = , *tmp = r
*pname = , *tmp = n
*pname = , *tmp = u
*pname = , *tmp = l
*pname = , *tmp = d
name = Arnuld num = 0
[arnuld@dune programs]$