answersLogoWhite

0

Why would you use a structure in programming?

Updated: 8/21/2019
User Avatar

Wiki User

7y ago

Best Answer

A data structure is an aggregate of data types. The data types may be arbitrary. For example:

struct Address {

int number;

char* street;

char* city;

char* county;

char postcode[9];

};

Had the data types been of the same type, we could have used an array instead of a structure:

struct Name {

char* first_name;

char* last_name;

};

char* Name_array[2]; // alternative to using a structure

However, unlike an array where each element is accessed using an index, members of a data structure are accessed by name:

void f (Name person) {

std::cout << person.first_name << ' ' << person.last_name;

}

Note that named members helps make code more readable. To achieve the same thing with an array, we'd need to use constants or (better still) an enumeration:

enum fields {first_name, last_name};

void g (Name_array* person) {

std::cout << person[first_name] << ' ' << person[last_name];

}

There is no advantage of one method over the other in terms of performance or memory consumption, however the structure method is arguably more intuitive. It's also much easier to work with an array of structures than with an array of arrays:

Name people[100]; // 100 name structures

And even easier when working with a structure of structures:

struct Name_address {

Name name;

Address address;

};

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why would you use a structure in programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the indentation in programming Why is it important?

In computer programming languages, indentation formats program source code to improve readability. Programming languages make use of indentation to define program structure . Programmers use indentation to understand the structure of their programs to human readers.


What is the message structure in windows programming?

No.


What is selection structure in programming?

2


What kind of programming structure should you use when you want to perform an action and then perform another action in order?

You tell me


What is a structure variable in computer programming?

A structure variable is a name that refers to a data structure. For example: struct S {/*...*/}; int main (void) { S x; /* x is a structure variable that refers to an instance of the structure S */ // use x... return 0; }


What is structured programming in generally?

It means using only three structure when programming: sequence, selection, repetition.


What are the classification of programming languages?

Programming languages, like human languages, are defined through the use of syntactic and semantic rules, to determine structure and meaning respectively. Thousands of different programming languages have been created, and new languages are created every year.


In structured programming the Sequence structure begins with a(n?

idea.


Data structure for language processing in system programming?

lado


You need to understand a subroutine's in order to use it?

You need to understand a subroutine's ____ in order to use it. a. internal structure b. local variables c. programming style d. interface


Why is it important to use the Structure Theorem in software design and programming?

beside its very convenient and easy to follow with, almost all high, low, and machine languages instruction are formulated to comply with the structure theorem.


What do you mean by non primitive data structure in C programming?

What do you mean by searching in data structure in C.?