Thread: pointer to nested structure

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    5

    pointer to nested structure

    Hi,
    I am new in nested structures. So I would like to get some help. In the following example I can write the "sweet" to the file. But I am unable to write the value "8" in the file through the structure variable. How should I write to put the value 8 in integer "tt" of structure "str3" and correctly able to write it in the file.

    Also another question. Let say I get a data base full with the data of the following type, now I read len_ppp of the first data from the file and this value means the remaining part of the first data has this length (otherwise I cannot calculate the length of the string of the first data and the value of the integer tt pointed by *cl and tt respectively). Now getting the length of the remaining part of the data by len_ppp, how could I fetch the data from the file and print it in terminal (each data at a time)?

    Code:
    #include <stdio.h>
    
    int main(){
    
    
    typedef struct three{
    int tt;
    }str3;
    
    typedef struct two{
    str3 rt;
    char *cl;
    }str2;
    
    typedef struct one{
    int len_ppp;
    str2 *ppp;
    
    }str1;
    
    
    str1 *pop;
    pop=(str1*)malloc(sizeof(str1));
    
    pop->len_ppp=5;
    pop->ppp=(str2*)malloc(sizeof(str2));
    
    pop->ppp->cl=malloc(5 * sizeof (char));
    
    pop->ppp->cl="sweet";
    pop->ppp->rt.tt=8;
    
    FILE *lt;
    lt=fopen("test.bin","wb");
    	fwrite((pop->ppp->cl), 5, 1, lt);
    	fwrite(&(pop->ppp->rt.tt), sizeof(int), 1, lt);
    
    return 0;
    
    }

    Please help me to understand it and get knowledge. Thank you.

    Best regards,
    Wahid

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    A couple notes to start with:
    1. Don't cast the result of malloc, it's unnecessary and it makes it harder to read code.
    2. malloc is defined in stdlib.h. Include that file at the top of your program so that it doesn't generate warnings.
    3. Move your structs/typedefs outside the main function.
    4. Fix your assignment of pop->ppp->c1. You tell the program to allocate 5 bytes (on the heap), and have c1 point to it. You then ask the program to make c1 point to the static string "sweet", located in the data section. C does not support string assignment in that way. You are not copying "sweet" into the memory you allocated. You are simply changing the address c1 points to, and losing your handle to the malloc'ed memory, which, if this ran in a loop, would cause a memory leak. Either just have it point to the string constant, or use strdup. Regardless of your choice, remember to always free all allocated memory when you're done with it.

    In the following example I can write the "sweet" to the file. But I am unable to write the value "8" in the file through the structure variable. How should I write to put the value 8 in integer "tt" of structure "str3" and correctly able to write it in the file.
    Your sample program successfully wrote the value 8 to the file in my case. How did you determine it didn't write it in your case?

    Also another question. Let say I get a data base full with the data of the following type, now I read len_ppp of the first data from the file and this value means the remaining part of the first data has this length (otherwise I cannot calculate the length of the string of the first data and the value of the integer tt pointed by *cl and tt respectively). Now getting the length of the remaining part of the data by len_ppp, how could I fetch the data from the file and print it in terminal (each data at a time)?
    Your code as it stands always writes 5 bytes for the string. Thus you can always allocate 6 bytes into pop->ppp->c1 before you read the string, then read 5 elements that are sizeof(char) into the string. Null terminate your string (pop->ppp->c1[5] = '\0') to be safe. Lastly, read an integer into pop->ppp->rt.tt.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    5
    Hi,
    Thank you for all good points. I learned a lot from your post.
    Your sample program successfully wrote the value 8 to the file in my case. How did you determine it didn't write it in your case?
    Yes, it wrote, but I opened the file in binary mode, so I could not see it inside the file as "8", so I thought it was writing garbage, but it actually wrote 8 in binary format.

    Thank you.

    /Wahid

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Structure Pointer
    By seubjoh in forum C Programming
    Replies: 3
    Last Post: 08-31-2009, 01:38 PM
  2. Structure Pointer to Structure
    By u_peerless in forum C Programming
    Replies: 1
    Last Post: 01-28-2009, 12:40 AM
  3. how to cast a char *mystring to a structure pointer ??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 08:59 AM
  4. Pointer to a structure
    By frenchfry164 in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 06:35 PM
  5. Pointer to next Structure
    By Garfield in forum C Programming
    Replies: 6
    Last Post: 09-16-2001, 03:18 PM