Thread: Delete data from struct

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    29

    Delete data from struct

    Hi everyone i have question how to delete data from struct. I made program for storing picture info and now i want to delete data for pictures that are smaller than 20byte.
    Code:
    struct picture {    
        char name[50];
        char extense[10];
        float size;
        char location[50];
        int position;
    };
    int main()
    {
        struct picture S[100];
        int i,n;
        printf("Enter number of pictures:");
        scanf("%d",&n);
        for(i=0; i<n; i++)
        {
            printf("Enter name of %d picture",i+1);
            scanf("%s",S[i].name);
            printf("Enter extension of %d picture",i+1);
            scanf("%s",S[i].extension);
            printf("Enter size of %d picture(in byte!)",i+1);
            scanf("%f",&S[i].size);
            printf("Enter location of %d picture",i+1);
            scanf("%s",S[i].location);
            printf("Enter position of %d picture (1-horizontal 2-vertical)",i+1);
            scanf("%d",&S[i].positon);
    }
    
    }
    Last edited by John5; 03-13-2016 at 11:58 AM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I assume you mean "delete an element from an array", not "delete data from a struct" (which doesn't make sense).
    To remove an element from an array you need to move the latter part of the array one element-length back.

    BTW, what you're calling "position" of the picture is actually called "orientation" (horizontal is landscape, vertical is portrait).

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    Yes i mean deleting element of array, can you give me some example how to delete element

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Code:
    #include <stdio.h>
    
    int main(void) {
        int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        int sz = sizeof a / sizeof *a;   // number of elements in the array.
    
        // print array
        for (int i = 0; i < sz; i++)
            printf("%d ", a[i]);
        putchar('\n');
    
        // Delete element at offset 3
        for (int i = 3; i < sz - 1; i++)
            a[i] = a[i+1];
        sz--;
    
        // print array again
        for (int i = 0; i < sz; i++)
            printf("%d ", a[i]);
        putchar('\n');
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    I made this but it doesn't delete them
    Code:
    int delete_picture(struct picture S[],int n)
    {
    	int i=0;
    	int z=0;
    	for(i=0; i<n-1; i++)
    	{
    		if(S[i].size<20)
    		{
    			S[i].size=S[i+1].size;
    			n--;
    			z++;
    		}
    	}
    	printf("Deleted pictures:%d",z);
    }

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The "for()" loop you have cycles through each element to find the ones that contain the desired criteria (a size of < 20).

    Once such an element is found, you then need another loop to shift elements, as shown in the example by algorism.

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    I try that but it doesn't delete struct data for that element.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Post your code. In fact, post a simplified complete example that compiles and illustrates the problem.

  9. #9
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    I want to delete array element that have picture size smaller than 20.
    Code:
    struct picture {    
    char name[50];
        char extension[10];
        float size;
        char location[50];
        int position;
    };
    
    void Print(struct picture S)
    {
        printf("%s %s %d %f %s %d",S.name,S.extension,S.asd,S.size,S.location,S.positon);
    }
    
    int delete_picture(struct picture S[],int n)
    {
        int i=0;
        int z=0;
        for(i=0; i<n-1; i++)
        {
            if(S[i].size<20.00)
            {
                S[i].size=S[i+1].size;
                z++;
                n--;
            }
        for(i=0; i<n; i++)
        {
            Print(S[i]);
        }
    printf("Delited pictures %d",z);
    }
    
    
    
    
    
    int main()
    {
        struct picture S[100];
        int i,n;
        printf("Enter number of pictures:");
        scanf("%d",&n);
        for(i=0; i<n; i++)
        {
            printf("Enter name of %d picture",i+1);
            scanf("%s",S[i].name);
            printf("Enter extension of %d picture",i+1);
            scanf("%s",S[i].extension);
            printf("Enter size of %d picture(in byte!)",i+1);
            scanf("%f",&S[i].size);
            printf("Enter location of %d picture",i+1);
            scanf("%s",S[i].location);
            printf("Enter position of %d picture (1-horizontal 2-vertical)",i+1);
            scanf("%d",&S[i].positon);
    }
    delete_picture(S[i],n);
    
    }
    Last edited by John5; 03-14-2016 at 11:07 AM.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Notice how algorism's example in post #4 uses a loop to shift elements. Your code is missing this.

    You already have a loop to go through each element and check if a condition is found - when it is, then you need an inner loop to do the element shifting.

  11. #11
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    Can you show me how to make that inner loop for shifting ?

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You have been given a concrete example in post #4. Except that, instead of starting at the arbitrary value of "3" as per that example, you want to start at the position where the condition has been found, which is "i" in your code. Note that this will require an additional indexing variable (you don't want to lose the value of "i", so you need a new index variable for shifting in the inner loop).

    To get you started:

    Code:
    for(i=0; i<n-1; i++)
    {
        if(S[i].size<20.00)
        {
            for(j=i; // ...

  13. #13
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    I try this but it doesn't work
    Code:
    nt delete_picture(struct picture S[],int n){
    	int i=0,j=0;
    	int z=0;
    	for(i=0; i<n-1; i++)
    	{
    		if(S[i].size<20)
    		{
    			for(j=i;j<n; j++)
    			{
    			S[j].size=S[j+1].size;
    			z++;
    			n--;
    		}
    		}
    	for(i=0; i<n; i++)
    	{
    		Print(S[i]);
    	}
    }

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should spend more than (literally) five minutes trying this before posting "it doesn't work".

    I also note that the latest "complete" program you posted (post #9) didn't compile due to errors.

    Start with a simple program that's easy to work with - strip out any unnecessary features (such as user input) so you can focus on the problem at hand.

    Code:
    #include <stdio.h>
    
    struct picture {
        char name[50];
        char extension[10];
        float size;
        char location[50];
        int position;
    };
    
    void Print(struct picture S)
    {
        printf("%s %s %f %s %d\n",S.name,S.extension,S.size,S.location,S.position);
    }
    
    int main(void)
    {
        struct picture S[6] = {
            { "Picture-0", "png", 10.0, "C:\\pictures_0", 0 },
            { "Picture-1", "png", 20.0, "C:\\pictures_1", 1 },
            { "Picture-2", "png", 30.0, "C:\\pictures_2", 0 },
            { "Picture-3", "png", 40.0, "C:\\pictures_3", 1 },
            { "Picture-4", "png", 50.0, "C:\\pictures_4", 0 },
            { "Picture-5", "png", 60.0, "C:\\pictures_5", 1 },
        };
        int i;
    
        for(i = 0; i < 6; i++)
            Print(S[i]);
    
        return 0;
    }
    There, now you have simplified functional program that you experiment with. Once you get the delete function working, that function can be moved back into your main project.

    Code:
    S[j].size=S[j+1].size;
    If you want to get rid of an entire element, you need to overwrite that element, not just a member of that element.

    Code:
    S[j] = S[j+1];
    There are also some subtle nuances you need to observe, regarding the limit and indexing variables. A little bit of thought (and some pencil and paper to help sketch these thoughts) should help you see these.

    Also, you should not be printing the array from within the delete function - try printing all values of the array in "main()" both before and after the delete function call, so you can see how the data has been changed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using data struct and I got an error in struct function
    By abrofunky in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2012, 07:47 PM
  2. how to delete data
    By Kinshara in forum C Programming
    Replies: 11
    Last Post: 11-03-2009, 08:17 AM
  3. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  4. add-delete data
    By mufanz in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2006, 10:49 AM
  5. delete memory in a struct destructor
    By JeremyCAFE in forum C++ Programming
    Replies: 5
    Last Post: 03-12-2006, 11:34 AM