Array of structs - deleting/adding elements and printing

Hi,

For the below code

1
2
3
4
5
6
7
8
9
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 doing this:

1
2
3
4
5
6
7
8
9
10
		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


or using a for loop like this?
1
2
3
4
5
6
7
	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 dynmaic array of structs? If so, what would be the simplest syntax to do that?

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

but in C? I'm guessing its malloc and free
Last edited on
While using C, I don't know of a way to "overload" the printf function to display your struct. I can't remember if structs allow for functions (I don't believe they do), but in C++ classes/structs, you have the option of creating a Display() method that can print your each of your structures.

As for deleting/creating objects, I'm not very familiar with C in that sense either, but I do believe your only option is malloc/free.
What I would do is start off with an array with "this number of elements" and for each cell I would add a flag to indicate whether the entry is valid or not. Say you want to store 2 elements, and maybe more later.

This comes is practical in a sense that you don't have to allocate new space, copy data, free old space every single time you want to add an element. This is what sophisticated containers do actually.
You can either specify a suitable maximum value for the size of the array or dynamically allocate a memory by using malloc, realloc, and free. realloc copies elemebts of the source memory to the destination memory.
To print elements of the array you should of course use a loop. To delete an element you should reorder your array such a way that the deleted element would be the last in the array and then use realloc function.
Last edited on
you can use a function that take orderslip argument and print it.

1
2
3
4
5
6
7
void printBook( struct Books book )
{
   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book book_id : %d\n", book.book_id);
}


and for your second problem you should use malloc and free
and here is a simple tut about them
http://www.tutorialspoint.com/cprogramming/c_memory_management.htm

i hope that my replay was helpful for u

As for deleting/creating objects, I'm not very familiar with C in that sense either, but I do believe your only option is malloc/free.


Malloc isn't so bad. Three things to know, 1) It returns a void pointer (so needs casting), 2) it needs an actual Byte amount, 3) The value of the pointer is NULL if not allocated:
1
2
3
4
int *i = NULL;
int howMany = 5;
i = (int*) malloc (howMany * sizeof(int));
if (i == NULL) printf("couldn't do it\n");


Free is easier than delete, you never use []
1
2
free (i);
i = NULL

Last edited on
Topic archived. No new replies allowed.