Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Regarding the implementation of data structures.
I'm attempting a question to do with data structures, file streams & linked lists. The code isn't complete yet as I am still halfway working on it. I am required to use data structures in the declaration section like so as shown in my code below. However, I'm still confused on what I should include in the double quotes for story.title
, story.author
& story.subject
(I'm not sure if I'm doing it right too). Can anyone help me out? (Also my title, author and book should not exceed 49 characters)
```
#include<stdio.h>
#include<stdlib.h>
//#define MAX 49
int main (void){
FILE *cfPtr;
struct book{ //the data structure that I am required to use
char *title;
char *author;
char *subject; //subject[]
};
int operation;
if ((cfPtr = fopen("library.dat","w")) == NULL){
printf("File could not be opened...\n");
}
else {
struct book aBook;
struct book *aPtr;
aBook.title = ""; //how should I write in the double quotes?
aBook.author = "";
aBook.subject = "";
printf("Enter operation number:");
scanf("%d", &operation)
switch(operation){
case 1:
printf("Title:\n");
scanf("%s", title);
printf("Author:");
scanf("%s", author);
printf("Subject:");
scanf("%s", subject);
break;
case 2:
printf("Enter Title:");
scanf("%s", Title);
//Incomplete coding (still working on it)
//I'm planning to use switch case by looking at the question but I also have to use linked lists
fprintf(cfPtr, "%s %s %s\n", title, author, subject);
}
fclose(cfPtr);
return 0;
}
1 answer
Since this is pretty clearly a homework exercise, and the point of those is for you to learn, I'm going to be evil and not spoon-feed the answer to you.
However, I will give you some pointers.
First, consider what the struct book
actually contains. What is the actual variable type of its members? (Which in your example are all the same, but of course could be different if you were to also, say, store the year of publication or the number of pages in or the physical weight of each respective book.) What does that variable type mean for the actual data at the specific memory location occupied by an actual instance of that struct, such as your aBook
variable? Basically, if you were to look at the actual memory occupied by aBook
immediately before the assignments to members of it (which begin at the line that has your comment "how should I write in the double quotes?"), what would you see and how would that relate to what the computer is doing under the hood when running your program? (For simplicity, assume that your program is the only thing that is running on the computer; don't consider things like multitasking, swapping memory to disk, and so on.)
Second, considering what you see at the memory occupied by aBook
, what change would need to be made to that in-memory data (and possibly other memory as well) such that you can use the members of aBook
to hold a reasonable amount of arbitrary data, such as a book title?
Third, in C, how do you make such a change? What does that do to the memory space occupied by the aBook
variable instance specifically?
Fourth, having made that change, how do you store a value such that it can later be read back, using only what's accessible through the aBook
variable that you do have? Again, what does that do to the memory space occupied by the aBook
variable instance specifically?
Here's a bit of a hint and teaser alike: the answers to the above will also help you find the next book in an arbitrarily-sized library.
4 comment threads