-2

Using ansi c
#define STDC_VERSION 201112L

I'm new to c, but took my lead from Here

Regardless of the approaches I've tried, I can't get the sum pointer to return.

Running the code succeeds (creates the correct number), and the printf in the addNumericStrings function works, but the printf in the main does not (prints nothing). I assume due to an error prior. I'm not getting any warnings on the build.

output

addNumericStrings.sum = 11234567889

main

#include <stdio.h>
#include <stdlib.h>
#include "addViaStrings.h"

int main(void) {
    char s1[] = "9999999999";
    char s2[] = "1234567890";

    char *sum = addNumericStrings(s1, s2);
    printf("main.sum = %s\n", sum);
}

functions

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

char addNumericStrings(char *s1, char *s2);
char leftPad(char *result, char *s, int maxlength);
int  findMaxLength(char *s1, char *s2);

char addNumericStrings(char *s1, char *s2){
    int maxlength = findMaxLength(s1, s2);
    char addend1[maxlength];
    char addend2[maxlength];

    char *sum = (char *) malloc(sizeof(char) * (maxlength + 2) );

    int a1, a2, total;
    int carry = 0;

//  char sum[(maxlength + 2)]; // +1 in case addition rolls over past maxlength
    // Prepare the strings for manual addition
    leftPad(addend1, s1, maxlength);
    leftPad(addend2, s2, maxlength);
    // Buffer sum with ascii zeros (#48), not 0, and not "\0"
    for (int i = 0; i < (maxlength + 1); i++) { sum[i] = 48; }
    sum[maxlength + 1] = 0;
    // Run the manual addition
    // Start adding individual ints from end (right side)
    for (int i = maxlength - 1 ; i >= 0; i--) {
        a1 = addend1[i] - '0'; // Convert to int
        a2 = addend2[i] - '0'; // Convert to int
        total = (a1 + a2 + carry);
        carry = 0;
        if ( total >= 10){
            carry += 1;
            total -= 10;
        }
        sum[i +1] = 48+total; // convert to ascii value for numbers (adding 48)
    }
    sum[0] = 48+carry; // add last carry to start of num always, even if 0
    sum[maxlength + 1] = '\0';
    printf("addNumericStrings.sum = %s\n", sum);
    return sum;
}

char leftPad(char *result, char *s, int maxlength){
    // Pads number to [000123456789]    
    ( ...snip... )
}

int findMaxLength(char *s1, char *s2){
// Returns int value of the length of the longer between s1 and s2
    int length1 = strlen(s1);
    int length2 = strlen(s2);
    int maxlength;
    (length1 > length2) ? (maxlength = length1) : (maxlength = length2);
    return maxlength;
}
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
RightmireM
  • 2,381
  • 2
  • 24
  • 42
  • 3
    `char addNumericStrings(char *s1, char *s2) {` but you return a `char *`. –  Oct 28 '18 at 16:25
  • Furthermore, `sum` is supposed to be a *string*, but you forget to *null-terminate* it. – Some programmer dude Oct 28 '18 at 16:29
  • 1
    On a couple of unrelated notes, first please don't use [*magic numbers*](https://en.wikipedia.org/wiki/Magic_number_(programming)). If by `48` you mean the character `'0'` then *say* so. That will also make the code independent of ASCII encoding (which isn't the only encoding). Also, your `findMaxLength` is using the conditional expression in a way that many would consider "wrong" (including myself). I suggest something like `maxlength = length1 > length2 ? length1 : length2;` Or skip `maxlength` completely and just `return length1 > length2 ? length1 : length2;` – Some programmer dude Oct 28 '18 at 16:31
  • @Someprogrammerdude. I did have a line that added a null terminator, but it didnt change the outcome, and didnt seem to matter for it being recognized as a string elsewhere in the script. I assume because it had one when I initialized. The line was ` sum[maxlength + 1] = '\0';` (replaced as a commented line in the original post)... but even re-adding it doesn't change the output. – RightmireM Oct 28 '18 at 16:33
  • 1
    Trust me, that null-terminator is important. Without it all functions that will look for the end (including your `printf` calls) will go out of bounds looking for it and you will have [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). That is seems to work without you adding it is pure luck (some would argue that you're *unlucky* it works). – Some programmer dude Oct 28 '18 at 16:36
  • @J.Doe. Thanks for the response. Unfortunately, I'm new to c and I'm not sure what you mean. `char *s1` and `char *s2` are pointers. I create a new `*sum` pointer reference (I think) using `char *sum = (char *) malloc(sizeof(char) * (maxlength + 2) );`. This was my best guess based on the link in shown in the OP. How should the line be different? – RightmireM Oct 28 '18 at 16:39
  • 1
    The return type of this function is `char`, but you return data of type `char *`. That's the problem. – ForceBru Oct 28 '18 at 16:40
  • 1
    You want to return a `char*` so the return type of `addNumericStrings()` must be `char*`, not `char`: `char* addNumericStrings(char *s1, char *s2)`. –  Oct 28 '18 at 16:40
  • @Someprogrammerdude. Good to know. I'll remember that. I'm not sure if I added it correctly (to the right index) but, even with the added line, the return still fails. Am I missing something additional? – RightmireM Oct 28 '18 at 16:40
  • @J.Doe. That was it. Thanks! Add this as an answer and I'll mark it accepted! – RightmireM Oct 28 '18 at 16:45
  • @Someprogrammerdude: 'please don't use magic numbers.'. Also noted. But in this case, it was due to my inability to figure out how convert the summed up variable `total` back from an `int` into a `char`. I could never find a way to do it - so instead I filled the indexed location in the `char sum` with its ascii value. I.e. `sum[i +1] = 48+total;` How should I replace this line to convert the `int total` for insertion into the `char sum`? – RightmireM Oct 28 '18 at 16:51
  • Also, if I may ask - why the down vote? – RightmireM Oct 28 '18 at 16:54
  • You use `addend1[i] - '0'`, why can't you use `sum[i +1] = '0'+total`? It's really the same thing. – Some programmer dude Oct 28 '18 at 17:32
  • @Someprogrammerdude Fair enough. I had actually tried that, but it didn't work. However, I did it gain and now it's working. I must have fatfingered it somewhere the first time. Thx. – RightmireM Oct 28 '18 at 17:35
  • @Makyen **For archival reference, this question was not the result of a typo**. It was the result of contradictory information received from other stack answers about the right way to declare a pointer return. The "typo" comment was about an aside, unrelated to the question. – RightmireM Nov 01 '18 at 09:22

1 Answers1

1

If you want to return a char * from your function, you have to use the appropriate return type:

char* foo(void)
//  ^
{
    char *bar;

    // ...

    return bar;
}