Thread: Dynamic Structure Array Size

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    216

    Dynamic Structure Array Size

    Hi, I'm in need of help getting my code to create a dynamic structure array based on the number of files. Here is the code.
    Code:
    #include <stdio.h>
    struct Version
    {
           int major;
           int minor;
           int build;
           
           int numFiles;
    };
    
    struct Download
    {
           char name[56];
           char outName[16];
    };
    
    int main()
    {
        struct Version ver;
        int j;
        
        ver.major = 0;
        ver.minor = 4;
        ver.build = 2;
        
        ver.numFiles = 1;
        
        struct Download file[ver.numFiles];
        
        file[0].name[] = "http://website.com/projects/dod/Files.rar";
        file[0].outName[] = "Files.rar";
        
        FILE *fp;
        
        fp = fopen("version.dat", "wb");
        
        fwrite(&ver, 1, sizeof(ver), fp);
        
        fclose(fp);
        
        ver.minor = 5;
        
        fp = fopen("update.dat", "wb");
        
        fwrite(&ver, 1, sizeof(ver), fp);
        fwrite(&file, 1, sizeof(file), fp);
        
        fclose(fp);
        
        sleep(100);
        
        fp = fopen("version.dat", "rb");
        
        fread(&ver, sizeof(ver), 1, fp);
        
        fclose(fp);
        
        printf("Version.dat\n\nVersion %i.%i.%i Number of files %i\n\n", ver.major, ver.minor, ver.build, ver.numFiles);
        
        fp = fopen("update.dat", "rb");
        
        fread(&ver, sizeof(ver), 1, fp);
        fread(&file[ver.numFiles], sizeof(file[ver.numFiles]), 1, fp);
        
        fclose(fp);
        
        printf("Update.dat\n\nVersion %i.%i.%i Number of files %i\n\n", ver.major, ver.minor, ver.build, ver.numFiles);
        for(j = 0; j < ver.numFiles; j++)
        printf("Name %s\nOutName %s\n\n", file[j].name, file[j].outName);
        
        getchar();
        return 0;
    }
    What this program (is suppose to) does is:

    1. Creates a version.dat to contain the version information (numFiles is useless for this file)
    2. Creates an updated.dat file to contain the new file version, the number of files needed to download, and finally the path to the file(s), and the outfilename(s).
    3. Read in this data, and display it to the screen

    Thanks for the help

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You cannot use the assignment operator for a string except at declaration time.
    Code:
    file[0].name[] = "http://website.com/projects/dod/Files.rar";file[0].outName[] = "Files.rar";
    use strcpy()

    If you want dynamic allocation at runtime, you should look at malloc() and free() and start thinking about pointers to structs.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Ok, well i got it working w/ strcpy. But strcpy does what i need, so i won't need malloc. thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of dynamic array
    By dayalsoap in forum C Programming
    Replies: 3
    Last Post: 10-05-2010, 08:54 PM
  2. How to get the size of an dynamic array?
    By sept in forum C++ Programming
    Replies: 7
    Last Post: 09-18-2007, 02:26 PM
  3. Dynamic Array Size?
    By Junior89 in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2007, 11:31 PM
  4. size of dynamic array
    By tommy_gunn in forum C Programming
    Replies: 3
    Last Post: 12-30-2004, 08:01 PM
  5. Dynamic Array Size
    By minignaz in forum C Programming
    Replies: 11
    Last Post: 12-12-2003, 06:05 PM

Tags for this Thread