143
1mon
29

rule

Sunoc - 1mon

You can't perform calculations on a string

Laugh in C

75
JusticeForPorygon - 1mon

Imagine having strings

34
Sunoc - 1mon

You mean arrays of chars ?

32
Atlusb @lemmy.world - 1mon

What about a pointer to a series of pointers which point to chars?

11
a_non_monotonic_function @lemmy.world - 4w

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.

5
anton - 4w
#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]));
}
10
renzhexiangjiao - 1mon

In computing, whole numbers are referred to as integers

they are known as integers outside of computing too lol

61
pory @lemmy.world - 4w

Yeah, but in computing (well, specifically the language this slide is about) they're referred to as integers and not referred to as "whole numbers" or any other synonym. If you're looking up something to do with integers in the documentation, you need to have the word "integer" in your vocabulary as "the" way to refer to numbers without decimal places to find what you're looking for. Same way you need to know "string" instead of "word" or "text field" or "sentence".

12
zqwzzle @lemmy.ca - 1mon

JavaScript trying to look nonchalant in the corner.

50
orockwell @lemmy.world - 1mon

Type coercion

8
jia_tan - 1mon

More like type mutilation

19
a_non_monotonic_function @lemmy.world - 4w

Depression.

3
Parafaragaramus - 1mon

Amazingly you can store all sorts of things in variables.

26
ThunderComplex @lemmy.today - 4w

Including nothing. Actually, depending on the language, multiple types of nothing (null vs uninitialized)

14
I Cast Fist - 4w

This has made a lot of people very angry and been widely regarded as a bad move.

8
gandalf_der_12te @discuss.tchncs.de - 4w

actually, you can even store pointers to nothing in memory. you just do:

void x;
void* x_ptr = &x;

then you can use it to invoke functions that take no arguments like this:

void open_texteditor (void editor_choice)
{
    // do nothing with the editor_choice because there is only one sensible editor
    open_nano(); 
    return void;
}
void favorite_texteditor;
void result = open_texteditor(favorite_texteditor);
print_error_on_bad_result(result);

(note that this is a joke comment)

6
dandelion (she/her) - 1mon

comp sci brainrot

25
sp3ctr4l @lemmy.dbzer0.com - 1mon

I mean, in Python, Int() literally is a pre - formed function for per - forming math on strings.

21
rumschlumpel @feddit.org - 1mon

IIRC int() doesn't let you do math on strings, it converts strings into integers and then you can do math with those numbers.

17
sp3ctr4l @lemmy.dbzer0.com - 1mon

Yes. It converts ... well, any data type, really, to an int, and then you can do math on that value, via the normal Python syntax.

My point was that Int() is pre - formed... the author of this slide here is hopefully, before they switch to the next side, asking the classroom if there is anything wrong with their slide.

Small spelling errors are often trvial in written language, but uh... they're kind of a very big deal in code.


You... could make a function that directly does some math operations on a string.

You could try to set up a bitshifting scheme, you could essentially make FancyMathInt(x,y), where x is converted to an int, and, perhaps hilariously, y is a string of math operations done to x, after y itself is parsed and converted to an instruction set...

There's a lot of ways you could do that.

... Most of them are probably stupid, but, niche edges do sometimes arise where something like that could be useful.

3
Treczoks @lemmy.world - 4w

This is programming 101 from which century?

12
spujb @lemmy.cafe - 4w

does it say programming 101 somewhere

3
Rooster326 @programming.dev - 4w

No programming 101 is what happens when you perform calculations on a string.

Please do try to keep up.

3
spujb @lemmy.cafe - 4w

oh i see we are just being judgmental for fun my fault for assuming it was genuine :(

1
Rooster326 @programming.dev - 4w

It is only a joke.

Calculations on strings are "allowed".

"10" + "1" = "101"

3
AItoothbrush - 4w

You can very much perform calculations on strings, and its a very cool thing, but i think thats wayyy above the level that this presentation is meant for cause you probably should know what integers are before that, even outside of programming...

8
ArtVandelay @lemmy.world - 4w

"preform calculations"

4
AnUnusualRelic @lemmy.world - 4w

Is it all right to postform calculations on a string?

3
TootSweet @lemmy.world - 4w

I can't let you do that, Dave.

2