Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:40:51+00:00 2026-06-10T14:40:51+00:00

For the below code struct orderSlip { char source[64]; char destination[64]; char item[64]; int

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:40:53+00:00Added an answer on June 10, 2026 at 2:40 pm

    “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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is my code snippet struct encode { char code[MAX]; }a[10]; int main() {
consider the code below: #include list.h struct List { int size; int* data; };
I am executing below code. int main() { struct tm storage={0,0,0,0,0,0,0,0,0}; char *p =
my test code is below: main1.c: #include <stdio.h> extern struct tt ; int main()
Given the code below: typedef struct {int a;} test_t; arbitrary_t test_dosomething(test_t* test) { if
The code below is passing the struct variable: struct someStruct { unsigned int total;
In the code below: template<typename T> struct X {}; int main() { X<int()> x;
Is something like the code below valid? struct foo { int a; int b[];
I have tested on stl vector with code below: struct structA{ char charArray[256]; }
For the code below public struct Person { public int ID; public static bool

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.