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
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

+2
−6

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;
}
History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

4 comment threads

There is no code below, or anywhere else. (2 comments)
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

+4
−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.

History
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 »