return string should work, but ..
Hello,
i am new to C, but know some basic and z80source
i try to make a decimal-string from any 8bit value, so string from 3 chars + \0
it all works, but i always have the wrong type of 'return' value
if it realy works, i remove all those printstatements.
version 038 by now:
Code:
#include <stdio.h>
char * strint(int num ) {
/* avoiding other libs , keeping it simple */
if (num >255) return ; /* set 8bit maximum */
char str[3] ; /* '255' needs 3 char spaces */
int s ;
for (s=0 ; s<3 ; s++) str[s]=' ' ;
str[3]='\0';
printf("1-%d..%s..\n",num, str);
str[0] = num /100 ;
printf("2-%c..\n",str[0]+48);
num = num -100* str[0];
str[0]=str[0]+48 ; /*ascii*/
printf("3-%d..%s..\n",num, str);
str[1] = num/10 ;
printf("4-%c..\n",str[1]+48);
num = num -10* str[1];
str[1]=str[1]+48 ; /*ascii*/
printf("5-%d..%s..\n",num, str);
str[2]=num;
printf("6-%c..\n",str[2]+48);
str[2]=str[2]+48 ; /*ascii*/
printf("7-%d..%s..\n",num, str);
return *str ;
}
void main() {
printf( "the number is: %c \n" , strint(213) ) ;
}
result:
ch@linux-4xd4:~/Downloads/fuse-1.3.3> ./strint_038
1-213.. ..
2-2..
3-13..2 ..
4-1..
5-3..21 ..
6-3..
7-3..213..
the number is: 2
printf( "the number is: %s \n" , strint(213) ) ;
gives
1-213.. ..
2-2..
3-13..2 ..
4-1..
5-3..21 ..
6-3..
7-3..213..
Segmentation fault
but it is a string, it should be,
what do i mis here?
i guess its simple, hope so anyway
Chris