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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:32:37+00:00 2026-06-19T00:32:37+00:00

How do I assign a value from struct to another. Here is my code.

  • 0

How do I assign a value from struct to another. Here is my code.

I believe I am assigning the address of the struct which is what I don’t want to do. I want to assign the values of “temp” to ‘a’. I commented the section I need help on. Thanks

Also off-topic.. How do I post code without having to indent myself every line, line-by-line?

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

#define SIZE 10

typedef struct dynArrStruct
{
    double value1;
    int value2;
    int value3;
}dynArr;


void init(dynArr* a)
{
    dynArr temp;
    temp.value1 = (double)(rand()) * rand() / rand();
    temp.value2 = rand()%100;
    temp.value3 = rand()%1000;
    printf("In init(): value1: %14.5f, value2: %6d, value3: %6d\n",
            temp.value1, temp.value2, temp.value3);
    a = &temp;  // THIS LINE
}


int main(int argc, char** argv)
{
    int i;
    dynArr a1[SIZE];
    dynArr* a2[SIZE];

    for(i = 0; i < SIZE; i++)
    {
        init(&(a1[i]));
        init(a2[i]);
    }
    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-19T00:32:38+00:00Added an answer on June 19, 2026 at 12:32 am

    Use assignment (assuming a is pointing to valid memory):

    *a = temp;
    

    Note this works because the members of the struct are not pointers. Using this technique (or memcpy()) on a struct that contained member pointers would result in both structs having members pointing to the same address, which may be dangerous (possibly resulting in dangling pointers). If a struct contains pointers then a deep copy of the struct is required (allocating memory for the member pointers and copying the pointed to objects in the source struct to the allocated memory in the destination struct).

    Also, in this case temp is superfluous and you just assign values directly to the members of a.

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

Sidebar

Related Questions

I want to assign a value from a select box to an input field
I want to return several values from a perl subroutine and assign them in
I am trying to assign a value from a query result to a session
I'm trying to assign a variable from a html text input value and then
I'm trying to assign char* values into a queue from a loop. I keep
I want to assign the value in QString to a const std::string QString qfile(some
can we assign null value to struct type of variable? struct MyStruct { }
I created a 2d array of structures and now I want to assign values
I am trying to assign a value from my database to a variable inside
In our code we have a big struct of mixed types, and want to

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.