spujb@lemmy.cafe to 196@lemmy.blahaj.zoneEnglish · 2 days agorulelemmy.cafeimagemessage-square25fedilinkarrow-up1127arrow-down12
arrow-up1125arrow-down1imagerulelemmy.cafespujb@lemmy.cafe to 196@lemmy.blahaj.zoneEnglish · 2 days agomessage-square25fedilink
minus-squareSunoc@sh.itjust.workslinkfedilinkEnglisharrow-up68·2 days ago You can’t perform calculations on a string Laugh in C
minus-squareJusticeForPorygon@lemmy.blahaj.zonelinkfedilinkEnglisharrow-up32·2 days agoImagine having strings
minus-squareAtlusb@lemmy.worldlinkfedilinkEnglisharrow-up11·2 days agoWhat about a pointer to a series of pointers which point to chars?
minus-squarea_non_monotonic_function@lemmy.worldlinkfedilinkEnglisharrow-up5·23 hours agoNASA’s 11th rule: All forms of indirection must indirect 12 times. This is not an upper or lower bound. This is the exact number of indirections. Why? F*** you, that’s why.
minus-squareanton@lemmy.blahaj.zonelinkfedilinkEnglisharrow-up9·1 day ago#include <stdbool.h> #include <stdio.h> #include <stdlib.h> char* addStrings(char* as, char*bs){ int aL=strlen(as); int bL=strlen(bs); if (aL<bL){ char* temp=bs; int tL=bL; bs=as; bL=aL; as=temp; aL=tL; } char* cs = malloc(aL+2); cs[aL+1]='\0'; bool carry = false; int bi=bL-1; for(int ai=aL-1; ai>=0; ai--,bi--){ char a=as[ai]; char b='0'; if(bi>=0)b=bs[bi]; char c=a+b-'0'; if(carry)c++; carry=c>'9'; if(carry)c-=10; cs[ai+1]=c; } if(carry) cs[0]='1'; else cs[0]='0'; return cs; } int main(int argc, char**args){ printf("%s + %s = %s\n", args[1] , args[2] , addStrings(args[1] , args[2])); }
Laugh in C
Imagine having strings
You mean arrays of chars ?
What about a pointer to a series of pointers which point to chars?
NASA’s 11th rule: All forms of indirection must indirect 12 times. This is not an upper or lower bound. This is the exact number of indirections. Why? F*** you, that’s why.
#include <stdbool.h> #include <stdio.h> #include <stdlib.h> char* addStrings(char* as, char*bs){ int aL=strlen(as); int bL=strlen(bs); if (aL<bL){ char* temp=bs; int tL=bL; bs=as; bL=aL; as=temp; aL=tL; } char* cs = malloc(aL+2); cs[aL+1]='\0'; bool carry = false; int bi=bL-1; for(int ai=aL-1; ai>=0; ai--,bi--){ char a=as[ai]; char b='0'; if(bi>=0)b=bs[bi]; char c=a+b-'0'; if(carry)c++; carry=c>'9'; if(carry)c-=10; cs[ai+1]=c; } if(carry) cs[0]='1'; else cs[0]='0'; return cs; } int main(int argc, char**args){ printf("%s + %s = %s\n", args[1] , args[2] , addStrings(args[1] , args[2])); }