Thread: Pointer and Struct ???

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    8

    Question Pointer and Struct ???

    This is the include file test.h
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/mman.h>
    #include <sys/stat.h>
    #include <errno.h>
    
    struct _address
    {
    	char *Street;
    	int HouseNumber;
    } ;
    
    struct _name
    {
    	int age;
    	char *lastname, *firstname;
    	struct _address *addr;
    } ;
    
    struct _student
    {
    	int num;
    	struct _name *NameOfStudent;
    } ;
    This is the write.c code to write data into binary file:
    Code:
    #include "test.h"
    int main()
    {
    	FILE *f;
            struct _student *hs1, *hs;
    	hs1 = (struct _student *)malloc(sizeof(struct _student));
    	hs1->NameOfStudent = (struct _name*)malloc(sizeof(struct _name));
    	hs1->NameOfStudent->addr = (struct _address*)malloc(sizeof(struct _address));
    	hs1->NameOfStudent->lastname = "John";
    	hs1->NameOfStudent->firstname = "A";
    	hs1->NameOfStudent->age = 15;
    	hs1->NameOfStudent->addr->Street = " London  ";
    	hs1->NameOfStudent->addr->HouseNumber = 123;
    	hs1->num = 1;
    
    	printf(" -------------  Input ----------------\n");
    	printf(" %d %s %s %d %s  \n", 
    			hs1->num, hs1->NameOfStudent->lastname, 
    			hs1->NameOfStudent->firstname, 
    			hs1->NameOfStudent->addr->HouseNumber, 
    			hs1->NameOfStudent->addr->Street);
    	f = fopen ("/root/test","wb");
    		fwrite((struct _student *)hs1, sizeof(struct _student), 1, f);
    		fwrite((struct _name *)hs1->NameOfStudent, sizeof(struct _name), 1, f);
    		fwrite((struct _address *)hs1->NameOfStudent->addr, sizeof(struct _address), 1, f);
    	fclose(f);
    
    
    	free(hs1->NameOfStudent->addr);
    	free(hs1->NameOfStudent);
    	free(hs1);
    
    	return 0;
    }
    This is the read.c code to read from binary file
    Code:
    #include "test.h"
    
    struct _student STUDENT;
    struct _address ADDRESS;
    struct _name NAME;
    
    int main()
    {
    	FILE *f;
    	struct _student *hsRead;	
    	struct _name *name;
    	struct _address *addr;
    	hsRead = &STUDENT;
    	name = &NAME;
    	addr =  &ADDRESS;
    	printf(" sizeof(struct _student) = %d \n", sizeof(struct _student));
    	printf(" sizeof(struct _name) = %d \n", sizeof(struct _name));
    	printf(" sizeof(struct _address) = %d \n", sizeof(struct _address));
    	printf(" -------------  Readfile ----------------\n");
    	//hs = (struct _student *)malloc(sizeof(struct _student));
    	f = fopen ("/root/test","rb");
    	while(!feof(f))
    	{
    		int status = fread((struct _student *)hsRead, sizeof(struct _student)+sizeof(struct _name)+sizeof(struct _address), 1, f);
    		if (status > 0)
    		{
    			name = hsRead->NameOfStudent;
    			addr = hsRead->NameOfStudent->addr;
    			printf(" STT : %d \n", hsRead->num);
    			printf(" NAME : %s %s \n",  name->lastname, name->firstname);
    			printf(" AGE(s) : %d \n", name->age);
    			printf(" ADDR : %d %s  \n",  addr->HouseNumber, addr->Street);
    		}
    	}
    	fclose(f);
    	return 0;
    }
    The result of read.c is
    Code:
     sizeof(struct _student) = 8
     sizeof(struct _name) = 16
     sizeof(struct _address) = 8
     -------------  Readfile ----------------
     STT : 1
     NAME : (null) (null)
     AGE(s) : 0
    Segmentation fault (core dumped)
    Why the result of NAME show here is (null) (null) and AGE(s) is 0 and the Segmentation fault ??? When I write the code WRITE and READ in ONE C file, it's ok. How can i fix it ??? Please help me. I'm a newbie in C on Linux.
    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try actually allocating space for 'firstname', 'addr', and 'Street'. Also, pick a naming convention and stick with it through out the whole program. See 'Street' and 'addr' as an example of what I'm talking about.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    oh yeah, you declared pointers but didnt allocate space for them using malloc...

    and is his fwrite implementation correct??

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I read posts similar to how a compiler reads source code. I stop at the first error I see usually. I, unlike a compiler, work on a 'at a glance' basis. Otherwise known as 'random access'.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > struct _address
    None of your symbols should begin with an underscore. All such symbols are reserved by the implementation, so to avoid any conflicts, you should not use leading underscores.

    > hs1->NameOfStudent = (struct _name*)malloc(sizeof(struct _name));
    Casting malloc in C isn't useful (see the FAQ).
    As best, it does nothing.
    At worst, it hides errors if you fail to include the correct header file, or if you mistakenly compile you C program with a C++ compiler.

    > fwrite((struct _student *)hs1, sizeof(struct _student), 1, f);
    Ditto here - fread/fwrite take void* as the first argument, so casts are not necessary.

    > f = fopen ("/root/test","rb");
    Don't tell me you develop code as root as well as surf the net as root?

    > while(!feof(f))
    Why you shouldn't do this is also in the FAQ.
    At least you do check the status of fread, which is a step up on most peoples efforts.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM