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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:02:35+00:00 2026-06-11T19:02:35+00:00

I am trying to copy a string into another char pointer variable using strcpy

  • 0

I am trying to copy a string into another char pointer variable using strcpy function.But I always get segmentation fault.Here is my code.

/* strcat example */
#include <stdio.h>
#include <string.h>

int main()
{
  char str[100]="";
  char *pch3;
  strcpy(pch3,"Ravi");
  //strcat(str,pch3); //Segmnetation fault.
  puts(pch3);
  return 0;
}

If I do the same thing in this one I still get segmentation fault.

 else
      {
         misc_rec_cnt++;
         fp1=fopen("breast-cancer-wisconsin-miscellaneous.data","a");
         fprintf(fp1,"%s",line2);
         fclose(fp1);
         fp2=fopen("missingSCNs.data","a");
         pch2=strtok(line2,",");
         fprintf(fp2,"%s\n",pch2);
         fclose(fp2);

         //pch3=(char *)malloc(sizeof(char)*strlen(line3));
         pch3 = strtok(line3,",");
         while(pch3!=NULL)
         {
             if(strcmp(pch3,"?") == 0)
             {
                strcat(str1,"0");
                strcat(str1,",");
             }
             else
             {
                //strcat(str1,pch3);
                strcat(str1,",");
             }
             pch3 = strtok(NULL,",");
         }
         strlen1=strlen(str1);
         memcpy(str2,str1,strlen1-1);
         fp3=fopen("breast-cancer-wisconsin-miscellaneous-cleansed.data","a");
         fprintf(fp3,"%s\n",str2);
         fclose(fp3);
      }
  • 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-11T19:02:36+00:00Added an answer on June 11, 2026 at 7:02 pm

    pch3 is an unallocated pointer, so you’re writing data to a location that doesn’t exist. Did you mean to assign it to str?

    char str[100]="";
    char *pch3;
    pch3 = str;
    strcpy(pch3,"Ravi");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to copy data from table into another by using a SELECT
Hey, I am trying to convert NSString to a C string using cStringUsingEncoding but
I'm trying to copy a string using 16-bit assembly. I have (among other things)
I am trying to copy certain parts of a string into other, new strings,
I am trying to copy values in string array into a DataRow , it
I'm trying to compress any given string to a shorter version, copy paste-able compressed
I'm trying to copy files to C:\Windows\System32 using cygwin bash. When I copy files
I'm trying to copy the <options> HTML Codes from one select list to another.
Im trying to copy and paste a table from excel into a word document.
I'm trying to copy a custom object from a RDC window into host (my

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.