-1

I am trying to write a C program where based on the size of the member of the structure ,that many members should be read into an array.

As you can see in the below code,based on the value of the bookPtr->size,I will add that many members into the array.

If bookPtr->size is 3 , I will read all the members x,y and z.

If bookPtr->size is 2 , I will read the members x and y.

If bookPtr->size is 1 , I will read the members x.

If bookPtr->size is 0 , no members are read.

But I want to optimize the code further.Is there a way so that code length is reduced.Thanks.

    #include<stdio.h>

    typedef struct
    {
        int x;  
        int y;
        int z;
        int size;
    }Book;



     void Get (Book* bookPtr)
    {
        Book mybook;
        int size = bookPtr->size;
        if(size == 3)
        {
        mybook->x = bookPtr->x;
        mybook->y = bookPtr->y;
        mybook->z = bookPtr->z;
        }

        else if(size == 2)
        {
            mybook->x = bookPtr->x;
            mybook->y = bookPtr->y; 
        }

        else if(size == 1)
        {
            mybook->x = bookPtr->x;
        }
        else
        {

        }

    }

    int main()
    {

        Book bookPtr;
        bookPtr.x =  2 ;
        bookPtr.y =  2 ;
        bookPtr.z =  20 ;   
        bookPtr.size = 2 ;

        Get(&bookPtr);
        return 0;

    }
  • 2
    Not unless you [use a single array of `int`](https://stackoverflow.com/a/1829927/100754) instead of three separate variables. – Sinan Ünür Jun 20 '17 at 17:00

3 Answers3

3

like this

void Get (Book* bookPtr){
    Book mybook;
    switch(mybook.size = bookPtr->size){
    case 3: mybook.z = bookPtr->z;
    case 2: mybook.y = bookPtr->y;
    case 1: mybook.x = bookPtr->x;
            break;
    default:
        break;
    }
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

You can really optimize code by using array inside structure, But if you don't want to change the code of structure then below code looks cool.

void Get (Book* bookPtr)
{
    Book mybook;
    int size = bookPtr->size;
    if(--size >= 0) {
        mybook->x = bookPtr->x;
    } 
     if(--size >= 0){
        mybook->y = bookPtr->y; 
    }
    if(--size >= 0)
    {
        mybook->z = bookPtr->z;
    }
}
  • Hi Amal,Thanks for the answer.Yeah I agree using arrays ,code can be optimized.But I want to know how it can be done in this scenario.Do ternary operators could be of help? – Gopal Krishna Jun 20 '17 at 17:14
  • No, I don't think ternary operator could make help in this case.If u got anything please lemme know. – amal srivastava Jun 20 '17 at 17:21
0

I would recommend putting all the related int fields in an array.

#include<stdio.h>

struct Book {
    int data[3];
    int size;
};

void
Print(struct Book *p)
{
    int i, sz = p->size;
    for (i = 0; i < sz; ++i)
    {
        printf("data[%d] = %d\n", i, p->data[i]);
    }
    return;
}

void
Get(struct Book *p)
{
    int i, sz = p->size;
    struct Book mybook;

    mybook.size = sz;

    for (i = 0; i < sz; ++i)
    {
        mybook.data[i] = p->data[i];
    }

    Print(&mybook);

    return;
}


int
main(void)
{
    struct Book book;

    book.data[0] = 2;
    book.data[1] = 2;
    book.data[2] = 20;
    book.size = 2;

    Get(&book);

    return 0;
}

This means the code can accommodate arbitrary numbers of those int fields in case requirements change in the future, instead of infecting every part of the code with the assumption that there are three fields named x, y, and z in the Book structure.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • Hi Sinan,Thanks for the answer.Yeah I agree using arrays ,code can be optimized.But I want to know how it can be done in this scenario.Is there any other alternative? – Gopal Krishna Jun 20 '17 at 17:20