Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing a struct with another struct

Tags:

c

copy

struct

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;
}
like image 895
ShadyBears Avatar asked Sep 07 '25 11:09

ShadyBears


2 Answers

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.

like image 124
hmjd Avatar answered Sep 10 '25 04:09

hmjd


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
}

temp is a local variable with automatic storage duration. It ceases to exist when init returns.

Fortunately, the pointer a that is made to point to temp also ceases to exist, so init doesn't create dangling pointers for your programme.

structs are assignable, so if the argument points to valid memory, you can just initialise the pointee with

*a = temp;

That would be okay for

init(&(a1[i]));

since &a1[i] points to allocated memory, but not for

init(a2[i]);

since a2[i] is an uninitialised pointer.

If you want to allocate memory in init, you'd have to pass a dynArr**, but then you can't use it for the first case. So you ought to allocate memory to a2[i] before calling init for the second case.

like image 43
Daniel Fischer Avatar answered Sep 10 '25 05:09

Daniel Fischer