Access and store value by index in a pointer

Paula Chittaro

I've got an assignment where I have to sum whole numbers up to 100 digits.

They gave me this struct to represent big numbers (I think there are better ways to represent this, but I'm not allowed to modify it):

typedef struct {
  char* string;
  int lengthError;
} BigNumber;

Where string is the number itself and lengthError is the length of the number or an error that is a previously defined enum.

I've also have the implementation of the sum function

BigNumber *sum(BigNumber* num1, BigNumber* num2) {
    BigNumber* result = malloc(sizeof(BigNumber));
    int limit = getLength(num1->lengthError, num2->lengthError);
    result->string = malloc(limit);
    int digitResult;
    int index = limit -1;
    int carry = 0;

    while(index != -1) {
        int d1 = ((int)num1->string[index]) - ((int)'0');
        int d2 = ((int)num2->string[index]) - ((int)'0');
        digitResult = d1 + d2 + carry;

        if (digitResult > 9) {
            digitResult = digitResult - 10;
            carry = 1;
        } else {
            carry = 0;
        }

        itoa(digitResult, &result->string[index], 10); //I think this is the problem
        index--;
    }

    result->string[limit] = '\0';
    printf("result: %s\n", result->string);
    return result;
}

I haven't finished writing that function, I know there are a lot of flaws in it, but the problem is that I can't get to sum 12 + 12. The result I get is 2.

I thought approaching this problem by picking the lasts character of both numbers, transform them into an int and sum them having in mind the carry digit. After I got the result in digitResult I try to convert it to a char and store it in the corresponding position of the result->string pointer

Once it has finished the operation, I add an \0 at the last position of the result->string.

So the question is, how do I make this operation to work as desired? Debugging the code, I noticed that the first time it stores the first result in result->string, following the example above this would be a number 4, it stores trash in that position instead. In the second addition, I store a number 2 correctly and that's the final result I get in when I print the result.

Adrian Mole

Your use of the itoa function is a problem (though, as you have also suggested, maybe not the only one).

The itoa function converts its first argument into a null-terminated string - so, as well as writing the character representation of digitResult at the indicated place in the string, it also adds a '\0' character after it. Thus, your string will always be terminated immediately after the last digit you write, and 12 + 12, giving 24 will appear to be just the first character: 2.

What you can do instead is to convert the digit yourself (reversing the operation you used to get the d1 and d2 values), then just directly set the string element to the converted digit.

So, instead of:

    itoa(digitResult, &result->string[index], 10);

use:

    result->string[index] = (char)(digitResult + '0');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there a way to store the value of a pointer but do not access the address?

Store value in pointer var

Access to another struct by value or by pointer

access the value of array by pointer in CLR

Access the value using pointer in Ada

String index value access

Best Java Collection to store key, value pairs and access its objects by Index or by key name

Cannot access index of array pointer C

Index limit for access array with pointer arithmetic in C

How many bits are required to store the pointer value?

How to store array and pointer value inputs in assembly?

Assigning the value of a pointer at a certain index in C

Access last index value of dataframe

Access index of of filtered value(s)

MustacheJS - access value by property as index

Access pointer value of a struct inside a function

Go access pointer value if its passed as interface{}

Copy member value on stack or access it with pointer?

c++ delete pointer and then access the value of it points to

how to access character pointer value using ctypes?

How to access a pointer's value that is a char array

value store in vector changed when I use pointer to set the value

C++ Does pointer of pointer automatically have access to new value?

Store specific index value of output in R

Store and find higher value index in a numpy array

Access data frame value and store into variable in python

Access store procedure value from controller

Access redux store with variable value in name

How do I access an index of a pointer inside of an array in C?