code add infinity from chatgpt

PHOTO EMBED

Sun Jul 02 2023 01:33:00 GMT+0000 (Coordinated Universal Time)

Saved by @thefirewall #html

There are several issues with your code that could be causing the failed checks. Here are some potential problems:

Buffer Size: The function takes size_r as an argument, representing the size of the buffer r to store the result. However, it seems that you are not properly handling the case when the result cannot fit within the provided buffer size. You need to check if the length of the result exceeds the buffer size and return 0 in such cases.

Buffer Manipulation: There are multiple instances in your code where you increment and decrement the r pointer without considering the buffer boundaries. This can lead to incorrect memory access and unexpected behavior. Make sure to handle the buffer correctly throughout the function.

String Length Calculation: The current implementation of calculating the lengths of n1 and n2 by incrementing the pointers is incorrect. You are modifying the original pointers, which makes it difficult to traverse the strings later. Consider using separate variables to calculate the lengths without modifying the original pointers.

Handling Carry: When performing addition, if the sum of digits exceeds 9, you need to carry over the value to the next position. It seems that you are correctly calculating the remainder (rem) and the carry (div), but you need to handle the carry properly to ensure accurate addition.

Returning the Result: After performing the addition, you need to return the pointer to the result. However, the current implementation does not correctly determine the final result pointer. Ensure that you properly adjust the pointer to the correct position before returning it.

Here's a modified version of your code that addresses these issues:

c
Copy code
#include <stdlib.h>

char *infinite_add(char *n1, char *n2, char *r, int size_r)
{
    int len1 = 0, len2 = 0, i = 0, div = 0, rem;
    char val = '0';

    // Calculate lengths of n1 and n2
    while (n1[len1] != '\0')
        len1++;
    while (n2[len2] != '\0')
        len2++;

    // Check if result can fit in buffer
    if (len1 > size_r || len2 > size_r)
        return 0;

    // Move pointers to the end of the strings
    n1 += len1 - 1;
    n2 += len2 - 1;

    while (i < size_r - 1)
    {
        if (len1 >= 0 || len2 >= 0)
        {
            // Get the digits from n1 and n2
            int digit1 = (len1 >= 0) ? *n1 - '0' : 0;
            int digit2 = (len2 >= 0) ? *n2 - '0' : 0;

            // Calculate sum and carry
            rem = (digit1 + digit2 + div) % 10;
            div = (digit1 + digit2 + div) / 10;

            // Store the sum digit in the result buffer
            r[i] = rem + '0';

            // Move the pointers and decrement lengths
            n1 = (len1 > 0) ? n1 - 1 : &val;
            n2 = (len2 > 0) ? n2 - 1 : &val;
            len1--;
            len2--;
        }
        else if (div > 0)
        {
            // Handle remaining carry
            r[i] = div + '0';
            div = 0;
        }
        else
        {
            // Reached the end of both strings and carry is 0
content_copyCOPY