I am trying to convert a normal C string into a Pascal string. I am running into trouble when I need to assign the first index of the pascal string to be the length of the string. I'll place my code below, but I cannot seem to figure out how to convert the length of the original string (especially when it is over 10), slen, into a char that can then be assigned to an index.
char pascal[slen];
char pascalFirst = slen + '0'; //Having such a hard time assigning pascal[0]
printf("%c\n", pascalFirst);
pascal[0] = pascalFirst;
printf("%s pasFirst\n", pascal);
for (int i =0 ; i < strlen(pascal); i++){
pascal[i+1] = s[i];
}
printf("%s\n", pascal);
+ '0'? That makes no sense.'0'is the ASCII value of zero (48) and shouldn't be there. Don't go down the road of Cargo Cult Programming. If you see someone do something that's where you need to stop, think, and understand what they're attempting before just mimicking what they did. The Pascal string format requires the first byte to be the length expressed as anunsigned char, not an ASCII value of any kind. Not trying to bust on you here, but C is an extremely unforgiving language and just pasting in arbitrary code can get you into heaps of trouble.