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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:54:11+00:00 2026-05-15T21:54:11+00:00

include #include <string.h> int main() { char *array[10]={}; char* token; token = testing; array[0]

  • 0

include

#include <string.h>

int main()
{
        char *array[10]={};
        char* token;
        token = "testing";
        array[0] = "again";
        strcat(array[0], token);
}

why it returns Segmentation fault?

I’m a little confused.

  • 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-15T21:54:12+00:00Added an answer on May 15, 2026 at 9:54 pm

    Technically, this isn’t valid C. (It is valid C++, though.)

    char *array[10]={};
    

    You should use

    char *array[10] = {0};
    

    This declares an array of 10 pointers to char and initializes them all to null pointers.

    char* token;
    token = "testing";
    

    This declares token as a pointer to char and points it at a string literal which is non-modifiable.

    array[0] = "again";
    

    This points the first char pointer of array at a string literal which (again) is a non-modifiable sequence of char.

    strcat(array[0], token);
    

    strcat concatenates one string onto the end of another string. For it to work the first string must be contained in writeable storage and have enough excess storage to contain the second string at and beyond the first terminating null character (‘\0’) in the first string. Neither of these hold for array[0] which is pointing directly at the string literal.

    What you need to do is something like this. (You need to #include <string.h> and <stdlib.h>.)

    I’ve gone for runtime calculation of sizes and dynamic allocation of memory as I’m assuming that you are doing a test for where the strings may not be of known size in the future. With the strings known at compile time you can avoid some (or most) of the work at compile time; but then you may as well do "againtesting" as a single string literal.

    char* token = "testing";
    char* other_token = "again";
    
    /* Include extra space for string terminator */
    size_t required_length = strlen(token) + strlen(other_token) + 1;
    
    /* Dynamically allocated a big enough buffer */
    array[0] = malloc( required_length );
    strcpy( array[0], other_token );
    strcat( array[0], token );
    
    /* More code... */
    
    /* Free allocated buffer */
    free( array[0] );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
#include<stdio.h> #include<zlib.h> #include<unistd.h> #include<string.h> int main(int argc, char *argv[]) { char *path=NULL; size_t size;
#include<string.h> #include<stdio.h> int main() { char *p; strcpy(p,hello world); } Well,Does it show undefined
#include<stdio.h> #include<string.h> int main() { char a[]=aaa; char *b=bbb; strcpy(a,cc); printf(%s,a); strcpy(b,dd); printf(%s,b); return
Here is my code #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char
Very simple example: #include <string> #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc,
#include <iostream> #include <string.h> using namespace std; int main() { int e=0; int b=0;
How print format string passed as argument ? example.cpp: #include <iostream> int main(int ac,

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.