answersLogoWhite

0


Best Answer

A C++ structure (struct) is simply a type of class where all members have public access by default (class members are private by default). Aside from that, C++ classes and structures have exactly the same meaning; the following definitions are exactly equivalent:

struct S {

int x; // public by default

private:

int y;

};

class C { public:

int x;

private:

int y;

};

Note that although the physical representations of S and C types are exactly equivalent, they are still fundamentally different types. That is, you cannot pass an object of type C into a function that expects an object of type S, unless you explicitly define a cast operator to perform an implicit conversion from C to S.

By convention you will typically use structures to define PODs ('plain old data' types where all members are implicitly public and the type is not itself derived from another non-POD type) and use classes when you need to establish an invariant. However, there's nothing in the language that fundamentally prevents you from using a structure to establish an invariant or to use a class as a POD. The only real disadvantage in doing so is that (conventional) C++ programmers may question why you would choose to do so; it can only lead to confusion.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

The main advantage of a struct is that it allows us to pass and return multiple values as a single data type. We can also use arrays to do this, however an array only allows us to pass and return multiple values of homogeneous types whereas a struct allows us to return heterogeneous type. Moreover, an array does not support the copy semantic so we cannot pass and return arrays by value, but we can with a struct.

Typically we use a struct to associate several pieces of information and treat the whole as a single entity, much as we would a database record except the fields are called data members.

Declaring and using struct data types is somewhat cumbersome because the struct keyword is also part of the type name:

struct point {

int x;

int y;

};

struct point p;

To simplify things, we typically declare an alias for the struct:

typedef struct point {

int x; int y;

} point_t;

point_t p;

The _t suffix usually denotes an alias for a type, however we often simplify further by placing the suffix on the type and leave the alias undecorated:

typedef struct point_t {

int x;

int y;

} point;

point p;

To initialise a struct, we can use member-wise initialisation:

point p {42, 0};

Or we can initialise members individually:

point p;

p.x = 42;

p.y = 0;

Note that we use the member-access operator (.) to access the individual members of a struct.

We can also use the copy semantic to initialise a struct:

point q {p}; // same as: struct q = p;

The copy semantic allows us to pass and return struct data types by value:

point scale (point p, double s) {

p.x *= s;

p.y *= s;

return p;

}

The size of a struct is the sum of its member types plus any padding bytes to allow for memory alignment. When the size of a struct is larger than a pointer, passing by reference is usually preferred:

point* scale (point* p, double s) {

p->x *= s;

p->y *= s;

return p;

}

Note that we use the pointer-to-member operator (->) when dereferencing the members of a pointer to struct.

Any data type may be used to define the members of a struct, including other struct types (known as embedded structures). For instance:

typedef struct circle_t {

point centre; // embedded structure

int radius;

} circle;

circle c {0, 0, 10}; // c.centre.x=0, c.centre.y=0, c.radius=10

A struct may also refer to its own type through a member pointer:

typedef struct node_t {

int data;

node* next; // pointer to the next node in a sequence of nodes (e.g., a list of nodes)

} node;

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

No Data hiding

Cannot add complex numbers

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

mala mahiti asat ter bhenchod tuzyakade kashala alo asto

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

when we want to integrate information in one entity then structure is used

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the advantages of structure in c language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is structure and union?

Structure is a query that is used in language c++


How do you create modules in c language?

by using structure in c.........


Advantages of c language in cryptography?

You can use OpenSSL.


Why is c called a structured language?

a synonym for structure language in "Comb-sturctured" languages. C in comb.


What data structure is used to represent graphs in c language?

A tree.


What is the program language c?

C is a pop language. C is a case sensetive language. C is motherof all language. C is block structure language. C is a high level language. C is advace of B language. C developed by D.richties in 1972 at AT & T Bell lab in USA. Sachin Bhardwaj 986854722 skbmca@gmail.com


What is basic structure of file handling in c language?

open, read/write, close


What is the difference between c and c plus plus extension?

c language is the structure oriented language and c does not follows the object oriented paradigms . c++ obeys the all object oriented language characteristics ========== C++ is a set of extensions to the C language to allow some (not all) principles of object-oriented programming to be used. Originally, C++ was a front end pre-processor for C and C++ compilers will translate C language functions.


What the difference between c and c plus plus?

The fundamental difference is that in C++ object-oriented programming (OOP) was added. C is a procedural language (that means. top-down structure design), where as C++, which is an extension of C itself, is an object oriented language.


How do you define the structure size in C language?

You cannot define the size of the structure, but you can query it with sizeof.The structure size in C language depends on the elements of the structure.Example:#include struct Test{int v;char str[100];};int main(){printf("The structure size is %d\n", sizeof(struct Test));return 0;}


Example of procedural programming language and object oriented programming language?

example of procedural programming are those programming language that have structure e.g basic,fortran,c++,c and pascal e.t.c


A compiler that translates a high level-language into another high-level language is called a source-to-source translator. What advantages are there to using C as a target language for a compiler?

C would be a good language if you wanted to then go on to improve the efficiency of the code perhaps by editing the translated code. I wouldn't call C a high language :)