Thread: Question regarding a function

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    1

    Question regarding a function

    Hey,
    I have stumbled upon an issue.
    I've attached my code below.

    I am writing a function of long numbers, and it works, it sums up the numbers.

    I need it to multiply the numbers instead, and when I do it, it gives me a wrong answer...
    Can you guys maybe check the code and review it for me?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef char* verylong;
    verylong add_verylong(verylong vl1, verylong vl2);
    
    
    int main() {
        verylong result = add_verylong("99", "888");
        for (int i = 0; i < sizeof(result); i++) {
            printf("%c", result[i]);
        }
        return 0;
    }
    verylong add_verylong(verylong vl1, verylong vl2) {
        int len_1 = strlen(vl1);
        int len_2 = strlen(vl2);
        int vl1_number = 0;
        int vl2_number = 0;
        int mul = 1;
        while (len_1 > 0) {
            int x = vl1[len_1 - 1] - '0';
            x = x * mul;
            vl1_number = vl1_number + x;
            mul *= 10;
            len_1--;
        }
        mul = 1;
        while (len_2 > 0) {
            int x = vl2[len_2 - 1] - '0';
            x = x * mul;
            vl2_number += x;
            mul = mul * 10;
            len_2--;
        }
        int number = vl1_number + vl2_number;
        int length = 0;
        int temp = number;
        while (temp > 0) {
            length += 1;
            temp /= 10;
        }
    
    
        verylong result = (char*)calloc(length + 1, sizeof(char));
        result[length--] = '\0';
        while (number > 0) {
            int x = number % 10;
            char c = x + '0';
            result[length--] = c;
            number /= 10;
        }
    
        return result;
    }
    



  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    835
    "verylong" is usually called BigNum, or similar. "verylong" makes me think of a typedef for long long or some implementation-defined larger value (e.g., __int128).

    sizeof(result) is the size of a pointer (8 or 4). To determine the length of a string, ensure that the string is zero-terminated and use strlen. You do so elsewhere so I'm not sure why you don't do that in main. Then again, it's unclear why you don't just print the string with printf.

    It makes no sense to convert your "very long" number into an int. If it's "very long" then how do you know it will fit in an int? If it fits in an int, then what's the point of a "very long" number?

    If this is an exercise then you're definitely doing it wrong. You are probably supposed to do the addition with the character string itself, not by converting the entire string to an int.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Question
    By Euclid365BCE in forum C Programming
    Replies: 9
    Last Post: 11-03-2015, 08:20 AM
  2. function question
    By unclebob in forum C++ Programming
    Replies: 4
    Last Post: 10-18-2006, 05:28 AM
  3. Function Question
    By Massaker in forum C Programming
    Replies: 3
    Last Post: 09-10-2005, 01:59 PM
  4. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM
  5. Function Question
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-23-2002, 07:13 PM

Tags for this Thread