Question

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++

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

1) Struct is a keyword ,it is used to declare structure type data. Structure datatype group the items ( different datatypes) ...(Struct is a collection of same/different data types)

Declaration of Struct

Struct Node {

Int Data

Char value

}; // here It groups the items type int,char

Assigning the datatype

Struct Node x; // x is Struct Node datatype

Accessing variables in Struct

x.Data ,x.value

2)

Simple example on Struct keyword

include<stdio.h>

#include<string.h>

struct Student

{

    char name[25];

    int age;

    char gender;

};

int main()

{

    struct Student s1;

    s1.age = 18;

    strcpy(s1.name, "Nick");

    strcpy(s1.name, "M");

    printf("Name of Student : %s\n", s1.name);

    printf("Age of Student : %d\n", s1.age);

    printf("Gender of the student %s\n",s1.gender);

    return 0;

}

3)

In C structure you can use only variables, you can't use member functions but in C++ you can add member functions to it

In C there is no topic of access specifier on structures but in C++ you can assign access specifiers(public, private,protect) to structures

In C you can directly initialize the variables but in C++ You can't directly initialize the variables

Add a comment
Know the answer?
Add Answer to:
Computer Science Midterm Exam Review Questions 5. What is a struct, and how do you declare...
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
ADVERTISEMENT