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

  • Home
  • SEARCH
  • 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 6178087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:24:14+00:00 2026-05-24T00:24:14+00:00

Can anyone explain to me why when initializing a char array, if the array

  • 0

Can anyone explain to me why when initializing a char array, if the array size is left blank, like this

char str1[] = "Hello";

the program will seg fault, but if it is specified like this

char str1[10] = "Hello";

it works fine.

Here is the full program

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

char concat_string(char str[], char str2[], char destination[], unsigned int bufferSize);

int main(int argc, char *argv[])
{
    unsigned int bufferSize = 64;
    // Both str1 and str2 must be defined
    // or else the program will seg fault.
    char str1[] = "Hello ";
    char str2[] = "World";
    char concatenatedString[bufferSize];

    concat_string(str1,str2,concatenatedString,bufferSize);

    printf("The concatenated string is: \n%s\n", concatenatedString);
    return 0;
}


char concat_string(char str[], char str2[], char destination[], unsigned int bufferSize)
{
    char buffer[bufferSize];
    strncat(str, str2, bufferSize);
    strncpy(buffer,str, bufferSize);
    strncpy(destination,buffer,bufferSize);
    return *destination;
}

  • 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-05-24T00:24:15+00:00Added an answer on May 24, 2026 at 12:24 am

    You have a buffer overflow right here in your concat_string function:

    strncat(str, str2, bufferSize);
    

    Your str only has room for seven bytes and it is already full before you try to append str2 to it. You’re getting lucky with this:

    char str1[10] = "Hello";
    

    as you still don’t have enough space allocated to append "World" to it; you’re also missing the trailing space on this version of str1 but that’s not relevant to your segfault. Your concat_string should be copying str directly to destination and then appending str2 to destination. This would also avoid altering the str and str2 arguments and that would be more polite; you also don’t pass the sizes of the str and str1 arrays so there’s no way to know if there is room to append anything to them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain me how this query will work and find nth highest salary
Can anyone explain what this mod_rewrite rule is doing? I'm trying to comment the
Can anyone explain what advantages there are to using a tool like MSBuild (or
Can anyone explain to me what this means? Run-Time Check Failure #0 - The
Can anyone explain why this happens? mybox:$ ruby script/console Loading development environment (Rails 2.3.5)
Can anyone explain the meaning of this code? if (data.result) { $('ul#intlist').append(data.content); }
Can anyone explain why this bubble sort function doesn't work and why I lose
Can anyone explain me why <ul> elementa cannot be empty? And why this HTML:
Can anyone explain what the jquery documentation is exactly referring to with this statement:
Can anyone explain this code snippet to me? <script type=text/javascript> function querySt(ji) { hu

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.