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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:17:47+00:00 2026-06-17T06:17:47+00:00

Possible Duplicate: Why do I get a segmentation fault when writing to a string?

  • 0

Possible Duplicate:
Why do I get a segmentation fault when writing to a string?

I am writing a very simple program where I am trying to concatenate two string in a for loop. The first string in the string concatenation is fixed and the second string is obtained by using itoa funciton. The program is building up successfully but when I am trying to run the program then it is not able to run and getting stopped. I just debugged the program and while debugging I realized that the program is getting stuck in the string concatenation operation. I am posting the program below. Thanks for all your support :

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

int main(int argc,char *argv[])
{
    char *str="NULL" ,dec[] = "NULL";
    int i,num;

    printf("Enter a number : \n");
    scanf("%d",&num);

    for (i=0;i<num;i++)
    {   str = "test_file_num_"; 
        itoa(i,dec,10);
        strcat(str,dec);
         printf("%s\n",str);
    }
 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-17T06:17:49+00:00Added an answer on June 17, 2026 at 6:17 am

    Writing to str is wrong, its memory is not accessible to write. When you do this char *str="NULL" for starters you are merely allocating 5 bytes (you will need more when you start concatenating), and you are also doing it on read only memory (might be on .data or might be on .text), in some cases it will work but it is undefined behavior.

    If you can’t use dynamic memory try to do something like this instead:

    char str[128]="NULL"
    

    This allocates 128 bytes on the stack (you might need more than that). It is not the best way to go, but it will be the smallest change that will fix your problem.

    EDIT:

    Same problem with this line str = "test_file_num_";
    It points str to a string literal that resides on read only memory. Change that for this:

    strcpy(str, "test_file_num_");
    

    This will copy the string literal to the stack allocated buffer.

    Finally, I think you should replace all the code inside the loop with simply this:

    printf("test_file_num_%d\n", i);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?

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.