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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:34:36+00:00 2026-06-13T07:34:36+00:00

Why does the following segfault? I am using standard c99, icc compiler with unix.

  • 0

Why does the following segfault?

I am using standard c99, icc compiler with unix. I can’t get this to not segfault, and I am curious why. I am not familiar with strcat/strcpy very much.

char *first = "First";
char *second = "Second";
char *both = (char *)malloc(strlen(first) + strlen(second) + 2);

strcpy(both, first);
strcat(both, " ");
strcat(both, second);

sprintf("%s %s", first, second);
  • 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-13T07:34:38+00:00Added an answer on June 13, 2026 at 7:34 am
    sprintf("%s %s", first, second);
    

    The first parameter of sprintf is a destination buffer. You have given it a constant string as a destination buffer.

    If you’re just trying to print out something, did you mean printf?

    Otherwise, correct use would be something like:

    // declaration of "dest" left as exercise for the reader
    //
    sprintf(dest, "%s %s", first, second);
    

    Although, sprintf has been superseded by snprintf, which is better to avoid buffer overflows.

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

Sidebar

Related Questions

I'm getting crazy with this IE 7... ==> hhttp://neu.emergent-innovation.com/ Why does following function not
Why does the following segfault, and how can I prevent it? <?php $str =
Why does the following program segfault? int main() { main(); } Even though it
Can any one tell me? what does following means in ruby program: obj =
Keep only ObservableCollections in the ViewModel and IEnumerables in the Model? Does following this
Does the following mean that dynamic TextFields will NOT be indexed? We currently do
Given that: 1) The C++03 standard does not address the existence of threads in
I am currently developing application that does following: Read Inbox (using MAPI) & SMS
Possible Duplicate: Why does this Seg Fault? I receive a segmentation fault when using
I came across the following C puzzle: Q: Why does the following program segfault

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.