Question

writing this program in C, how else do i declare the struct Graph* graph = new...

writing this program in C, how else do i declare the struct Graph* graph = new Graph

and

graph->edge = new edge[E]

without using new because this is C

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

First, you need to know that C is procedure oriented,not object oriented like C++, so C doesn't require any object creation since it doesn't have any class. If you wish to create any structure object, kindly follow the following example-

#include<stdio.h>
struct data
{
int age;
char name[10];
};
//This is how a structure is defined
int main()
{
struct data s1,s2;
//This is how objects of structure is created(two objects created).
s1.age=14;
s2.age=13;
scanf("%s%s",s1.name,s2.name);
//Here I have taken input from user in this way, for the names of two objects for which the structure was created
printf("%s %s %d %d",s1.name,s2.name,s1.age,s2.age);
return 0;
}

The above program might give you a idea of how to define or create structure objects. Now back to your query,

just define the graph structure and then in main function, use struct graph g; which will create an object for your graph structure.For the edge if you are creating any nested structure, then you will have to call it like-

struct graph g;

struct g.edge e;

//Below is an example of nested structure and how to create objects of nested structure

struct student

{

    struct person

    {

        char name[20];

        int age;

        char dob[10];

    } p ;

    int rollno;

    float marks;

} stu;

stu.p.age will help you access the age of person structure inside student structure.

I hope that I could help you understand the concept of using structures in C. If you need more help, there are other online video tutorials available in youtube.

Add a comment
Know the answer?
Add Answer to:
writing this program in C, how else do i declare the struct Graph* graph = new...
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
  • Computer Science Midterm Exam Review Questions 5. What is a struct, and how do you declare...

    Computer Science Midterm Exam Review Questions 5. What is a struct, and how do you declare one? 6. Define a struct and use it to solve a simple problem. 7. What is the main difference between structs in C and C++

  • in c++, create a struct named piggybank. this struct is meant to be hold the number...

    in c++, create a struct named piggybank. this struct is meant to be hold the number of each US currency coins in the piggybank.Next, write a function that will take this struct and an integer value as argument and increment the coin in the struct depending on the integer value. If the value is not a valid US coin the function will return false. Do this without using any if/else statement.

  • I was practicing program problems without answer. Not sure on how to write this program and...

    I was practicing program problems without answer. Not sure on how to write this program and I need help. it needs to be in C language. Thank you! Assume the node structure is as follows: struct node { int data; struct node "next; struct node "prev; please write a function struct node *merge(struct node *g. struct node 'n) to merge two doubly linked lists contains integer numbers without duplicates in the increasing order. You have to remove duplicates. Also show...

  • Create a c program of your choosing that follows the specifications below: Define a struct with...

    Create a c program of your choosing that follows the specifications below: Define a struct with 4 or more members. The struct must be different from the ones used in class (20 points) Declare an array of your struct using a size of 10 or more (20 points) Load the data for each element in your array from a text file (20 points) Display the data in your array in the terminal (20 points) Provide brief comments for every line...

  • I am writing a C program that takes either command line arguments or has a file...

    I am writing a C program that takes either command line arguments or has a file passed to it using < on the command line. My question is how do I check to see if there is a file passed using <. I think I need a way to check stdin to see if there is any data present. Thanks

  • Which of the following are ways to declare a new data type in C++? more than...

    Which of the following are ways to declare a new data type in C++? more than 1 choice is accepted A. There is no need to declare the data type in C++ B. Using the new keyword C. Create an alias using typedef D. Create a new type by using class or struct. E. Create an extension of an existing type using inheritance. F. Using the auto keyword QUESTION 6 When an object is copied by assigning each of the...

  • I am writing a program in C++, which requires me to read an input text file...

    I am writing a program in C++, which requires me to read an input text file using command line argument. However, I am using xcode on my Macbook to write C++ program, and use terminal instead of command. How do you use int main(int argc, char** argv[]) to read an input file. My professor requires us not to hard code the text file name like .open("example.txt"); Thank you!

  • C++ 29.5 Lab 29: Pointers Exercise: Before the main function, declare a struct called Movie which...

    C++ 29.5 Lab 29: Pointers Exercise: Before the main function, declare a struct called Movie which has title as a string and year as the integer as the member variables. Also before the main function, instantiate an object of Movie called m with initial value "Avengers" as title and year as 2019. Note m is the global variable and A in the title is uppercase. Also before the main function, declare another global variable which is a pointer to the...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • I need help writing this program, below are the directions input.dat information Be sure that you...

    I need help writing this program, below are the directions input.dat information Be sure that you downloaded the input file input.dat In the global space, define a struct called Album The members of the struct shall include: - artist - title year - numTracks Your program shall open the input.dat file and read the contents into memory - The very first value in the file always states how many albums are in the file - This is how you'll know...

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