#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.
#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()
{
}
#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];...