Thread: Removing the contents of a variable

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    7

    Removing the contents of a variable

    Hello!

    I'm creating a database for uni and one of the objectives is to delete some information, which therefore necessitates the removal of the contents of a defined varible type using typedef struct; one of the 'parts' within the typedef is 'first' which is a string. I tried the following:

    Code:
    data[delrecno].first = "";
    However that does not work! Visual Studio says 'expression must be a modifiable lvalue'.

    Is there any way of completley removing the contents of the variable?

    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > one of the 'parts' within the typedef is 'first' which is a string. I tried the following:
    Well a 'string' in C doesn't tell us much, since there is no such thing as a string type in C.

    But you can store things like strings in various ways.

    Perhaps you have one of these.
    Code:
    typedef struct foo {
        char *first;
    };
    typedef struct foo {
        char first[100];
    };
    Though from the error message, the second seems far more likely.

    You should perhaps try
    strcpy(data[delrecno].first,"");

    > Is there any way of completley removing the contents of the variable?
    Whilst copying an empty string is functional enough to allow you to re-use the memory later on, it does leave the rest of the data intact for prying eyes to peek at.
    If first really is an array, then
    memset( data[delrecno].first, 0, sizeof(data[delrecno].first) );
    ensures that the memory associated with the string has been fully erased.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    It depends on what you mean by "completely remove". A variable is a named chunk of memory, and that chunk of memory will obviously always have something in it (even if it's random garbage). Unless you want to rip the ram out of the machine, you can't completely remove it!

    It looks like first is an array of characters. In that case you have two options. One is to simply set it's first element to the null character ('\0'), which indicates an empty C-style string. With this option, although the string is "empty", the old contents beyond the first character are still there and if you write the entire array of characters out to a file that old data will still be recoverable (a possible security concern).

    The other option is to overwrite all the characters with '\0' characters (memset(first, 0, sizeof first)) or possibly space characters, depending on exactly what you want. That way, if you write the array back out none of the data will be recoverable.

    Another possibility is to not write out the record at all. It depends on exactly what you mean by "database". Are you reading the entire thing into an array, operating on it, and then writing the array back out again? In that case, why bother writing out dead records at all? If you are fseek'ing and writing records in place, that's another story.

  4. #4
    Registered User
    Join Date
    Jan 2017
    Posts
    7
    In a nutshell, what I'm trying to do for my uni project is to create a database of people which makes use of the 'typedef struct' command which is set to contain first name, last name, date of birth etc. In my code I have created an array of 50 of these. In my original question 'first' denotes the part which stores the first name of the person. Also within each 'struct' I have included it's number, i.e. its numerical position in the array.
    One of the 'functions' of the database is to remove/delete one of the records to make way for new data. This is what my original question was based on.
    Setting each part of the 'typedef struct' to 'nothing' (as above) seemed like what could be done, because another function of the database is to display all of the records using:
    Code:
    printf("\nFirst name\tLast name\tDate of birth\tID");
    for (i = 0; i < number; i++)
        {
          printf("\n%i\t%s\t%s\t%i/%i/%i\t%i", (i+1), data[i].first, data[i].last, date[i].day, date[i].month, date[i].year, data[i].ID);                
         }
    In this way would the deleted record be skipped over?
    Thanks for your help.

  5. #5
    Registered User
    Join Date
    Jan 2017
    Posts
    7
    Oh and 'date' (i.e. date[i].day) is another 'typedef struct' within the 'data' typedef which contains the individual parts of the date of birth - this is shown below:
    Code:
    
    
    Code:
    typedef struct { int day; int month; int year; }date;
    typedef struct { int recnum; char first[25]; char last[25]; date DOB; int ID; } data;


  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    A "database" is usually saved to a file. Are you saving this to a file or not?
    Also, what is "number"?

  7. #7
    Registered User
    Join Date
    Jan 2017
    Posts
    7
    Saving to a file is another necessity of this project yes.
    number is the number of elements of the array which have data in them

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    The only way to really "delete" an element from an array is to shift the array down over the removed element. Something like:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int size = 10;  // number of elements
    
        for (int i = 0; i < size; i++) printf("%d ", a[i]);
        putchar('\n');
    
        // remove element at offset 2:
        int elem_to_remove = 2;
        if (elem_to_remove < size - 1)
            memmove(&a[elem_to_remove], &a[elem_to_remove+1],
                    sizeof a[0] * (size - elem_to_remove));
        size--;
    
        for (int i = 0; i < size; i++) printf("%d ", a[i]);
        putchar('\n');
    
        return 0;
    }
    Alternatively, you can "mark" an element as deleted and write logic to essentially ignore that element when printing, searching, and (possibly) writing to a file.

    EDIT: Now that I think about it, moving the elements would make your "recNum" values wrong, although I really don't see what their purpose is in the first place. What's the point of storing the array position in the elements?
    Last edited by algorism; 02-05-2017 at 02:00 PM.

  9. #9
    Registered User
    Join Date
    Jan 2017
    Posts
    7
    That's really great, thank you! Could you explain line 12 a bit though? I haven't been shown any of those functions in lecture.

  10. #10
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Code:
    memmove(to_address, from_address, number_of_bytes);
    memmove simply moves data from one place to another, in a way that's safe to do even if the data is part of the same array (memcpy is not safe in that case).
    The first parameter is the destination (to).
    The second is the source (from).
    The third is the number of bytes to move, which is calculated by multiplying the number of elements to move by the size of each element (the size of the first element is used with the sizeof operator to determine the element size).

    BTW, read my edit above (about recNum).

  11. #11
    Registered User
    Join Date
    Jan 2017
    Posts
    7
    Great, thank you!I'll give this a go soon!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass/Save Variable contents from class to class !
    By k@$f in forum C Programming
    Replies: 4
    Last Post: 12-30-2013, 11:23 AM
  2. Scanning file contents to variable
    By Porl in forum C Programming
    Replies: 5
    Last Post: 02-11-2011, 09:15 AM
  3. Removing @far
    By Scyanide in forum C Programming
    Replies: 17
    Last Post: 12-04-2008, 03:21 PM
  4. Get Variable Contents
    By stickman in forum C++ Programming
    Replies: 24
    Last Post: 04-19-2006, 04:06 PM
  5. removing contents of a folder
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2001, 05:02 AM

Tags for this Thread