Question

#define MAX_DEPARTMENT_LENGTH 6 #define MAX_COURSE_NUMBER_LENGTH 5 #define MAX_COURSE_LOCATION_LENGTH 12 struct class_info {        char department [MAX_DEPARTMENT_LENGTH];...

#define MAX_DEPARTMENT_LENGTH 6

#define MAX_COURSE_NUMBER_LENGTH 5

#define MAX_COURSE_LOCATION_LENGTH 12

struct class_info {

       char department [MAX_DEPARTMENT_LENGTH];

       char number [MAX_COURSE_NUMBER_LENGTH];

       char location [MAX_COURSE_LOCATION_LENGTH];

}

can you write a function to store this struct into a c-string?   Don't worry about allocating memory or anything like that, keep it as basic as possible, however try to treat this as if the were an array of pointers to this struct. Thank you. preferably use sprintf/fprintf.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#define MAX_DEPARTMENT_LENGTH 6

#define MAX_COURSE_NUMBER_LENGTH 5

#define MAX_COURSE_LOCATION_LENGTH 12

struct class_info {

   char department [MAX_DEPARTMENT_LENGTH];

   char number [MAX_COURSE_NUMBER_LENGTH];

   char location [MAX_COURSE_LOCATION_LENGTH];

};

// copy each field of struct into array provided as argument

char* store(struct class_info *arr)

{

    // create a string to store the answer

    char *ans = (char *)malloc( (MAX_DEPARTMENT_LENGTH + MAX_COURSE_NUMBER_LENGTH + MAX_COURSE_LOCATION_LENGTH + 100) * sizeof(char) );

   

    sprintf(ans, "Department : %s\nNumber : %s\nLocation : %s\n", arr->department , arr->number , arr->location );

   

    return ans;

}

int main()

{

}

Add a comment
Know the answer?
Add Answer to:
#define MAX_DEPARTMENT_LENGTH 6 #define MAX_COURSE_NUMBER_LENGTH 5 #define MAX_COURSE_LOCATION_LENGTH 12 struct class_info {        char department [MAX_DEPARTMENT_LENGTH];...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT