0

I want to assign a1 into ana[0], ana1 only inside the storeDataHere function.

Inside saveData function, just need to pass each data_array[0] to storeDataHere function.

#include <stdio.h>
#include <string.h>
struct Analysis{
    int a;
    int b[10];
};

struct Analysis ana[2];

void storeDataHere(void* res)
{
    // initialize a1;
    struct Analysis a1;
    a1.a = 1;
    memset(a1.b, 0, 10*sizeof(int));

    for(int i=0; i<10; i++)
        a1.b[i] = 1;


    // store a1 into ana[0], ana[1];
    struct Analysis temp = res;
    temp = a1;

}

void saveData(void* data, size_t size)
{

    struct Analysis *data_array = data;
    // pass two struct elements to storeDataHere;
    storeDataHere((void*)data_array[0]);
    storeDataHere((void*)data_array[1]);
}



int main()
{
    // pass struct array ana to saveData
    saveData( (void*)ana, sizeof(ana[0]));

    for(int i=0; i<2; i++)
    {
        printf("\n%d\n", ana[i].a);
        for(int j=0; j<10; j++)
            printf("%d", ana[i].b[j]);
    }

    return 0;
}

Here is the output with errors:

enter image description here

How to solve this if I keep this structure of function? (Not to change the function param)

5
  • you probably need to change this struct Analysis temp = res; to this struct Analysis* temp = res; Commented Sep 10, 2016 at 5:31
  • Another of your classmates has also inquired about this assignment: stackoverflow.com/questions/39419832/…. I suggest you read his question and its answer, as they may be helpful. Commented Sep 10, 2016 at 5:35
  • also, instead of storeDataHere((void*)data_array[1]);, try to use storeDataHere((void*)(data_array + sizeof(struct Analysis)*offset); Commented Sep 10, 2016 at 5:35
  • Also, I think that converting your void * to a struct Analysis * in function saveData() is likely missing the point of the exercise. One would declare the function parameter as a struct Analysis * in the first place if the function was meant to be specific to that data type. Commented Sep 10, 2016 at 5:41
  • @JohnBollinger: Interesting: you've found a third person working on this exercise — there's also stackoverflow.com/questions/39404854/…? by another user, mdadurian, with the same struct Analysis and passing the data around via void *. I used search term '[c] struct analysis is:q' and ordered by newest. I'd come across Patrick and mdadurian (and this homework problem) before — and not Jam. Commented Sep 10, 2016 at 6:19

3 Answers 3

2

See answer here:

https://repl.it/D2Cd

// use struct Analysis* instead of void*
//
void storeDataHere(struct Analysis* res)
{
    int i;
    // initialize a1;
    struct Analysis a1;

    a1.a = 1;
    // memset(a1.b, 0, 10*sizeof(int)); no need to memset cause you set later

    for (i = 0; i < 10; i++) 
    {
        a1.b[i] = 1;
    }

    // store a1 into ana[0], ana[1];
    *res = a1; // assign value of pointer res to a1 
}

// pass array struct to function
void saveData(struct Analysis data[])
{
    struct Analysis *data_array = data;
    // pass two struct elements to storeDataHere;
    storeDataHere(&data_array[0]);
    storeDataHere(&data_array[1]); // pass pointer of each element to StoreDataHere
}

See the link for more details. Happy coding!!

Sign up to request clarification or add additional context in comments.

Comments

1

Change (you need to cast void pointer and then dereference):

struct Analysis temp = res;

to

struct Analysis temp = *((struct Analysis *)res);

Another correction (cast and pass the correct type):

storeDataHere((void*)data_array);

Comments

1

You're trying to assign a pointer to an object, that's impossible, to have a copy you should:

// Copy values
struct Analysis temp = *((struct Analysis*)res);

// or
// Copy pointer
struct Analysis *temp = res;

Next problem is, passing elements of data_array such as data_array[0] to the function (according to your code), you must pass whole structure:

storeDataHere((void*)data_array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.