• C

Nested Structures, files and pointers

I have a 3 structures and 2 data files in the design of a small CD Library in my suburb. They are nested. 1) CDList which contains the number of CDs and a CD structure. 2) CD structure contains details of CD and a Sale structure. 3)Sale has details of the CD rentals. And 2 Files 1) CD.txt 2) Sales.txt. The data is already loaded in the text files. I think the design is okay. The question is how can we implement nested structures with pointers?
LVL 1
manojantonyAsked:
Who is Participating?
 
Amritpal SinghConnect With a Mentor Commented:
plz visit the following link
http://www.funducode.com/freec/pointers/pointer5.htm

for additional help u can visit the following links too
http://www.funducode.com/freec/pointers.htm
http://www.funducode.com/freec.asp

BOL
0
 
jhshuklaCommented:
1) CDList which contains the number of CDs and a CD structure
or is it
1) CDList which contains the number of CDs and an array of CD structures?

think about what is needed for a sales structure. probably a list of rentals and number of rentals. you should have a struct that has a pointer to a rental struct and an int which is the number of rentals.

the smart thing that C++'s STL vector does it to allocated more memory than immediately needed and has another memer called capacity. you could immitate its behaviour. visit sgi.com for STL interfaces.
0
 
manojantonyAuthor Commented:
Hi,
I am working on these
1) Load the data from files to the structures.

2) I have identified the following functions
- Display 1) CD List and  2) Sales
- Search for a 1) particular CD  2) Sales
- Make a rental
- Add a New CD title

All functions have argument a pointer to the CDList structure.
I will be updating the structure design and file format
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
manojantonyAuthor Commented:
The structure is like this

1) CDList  - Top level which contains no of Cds and CD structure array
typedef struct cdList
{
   CD cds[500]; // structure
   int count; // no of CDs
} CDList;

2) CD - which contains details od CD Sales structure array - details loaded from cd.txt
typedef struct cd
{
   int id;                     // CD id
   char title[40];          // CD title
   char director[40];    // directed by
   float cost;              // rental
   int count;              // no. of. cds available
   int rentCount;        // no of rentals
   Sale sales[100];    // sales details of the CD
} CD;

3) Rentals / Sales - details loaded from sales.txt
typedef struct sale
{
   struct tm date;     // time and date of sale
   int quantity;         // no of CDs sold
   float price;           // cost of book x quantity
} Sale;
0
 
manojantonyAuthor Commented:
None is interested in this ?
0
 
jhshuklaCommented:
typedef struct cdList
{
   CD cds*; // structure
   int count; // no of CDs
} CDList;

typedef struct cd
{
   int id;                     // CD id
   char title[40];          // CD title
   char director[40];    // directed by
   float cost;              // rental
   int count;              // no. of. cds available
   int rentCount;        // no of rentals
   Sale sales*;    // sales details of the CD
} CD;

now you will need to specify the number of cds in the cdlist and create and array of CD's using malloc. whenever you need to add/remove CD's, you need to re-allocate memory and copy from the old array to a new one. don't forget to free() the old array. but this is VERY inefficient. that's why I recommended immitating vector's behavior.

same thing with sales inside CD.

jaydutt
0
 
manojantonyAuthor Commented:
//function for reading a line from file and splitting
//with : as parser;
//Error: char* to char[] not possible !

void splitLine(CDType* list, char *line)
{
    char *word, *pEnd;
    long l; int count = 0;
    const char *sep = "\n\0:";
    for(word = strtok(line,sep); word; word=strtok(NULL,sep), count++)
    {
      if (count==0)
      {
        l = strtol(word,&pEnd,0);
        list->cds[list->count].id = l;
      break;
      }
      elseif( count==1)
      {
         /*title is char array */
         list->cds[count].title = word;
        /* am getting the error here*/
         break;
      }
      /* code for count = 2,3, ..*/
    }    
}

// how to solve this ?
0
 
jhshuklaCommented:
cd.title is an array. In C/C++ an array is implemented as a fixed pointer to a chunk of memory. since it is fixed, it cannot be assigned a value. you need to use strcpy instead.
syntax: strcpy(char *destination, const char *source);
additionally you need to free the memory allocated to word. strtok works like this.
char * strtok(string, sep){
  search in string for a char from the sep, store to index
  malloc index+1 bytes, assign return value to pchr
  copy index bytes from string to pchr and terminate with null
  shift chars in string to the left so that the first char after string[index] is now at string[0]
  return pchr;
}
see that malloc in strtok? that's why you need to free(word). if you don't, your program won't crash but there are memory leaks and it affects your system's performance adversely.
0
 
manojantonyAuthor Commented:
/*
str is a string, sale is int
d is a structure with d , m, y for day month year
books, sales are structures
ndate is struct tm*
*/
//........
if (sscanf(str,"%d/%d/%d", &d.d, &d.m,&d.y) == 3)
{
sale = list->cds[id].count;
list->books[bID].sales[salesCount].ndate->tm_mday = d.day;
}
//.......

no compilation error .. but it is having run time error.
what can it be ?

and am about to finish my program and this will be my question.

50 points is already for jhshukla
and rest is urs. !

0
 
jhshuklaCommented:
where is your runtime error?
0
 
manojantonyAuthor Commented:
I am doing with bcc in windows. some memory error and the program gets terminated ie, the dos cmd window will close on reaching this piece of code.
0
 
jhshuklaCommented:
does it breakdown at sscanf or in the assignment?
0
 
manojantonyAuthor Commented:
list->books[bID].sales[salesCount].ndate->tm_mday = d.day;
0
 
jhshuklaCommented:
could you post whole code so that I can run it on my machine and try to figure it out?
0
 
manojantonyAuthor Commented:
the code is to convert string to time. srtptime is not working on windows
0
 
jhshuklaConnect With a Mentor Commented:
sorry, was slightly bz.
did you check the value of ndate? is it pointing to right location?

>> list->books[bID].sales[salesCount].ndate->tm_mday = d.day;
why are you using "d.day" here and "d.d" in sscanf?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.