2

So I have seen that some people do not recommend using String literals to create a char in C. However, I have found this to be the most useful method and I have a char set as

char a[] = "456";

I understand that the memory is fixed in this data type. However, in my assignment I believe that I need to be able to reallocate memory for my char value since I am adding integers that are represented as strings. For example:

char b[] = "500";
char c[] = "501";
//add function for adding b and c. have a char d[] equal to "1000". 

I am using long addition to accomplish this. I have tried experimenting with the realloc function but it did not work. How would I continuously append new characters onto a string?

user3386109
  • 34,287
  • 7
  • 49
  • 68
  • 2
    Perhaps you can put together a [mcve] so we can see what you're trying to do. – Retired Ninja Feb 10 '19 at 05:59
  • 1
    A useful property of addition is that the sum of two numbers has at most one more digit than the larger of the two summands. – user3386109 Feb 10 '19 at 06:29
  • If you are attempting to use realloc on an array, it is not expected to work. You cannot realloc an array. – William Pursell Feb 10 '19 at 06:30
  • https://stackoverflow.com/questions/10279718/append-char-to-string-in-c – Islam Hassan Feb 10 '19 at 06:32
  • As William pointed out, actually, there is no string literals created in `char a[] = "456"`, the string literal is an initalizer of the array `a`. – stensal Feb 10 '19 at 06:53
  • The `realloc()` function does work if used correctly; it is not particularly hard to use it correctly, though it is very easy to use it incorrectly. A better description would be "I have tried experimenting with the `realloc()` function but was not able to work out how to use it correctly". – Jonathan Leffler Feb 10 '19 at 07:33
  • @JonathanLeffler, I believe he cannot use `realloc` if he insists to use `char a[] = "456";` to declare a storage. My understanding is `realloc` can only reallocate a storage that is allocated on heap. Correct me if I'm wrong. – stensal Feb 10 '19 at 14:19
  • @stensal — your right that one of the steps would be to make sure the data is allocated; one of the easy ways of misusing `realloc()` is to try and `realloc()` memory that was not previously allocated. A solution using `realloc()` and friends would need careful reworking of the code. Nevertheless, it could be done; `realloc()` is not broken. – Jonathan Leffler Feb 10 '19 at 14:51

2 Answers2

1

so the way I see it is by typing:

char a[] = "456";

you initialise a character array of size 4 (3 chars +'\0'). storing anything longer in the same array could cause an undefined behavior and must be avoided. long story short: you can modify the value of this array, as long as the size of it doesn't change. You can initialise an array like this:

char a[100] = "456";

to leave some extra space for any possible additions to the string, but that's about it, since a size of the array is known at compiling, and doesn't ever change. also please note that this won't work:

char * a = "456";

it's a read-only string literal which cannot be modified or realloced.

PS: I'm a novice, please correct me if I'm wrong!

Duck Ling
  • 1,577
  • 13
  • 20
1

For your assignment, you must define a destination array with enough space to hold the result, which for an addition is easy to determine: it is the length of the longest argument plus one character for a possible extra digit plus one character for the null terminator.

You can allocate this array with malloc() and return a pointer.

Here is an example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *bigadd(const char *a, const char *b) {
    size_t alen = strlen(a);
    size_t blen = strlen(b);
    size_t clen = (alen > blen) ? alen : blen;
    size_t mlen = (alen > blen) ? blen : alen;
    char *c = malloc(clen + 2);
    if (c != NULL) {
        size_t i;
        int carry = 0;
        c[clen] = '\0';
        for (i = 1; i <= mlen; i++) {
            carry += a[alen - i] - '0' + b[blen - i] - '0';
            c[clen - i] = '0' + carry % 10;
            carry /= 10;
        }
        for (; i <= alen; i++) {
            carry += a[alen - i] - '0';
            c[clen - i] = '0' + carry % 10;
            carry /= 10;
        }
        for (; i <= blen; i++) {
            carry += b[blen - i] - '0';
            c[clen - i] = '0' + carry % 10;
            carry /= 10;
        }
        if (carry) {
            memmove(c + 1, c, clen + 1);
            c[0] = (char)('0' + carry);
        }
    }
    return c;
}

int main(int argc, char *argv[]) {
    const char *a = argc > 1 ? argv[1] : "123456890123456890123456890";
    const char *b = argc > 2 ? argv[2] : "2035864230956204598237409822324";
    char *c = bigadd(a, b);
    printf("%s + %s = %s\n", a, b, c);
    free(c);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189