Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs

Dashboard
Notifications
Mark all as read
Q&A

Regarding the implementation of data structures.

+1
−3

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 aBook.title, aBook.author & aBook.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){
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

3 comment threads

Please don't delete your question (1 comment)
Just type text (1 comment)
Regarding string handling, [this post](https://software.codidact.com/posts/284849) might be helpful (1 comment)

1 answer

+2
−2

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.

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

I have a question if I don't initialize them like this: aBook.title = ""; aBook.... (1 comment)

Sign up to answer this question »