1

How to assign previously read element of a struct to an empty (new) array?

In the following example, after each input element of struct2, it should be stored to a new array arr.

This example gives SIGSEGV segmentation fault.

Could someone point out how to resolve this?

EDIT:

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

typedef struct
{
    int id;
    char name[30];
    float price;
}PRODUCT;

typedef struct
{
    int prodNumRep;
    PRODUCT *productsRep;
    float *quantityRep;
}REPOSITORY;

void inputProd(PRODUCT *prod)
{
    printf("ID: ");
    scanf("%d",&prod->id);
    printf("Name: ");
    scanf("%s",prod->name);
    printf("Price: ");
    scanf("%f",&prod->price);
}

void inputRep(REPOSITORY *rep)
{

    printf("REPOSITORY: \n");
    printf("Number of products: ");
    scanf("%d",&rep->prodNumRep);

    rep->productsRep=calloc(rep->prodNumRep,sizeof(*rep->productsRep));
    rep->quantityRep=malloc(rep->prodNumRep*sizeof(float));

    //new array
    REPOSITORY *arr;
    arr=(REPOSITORY*)malloc(rep->prodNumRep * sizeof(REPOSITORY));

    int i;
    for(i=0;i<rep->prodNumRep;i++)
    {
            printf("%d. product: \n",i+1);
            inputProd(rep->productsRep+i);
            printf("Quantity: ");
            scanf("%f",&rep->quantityRep[i]);

            //assign struct2 (previously read with inputStruct1) to   array - SIGSEGV segmentation fault
            arr->productsRep[i]=rep->productsRep[i];
            arr->quantityRep[i]=rep->quantityRep[i];

    }
}

int main()
{
    REPOSITORY *rep;
    rep=(REPOSITORY *)malloc(sizeof(REPOSITORY));
    inputRep(rep);

    return 0;
}
3
  • 1
    You really need to come up with more meaningful variable names. This code is barely readable. Commented Jun 30, 2016 at 10:42
  • What is inputStruct1() ? Commented Jun 30, 2016 at 10:46
  • Did you run a static code analysis tool to find the root cause? These tools read simply read code and detect potential issues. As a start, you could cppcheck give a try. It is easy to use and open source. Commented Jun 30, 2016 at 19:36

2 Answers 2

3

Your problem is that arr->productsRep[i] is actually attempting to de-reference the pointer at productsRep, but you've not allocated any memory to productsRep. I see what you're trying to do, but I think think you need to restructure your logic and flow of execution.

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

3 Comments

Array productsRep is allocated with rep->productsRep=calloc(rep->prodNumRep,sizeof(*rep->productsRep));
@user300044: You should always check the return value of scanf(). It is entirely possible that rep->prodNumRep is uninitialized. Also, don't cast malloc().
@user300044: And yes, rep->productsRep is allocated, but arr->productsRep is not.
1

There is no need to declare a new array in the function. Remove from the function the following statements

//new array
REPOSITORY *arr;
arr=(REPOSITORY*)malloc(rep->prodNumRep * sizeof(REPOSITORY));

and

        //assign struct2 (previously read with inputStruct1) to   array - SIGSEGV segmentation fault
        arr->productsRep[i]=rep->productsRep[i];
        arr->quantityRep[i]=rep->quantityRep[i];

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.