Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of structs - deleting/adding elements and printing

For the below code

struct orderSlip
{
    char source[64];
    char destination[64];
    char item[64];  
    int position;
};
//global
struct orderSlip data[100];

Is there another way of printing out the data for each element other than these methods below:

printf("%s\n",data[0].source);
    printf("%s\n",data[0].destination);
    printf("%s\n",data[0].item);
    printf("%i\n\n", data[0].position);

    printf("%s\n",data[1].source);
    printf("%s\n",data[1].destination);
    printf("%s\n",data[1].item);
    printf("%i\n", data[1].position);

etc

for(int n = 0; n< 3; n++)
{
    printf("%s\n",data[n].source);
    printf("%s\n",data[n].destination);
    printf("%s\n",data[n].item);
    printf("%i\n\n", data[n].position);
}

For deleting and adding, do I have to make a dynamic array of structs? If so, what would be the simplest syntax to do that? Something like this c++ code

int * bobby;
bobby = new int [5];
delete bobby[5];

but in C? I'm guessing it has do with malloc and free

like image 869
Dog Avatar asked Aug 22 '12 03:08

Dog


People also ask

Can arrays hold structs?

An array is a collection of data items of the same type. Each element of the array can be int, char, float, double, or even a structure. We have seen that a structure allows elements of different data types to be grouped together under a single name.

How do you delete an element from an array of structures in C++?

You can't actually delete an item from an array, since arrays have a fixed size. The usual way to remove an item from a fixed-size container is to shift all subsequent elements down one space to overwrite the item you don't want.

How do you access an array in a struct?

Array elements are accessed using the Subscript variable, Similarly Structure members are accessed using dot [.] operator. Structure written inside another structure is called as nesting of two structures. Nested Structures are allowed in C Programming Language.


1 Answers

"For deleting and adding, do I have to make a dynamic array of structs? If so, what would be the simplest syntax to do that? Something like this c++ code"

Not if you know that you will never have more than x amount of items or at least check to make sure you aren't exceeding what you planned was the max. Then you can use your static array.

Adding only requires you to have a variable that keeps track of how many items are in the array:

void add_item(struct orderSlip *p,struct orderSlip a,int * num_items)
{
   if ( *num_items < MAX_ITEMS )
   {
      p[*num_items] = a;
      *num_items += 1;
   }
}

Deleting from a static array would require a for loop that would move the items above it down one and decrementing the int keeping track of the number of items.

void delete_item(struct orderSlip *p,int *num_items, int item)
{
   if (*num_items > 0 && item < *num_items && item > -1)
   {
      int last_index = *num_items - 1;
      for (int i = item; i < last_index;i++)
      {
         p[i] = p[i + 1];
      }
      *num_items -= 1;
   }
}

You could simplify printing the struct by passing it to a function that does the work.

void print(const struct orderSlip  *p);

or

void print(const struct orderslip s);

optionally

void print(const struct orderslip s, FILE *fp);

or

void print(const struct orderslip *p, FILE *fp)
{
   fprintf(fp,"%s\n",p->source);
    ...
}

and

void print_all(const struct orderSlip *p, int num_items)



//global
struct orderSlip data[MAX_ITEMS];
int num_items = 0;



int main(void)
{
...
       print_all(data,num_items);                                       
       strcpy(a.source,"source 2");
       strcpy(a.destination,"destination 20");
       strcpy(a.item,"item xyz");
       a.position = 99;
       add_item(data,a,&num_items);
       print_all(data,num_items);
       delete_item(data,&num_items,0);
       print_all(data,num_items);
like image 187
Scooter Avatar answered Sep 28 '22 18:09

Scooter