Question

How to connect one Struct to other Structs in C. Suppose I have 7 structs: struct...

How to connect one Struct to other Structs in C.

Suppose I have 7 structs:

struct file files[7];

Each of the structs has a unique name such as file1, file2, file3, file4, file5, file6, file7

What I need is in each of the structs store from 3 to 6 names of other structs, and if a struct is connected to one struct, that struct must also be connected to the same struct.

EXAMPLE:

A struct with name file1 stores name of file2, file3, file4, file5

So, all of those structs must also store the name file1 plus 2 to 5 names of other structs.

All coding must be done in C

0 0
Add a comment Improve this question Transcribed image text
Answer #1
struct File {
        struct File* files[7];
};

struct File* createFileStruct() {
        struct File* r = (struct File*) malloc(sizeof(struct File));
        for(int i=0; i<7; i++) {
                r->files[i] = NULL;
        }
        return r;
}

void connectStructure(struct File* src, struct File* dest) {

        // check if dest is already present in src, 
        // then no need to do anything.
        for(int i=0; i<7; i++) {
                if(src->files[i] == dest) {
                        return;
                }
        }

        // Now entry is not there, hence make an entry
        for(int i=0; i<7; i++) {
                if(src->files[i] == NULL) {
                        src->files[i] = dest;
                        break;
                }
        }

        // SIMILARLY, create an entry in destination also.
        for(int i=0; i<7; i++) {
                if(dest->files[i] == NULL) {
                        dest->files[i] = src;
                        break;
                }
        }
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
How to connect one Struct to other Structs in C. Suppose I have 7 structs: struct...
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
  • LANGUAGE = JAVA (IDE Eclipse preferred) Lab 21 AEIOU Counter Objective: Write a program that reads...

    LANGUAGE = JAVA (IDE Eclipse preferred) Lab 21 AEIOU Counter Objective: Write a program that reads a file and counts the number of times the vowels ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ occurs exactly in that order. It ignores consonants It ignores any type of space It ignores case The only thing it cannot ignore is if another vowel occurs out of order These count AEIOU aeiou hahehihohu Take it out These do not AEIuO Taco is good Take it over...

  • Write Linux command for each question. Please use one single commend to do following process. Q1:...

    Write Linux command for each question. Please use one single commend to do following process. Q1: Display all lines of list1.txt and list2.txt containing the letter ‘p', and sort the result. Q2: Append “file1.txt” and “file2.txt”, and redirect to “file3.txt”. Q3: Sort “file3.txt”, and redirect the result to file “file4.txt” Q4: Use shorthand way to change the permission of “file3.txt” to “user - write read execute, group - read execute, other - execute”. Q5: Extract first column from “file3.txt”, and...

  • Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing...

    Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing an array of structs to a function, and returning a struct as the value of a function. A function in the program should read several rows of data from a text file. (The data file should accompany this assignment.) Each row of the file contains a month name, a high temperature, and a low temperature. The main program should call another function which...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your...

    This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your knowledge of basic C++ programing to develop your solution. Develop a functional flowchart and then write a C++ program to solve the following problem. 1. Create a text file named file1.txt and write your brand of car (like Honda, Toyota, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your...

  • For this problem, you have to use following hash function: key modulo the number of buckets....

    For this problem, you have to use following hash function: key modulo the number of buckets. Input format: This program takes a file name as argument from the command line. The file is either blank or contains successive lines of input. Each line contains a character, either ‘i’ or ‘s’, followed by a tab and then an integer, the same format as in the Second Part. For each of the lines that starts with ‘i’, your program should insert that...

  • Project 8 – Populating a struct and saving it. This is another “switch” assignment. Rather like...

    Project 8 – Populating a struct and saving it. This is another “switch” assignment. Rather like the scenarios, here you must select someone else's posted .h file, post a claim, and using it, codepopulating their struct with values you choose. Here “populate” means to give each member a value, using assignment operators and strcpy ( ) as needed. This all assumes you completed Project 7 first! You can't skip ahead on this one. I populated the Cat fluffy in my...

  • Program in C Student Data using Structs In this assignment you will create a program (using...

    Program in C Student Data using Structs In this assignment you will create a program (using multiple .c files) to solve the following problem: You have been hired by the university to create a program that will read in input from a .txt file. This input will contain a student name, student id #, their age and their current gpa. You should use the following struct for your student data: struct Student{                 char name[50];                 int id;                 float...

  • Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...

    Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeFiles as follows: java MergeFiles filel file2 mergedFile II. MergeFiles reads names stored in files file and file2 III. Merges the two list of names into a single list IV. Sorts that single list V. Ignores repetitions VI. Writes the sorted, free-of-repetitions list to a new file named mergedFile.txt VII. The names in all three files are stored as one...

  • In C. I want to read in the data of a text file into an array...

    In C. I want to read in the data of a text file into an array of dynamically allocated structs. The file is read as a command-line argument The text file format is: <number of classes in this file> <department name of class 1>:<number of class 1>:<location of class 1> <department name of class 2>:<number of class 2>:<location of class 2> ... Assume that no piece of information in the file contains a colon: the colons are only used as...

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