Hey all. I'm having a bit of problem with structs. Say we have a struct comprised of
some strings and ints.One of the strings is a keycode,which is used for hashing.So my program reads from a file some information,creates a struct and using that keycode it places the struct on a specific position on a struct array (so far everything ok).
Now,the second functionality should be,given a specific keycode,which may or may not exist in one of the arrays of the struct,if there is a struct with that keycode the struct containing it must be deleted.
So,question is,how do i set a struct to null?
Using printf i see that indeed the program works and finds the keycodes if they exist.
So,to put it simple,how do i set myArray[index] to null?
What i tried is declare a new myStruct,named cat2,then use the appendEntry(struct myStruct ca,int index) method.The appendEntry method copies
every field of "cat2" to myArray[index]. (i also tried myArray[index]=cat2; but same result.).Shouldn't every string field of cat2 be "" by default???
I print myArray using :

void printAll (void) {
           for (int i=0;i<1000;i++) {
               if (myArray[i].info[0]!='\0') {
               printf("\n%s-%d-%d-%s-%s\n", myArray[i].name,myArray[i].number1,myArray[i].num2,myArray[i].lName,myArray[i].info);
               }
           }
      }

So the problem is if i don't use the delete feature it prints eg
abcd-20-25-qwer-poi
and after using the delete feature (supposing i give "abcd" as key)
-20-25-qwer-poi
when it shouldn't be printed at all. I didn't post any code because its kind of messy,and because i'm guessing this is a noob mistake of mine,as a matter of handling structs.
Thanks in advance for your time!

Recommended Answers

All 4 Replies

Hi Chester I have a vague idea of what your question is , but before I dispense any advise can you please clarify your question ?

PS: Some random thoughts which may or may not help you
1. If you have an array of structs, there is no way to set arr[index] = null. What you can do is add a member to the struct definition called as isValid. Later if you want to set arr[index] = null, you can instead set arr[index].isValid = 0. In your print functions you can ignore all arr[index] where isValid is set to 1

2. If you have 2 struct variables t1 and t2 then if you write t1 = t2 , all of t1's contents will be replaced by t2 contents

Hope this helps

>>So,question is,how do i set a struct to null?
In an array, you can't. One way to do it is to add a deleted flag to the structure and set it to true if the struct is to be considered deleted. Another option is to set the first byte of the key field string to 0 to identify it as a deleted struct. A third option is to move all the remaining structs up one slot in the array, overwriting the struct you want to delete. That will leave an empty struct at the end of the array.

>>So,to put it simple,how do i set myArray[index] to null?
You can't, unless myArray is an array of pointers, such as MyStruct* myArray[MAX_ROWS]; In that case you call malloc() to allocate each of the rows in the array, and free() to delete one.

IMO a better solution would be to use a linked list, not an array. That will let you actually delete the node in the list and will leave no unused nodes.

Thank you both very much.Abhi i did what you said,i declared a myStruct,set all strings to "" and numbers to 0.Then i copy this empty struct when i want to delete something.The program was supposed to delete data,so i suppose this is best way.
AncientDragon you were right.I hadn't thought of it.When i deleted a struct,but then later on searched for another struct to delete,my search would stop at the "gap" created by the previous deletion. So there was a major bug.Thanks!

>>When i deleted a struct,but then later on searched for another struct to delete,my search would stop at the "gap" created by the previous deletion

Yup, you have to teach your program to ignore deleted structs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.