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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:37:57+00:00 2026-05-27T20:37:57+00:00

Below is my code #import <stdio.h> #import <string.h> int main(int argc, const char *argv[])

  • 0

Below is my code

#import <stdio.h>
#import <string.h>

int main(int argc, const char *argv[])
{
    char *str = "First string";
    char *str2 = "Second string";
    
    strcpy(str, str2);
    return 0;
}

It compiles just fine without any warning or errors, but when I run the code I get the error below

Bus error: 10

What did I miss ?

  • 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-27T20:37:57+00:00Added an answer on May 27, 2026 at 8:37 pm

    For one, you can’t modify string literals. It’s undefined behavior.

    To fix that you can make str a local array:

    char str[] = "First string";
    

    Now, you will have a second problem, is that str isn’t large enough to hold str2. So you will need to increase the length of it. Otherwise, you will overrun str – which is also undefined behavior.

    To get around this second problem, you either need to make str at least as long as str2. Or allocate it dynamically:

    char *str2 = "Second string";
    char *str = malloc(strlen(str2) + 1);  //  Allocate memory
    //  Maybe check for NULL.
    
    strcpy(str, str2);
    
    //  Always remember to free it.
    free(str);
    

    There are other more elegant ways to do this involving VLAs (in C99) and stack allocation, but I won’t go into those as their use is somewhat questionable.


    As @SangeethSaravanaraj pointed out in the comments, everyone missed the #import. It should be #include:

    #include <stdio.h>
    #include <string.h>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please have a look at the code below: import string from collections import defaultdict
I import excel file into database from my below connection string of .net code.
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I am creating a random ID using the below code: from random import *
I am having the below code. import xml.dom.minidom def get_a_document(name): return xml.dom.minidom.parse(name) doc =
The code below binds an ip address to urllib, urllib2, etc. import socket true_socket
Below code saying error incorreect syntax near Main INSERT INTO tbl ( 'Week', Main,
The below code in Java throws Null pointer exception. public class New{ int i;
I'm trying to work with the below code: import javax.servlet.*; import javax.servlet.http.*; import java.io.*;
Im coding a problem(reference --http://www.codechef.com/FEB11/problems/THREECLR/) The below is my code import java.io.*; import java.util.*;

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.