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;
}
user300048
  • 133
  • 7
  • 1
    You really need to come up with more meaningful variable names. This code is barely readable. – Lundin Jun 30 '16 at 10:42
  • What is `inputStruct1()` ? – user3078414 Jun 30 '16 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. – orbitcowboy Jun 30 '16 at 19:36

2 Answers2

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.

LordWilmore
  • 2,829
  • 2
  • 25
  • 30
  • Array productsRep is allocated with rep->productsRep=calloc(rep->prodNumRep,sizeof(*rep->productsRep)); – user300048 Jun 30 '16 at 11:06
  • @user300044: You should *always* check the return value of `scanf()`. It is entirely possible that `rep->prodNumRep` is uninitialized. Also, [don't cast `malloc()`](http://stackoverflow.com/a/605858/485088). – Tim Čas Jun 30 '16 at 11:21
  • 1
    @user300044: And yes, `rep->productsRep` is allocated, but `arr->productsRep` is not. – Tim Čas Jun 30 '16 at 11:26
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];
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335