array of pointers to structs

This is a discussion on array of pointers to structs within the C Programming forums, part of the General Programming Boards category; I have created a struct, saved 100 entries of that struct into a file and will need to sort them ...

  1. #1
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323

    array of pointers to structs

    I have created a struct, saved 100 entries of that struct into a file and will need to sort them by what ever means. I understand you can create an array of pointers to that struct like this:

    Code:
    struct test {
    	int testDataNum;
    	char testDataName[50];
    };
    
    struct test *testPtr[100];
    Now, say I have already loaded 100 entries into a file using fseek(), fwrite() according to sizeof(struct test). My question is; is there a way to read these entries using something like fread() and put their address(to the beginning of the struct) into the array of pointers? fread() returns the actual data according to the size specified, not the location. I want to be able to sort the array of pointers, dereferencing them, so I dont have to creat this huge multidemensional array then sort that. I only need to print out the sorted list, the information can stay unsorted in the file. How would I get the address of each struct in the file?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    You'll want to malloc() the memory for each of those pointers:
    Code:
    testPtr[i] = (struct test*)malloc(sizeof(struct test));
    Then just fread() them one at a time:
    Code:
    fread(testPtr[i],sizeof(struct test),1,f);
    If you plan on using fseek() and you want your code to work on a DOS/Windows machine, be sure to open the file in binary mode.

    Don't forget to free() the memory

    gg

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    You'll want to malloc() the memory for each of those pointers:
    why would I need to allocate the memory for the pointers when it already exists. the array has been declared it should have 100 (garbage) elements that will accept an address for type struct test. I want to get the address of each entry in the file and put them in the array. fread() will return the full entry, not the address in the file. Is there a way to get that address?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>why would I need to allocate the memory for the pointers when it already exists.
    The wording is confusing, that's all. You need to malloc() memory for each struct; the address returned by each call to malloc() will be stored in your array of pointers.

    An example might be:

    Code:
    /* Untested */
      struct test single;
      struct test *testPtr[100];
      int i;
      
      for (i = 0;(fread(&single, sizeof(single), 1, fp) == 1); i++)
      {
        testPtr[i] = malloc(sizeof(struct test));
        *(testPtr[i]) = single;
      }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    You give fread() the memory where is should place the read bytes, it doesn't return a pointer.
    Also, you're previous code snipet declares an array of pointers, not an array of structs...
    Study this code:
    Code:
        struct test *testPtrArray[100];
        struct test testArray[100];
    
        printf("size of arrays: testPtrArray:%d, testArray:%d\n",
               sizeof(testPtrArray),sizeof(testArray));
        printf("size of elements: testPtrArray:%d, testArray:%d\n",
               sizeof(testPtrArray[0]),sizeof(testArray[0]));
    So if you don't want to use malloc(), you can use the "testArray" declaration which will allocate space for 100 structs on the stack.

    Then you can use fread() like this:
    Code:
    fread(&(testArray[i]),sizeof(struct test),1,f);
    gg

  6. #6
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    I dont need 100 structs. I have the struct declaration there so I may write the correct byte size to hold the name and the info in a file. I have already written to the file with a different program. I need to read that information that was in the file. I dont need to allocate any memory because the information is in a file. I WANT to have an array of pointers. Those pointers will point to struct data type. I want them to point to the different byte locations in the file. Is there a way to get those byte locations in the file??? I will then put those address, which are struct address, in the array of pointers???

    Thanks in advance and sorry for any misunderstanding, or miswording on my part.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    Read this
    and this

    You can map the file into memory (using some "advanced" api's), or read the entire file into malloc'd memory yourself, or read in individual record(s) at a time.

    That's all your options right there.


    gg

  8. #8
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  3. Creating an array of pointers to int[]
    By OkashiiKen in forum C Programming
    Replies: 3
    Last Post: 09-29-2006, 06:48 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21