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 8650675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:52:54+00:00 2026-06-12T13:52:54+00:00

I am making a program that concatenates two strings given by user. Everything is

  • 0

I am making a program that concatenates two strings given by user. Everything is fine, but I don’t know why the program shows that the sizeof of the final result is 8-bit long. Doesn’t matter how long the strings are, it always shows 8. I guess, that it is the size of char, but I would like to know why it acts this way. This is the code:

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

char* concatenate(char *fir, char *sec)
{
int firLen = strlen(fir);
int secLen = strlen(sec);
int len = firLen + secLen + 1;
int i = 0,c=0;
int *wsk = &i;

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

while (fir[i]!='\0')
{
    result[i]=fir[i];
    (*wsk)++;
}

while (sec[c]!='\0')
{
    result[i]=sec[c];
    (*wsk)++;
    c++;
}

result[len-1] = '\0';
return result;
}

int main(int argc, char **argv)
{
char *first, *second, *output;
int size1, size2;

printf("How long will your first string be: ");
scanf("%d", &size1);

first = (char *) malloc ((1+size1)*sizeof(char));
if (!first)
{
    puts("\nError. Can't allocate memory!");
    abort();
}

printf("How long will your second string be: ");
scanf("%d", &size2);

second = (char *) malloc ((size2+1)*sizeof(char));
if (!second)
{
    puts("\nError. Can't allocate memory!");
    abort();
}

printf("\nPlease, type in the first string: ");
scanf("%s",first);

printf("\nPlease, type in the second string: ");
scanf("%s",second);

output = (char *)malloc((size1+size2+1)*sizeof(char));
output = concatenate(first, second);

printf("\nConcatenation of the strings: %s", output);
printf("\n%d", sizeof(output));

free(first);
free(second);
free(output);

getchar();
return 0;
}
  • 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-12T13:52:55+00:00Added an answer on June 12, 2026 at 1:52 pm

    Don’t use sizeof to determine string length, use the strlen function, as you’re doing in other parts of your program.

    printf("\nConcatenation of the strings: %s", output);
    printf("\n%d", strlen(output));
    

    Use sizeof to determine the size of data types, like char, a struct, an array, etc.

    You’ve mentioned that you want to use sizeof to check if everything is fine with memory allocation, but you can’t use sizeof this way on a buffer of memory allocated with malloc: you can only rely on the return value of malloc – which you aren’t checking – to know if your allocation was successful.

    You can use sizeof to determine the size of an array:

    char myStr[13];
    printf("%d\n", sizeof(myStr));
    

    But this only works for arrays, and not buffers allocated using malloc.

    Also this line creates a memory leak because you overwrite the pointer in the next line:

    output = (char *)malloc((size1+size2+1)*sizeof(char));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a program that asks for an integer from the user, it then
I'm making a program that will output lines to a text file. I don't
I am making a C++ program that checks if given aray is a latin
I've been making a program that uses core data. It has two entities, Medicine
I'm making a program that takes names from the user seperated by commas. The
I'm making a program that makes a fractal, and the user is suppose to
I am making a program that inserts a two-dimensional array with random integers and
I tried making a program that flips a coin(shows image of heads first and
I am making a program that makes use of two Array Lists. I've researched
I'm making a program that fits the wizard concept ideally; the user is walked

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.