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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:19:38+00:00 2026-06-17T12:19:38+00:00

Normally making a copy of a struct is as simple as just using the

  • 0

Normally making a copy of a struct is as simple as just using the = operator and the compiler generates code to copy the struct over for you. However, for this part the function has to return a pointer to a struct so I’ve been working with that the whole time only to get to a part where I realized that everything I tried wasn’t copying the struct over correctly.

A basic example of my problem is

typedef struct command_stream *command_stream_t;
command_stream_t ty = (command_stream_t) malloc(sizeof(struct command_stream));
command_stream_t yy;

do some code
//ty contains a variable words which is an array of strings

*yy = *ty;
 ty->words = NULL; //set to null to see if yy still contains a copy of the struct
 printf("%s", yy->words[0]);

I get a segmentation fault here. However, if I change it so it isn’t a pointer

typedef struct command_stream command_stream_t

yy=ty;
ty.words = NULL;
printf("%s", yy.words[0]);

This works just fine! I’m not entirely sure how I should do the same thing for pointers, and I don’t really want to go back and change 500+ codes of lines…

  • 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-17T12:19:39+00:00Added an answer on June 17, 2026 at 12:19 pm

    Your yy pointer is never initialized.

    You should allocate enough memory there to hold the struct and then either copy with * like you did, or use memcpy with the pointers and size.

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

Sidebar

Related Questions

I was reading about this scenario where making use of the C# using statement
I'm making a simple script that works on Jinja2 templates. Right now it's just
I'm talking about using the wiki syntax. Normally, I create an article by making
Normally, if I want to start a new activity I can use StartActivity(typeof(foo)); This
Normally the XML files I have to parse are like this: <row id=1> <title>widget<title>
I was wondering, I normally use std::string for my code, but when you are
Ok, I am making a program using SymPy. I use sympy.Eq, sympy.solve, and sympy.Symbol.
I have X number of RadioButtons (Silverlight). Normally this value is between 2-5. Call
Coming from Java or Python I am used to making such constructs and normally
I am making an application using c#.net. It contains a filesystem minifilter driver also.

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.