Solved

Nested Structures, files and pointers

Posted on 2004-09-24
16
520 Views
Last Modified: 2008-01-09
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?
0
Comment
Question by:manojantony
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
16 Comments
 
LVL 6

Accepted Solution

by:
Amritpal Singh earned 20 total points
ID: 12149478
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
 
LVL 9

Expert Comment

by:jhshukla
ID: 12149488
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
 
LVL 1

Author Comment

by:manojantony
ID: 12149492
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:manojantony
ID: 12149504
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
 
LVL 1

Author Comment

by:manojantony
ID: 12156694
None is interested in this ?
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12157733
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
 
LVL 1

Author Comment

by:manojantony
ID: 12251259
//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
 
LVL 9

Expert Comment

by:jhshukla
ID: 12255666
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
 
LVL 1

Author Comment

by:manojantony
ID: 12256862
/*
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
 
LVL 9

Expert Comment

by:jhshukla
ID: 12260041
where is your runtime error?
0
 
LVL 1

Author Comment

by:manojantony
ID: 12260111
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
 
LVL 9

Expert Comment

by:jhshukla
ID: 12260544
does it breakdown at sscanf or in the assignment?
0
 
LVL 1

Author Comment

by:manojantony
ID: 12260577
list->books[bID].sales[salesCount].ndate->tm_mday = d.day;
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12264123
could you post whole code so that I can run it on my machine and try to figure it out?
0
 
LVL 1

Author Comment

by:manojantony
ID: 12264813
the code is to convert string to time. srtptime is not working on windows
0
 
LVL 9

Assisted Solution

by:jhshukla
jhshukla earned 80 total points
ID: 12272010
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question