Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:05:52+00:00 2026-06-10T11:05:52+00:00

I have a problem with an add calculator. Valgrind reports no memory errors, no

  • 0

I have a problem with an “add calculator”.
Valgrind reports no memory errors, no errors from compiler but the program doesn’t show any output despite the printf – “Base is “.

All pointers, and variables are (n my opinion) correctly initialized.

getnum function gets a number, returns a pointer to char – char *,
add function processes two numbers as strings, returns result which is a pointer to char (char *) as well.

I don’t know whether the problem is memory allocation or procedures connected to processing arrays…

Here’s the code:

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

#define MAX(A,B) ((A)>(B) ? (A) : (B))

char *getnum(FILE *infile, int base)
{
    int len = 10;
    int c;
    int pos = 0;
    char *num = NULL;
    char *tmpnum = NULL;

    num = malloc(sizeof(char)*len);

    while (((c = fgetc(infile)) != EOF) && (isalnum(c))) {

        if (isdigit(c)) {
           /* irrelevant*/
        }
        else if (isalpha(c)) {
            fprintf(stderr, "Wrong base, expected 16\n");
            free(num);
            return NULL;

        }
        if (pos >= len) {
            /*realloc*/
        }
    }

    return num;           
}

int main(int argc, char **argv)
{
    FILE *infile = NULL;
    char *number1 = NULL;
    char *number2 = NULL;
    char *result = NULL;
    int base, i, j = 0, length, count = 0;


    infile = fopen(argv[1], "r");

    base  = atoi(argv[2]);
    while (!feof(infile)) {
        number1 = getnum(infile, base);

        number2 = getnum(infile, base);

        break;
    }
    printf("Base is %d\n", base);
    result = add(number1, number2, base);

    length = strlen(result);
    for (i = 0; i <= length - 1; i++) {
        if (result[i] == '0') {
            count++;
        }
    }

    for (j = i; j == (length - 1); j++) {
        printf("Result is: %s\n", &result[j]);
        break;
    }
    free(result);
    result = NULL;
    fclose(infile);
    return 0;
}

Trying to work it out for the past 4 hours and can’t find a mistake.
Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:05:53+00:00Added an answer on June 10, 2026 at 11:05 am

    There is one severe typo near the end of main().

    for (j = i; j == (length - 1); j++) {
    /*            ^^ SHOULD BE <= */
        printf("Result is: %s\n", &result[j]);
        break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem, when I want add Node to my GUI from other Thread.
I won't create wallpaper-application but have one problem. Please tell me how add ImageView
I have problem to add jQuery to some existing code, please help. We have
I have a problem: I've add a Jquery UI drag-n-drop widget to my page,
I have problem on my blog: http://osify.com while processing add new post with above
I have a problem where I have to add thirty-three integer vectors of equal
I have a problem with WPF Toolkit Chart Control. I can't add text values
I have a problem with XamDataChart in a WPF application. When I add the
I have a little problem, I have an array and I want to add
I have a small problem, I have tried different solutions to add a image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.