answersLogoWhite

0

Structures are a type of variable, just as int and char are types of variable. All variables of the same type have one thing in common: their size is always the same. Typically, an int is 4 bytes, and a short is 2 bytes while a char is 1 byte. So a structure with 2 ints is therefore 8 bytes in length. A structure with an int and a char is 5 bytes, but structure member alignment will typically round this up to the nearest 4 bytes, padding any unused bytes (dependant upon your compiler's member alignment options). Regardless, a structure's size is known in advance by the number and type of its member variables. It's size is fixed. This is important when creating arrays of structures because every element in the array must be same size, and the size must be known in advance.

This then makes it impossible to store variable length strings and other dynamic arrays within a structure. That is where pointer variables come in. Since all they store is a memory address, pointer variables are a fixed size (4 bytes on a 32-bit system). It really doesn't matter how much memory is allocated to that memory address, because that memory is not physically part of the structure -- only the pointer variable itself is a member of the structure. The pointer variable simply "points" to the memory address stored in it, and the type of the pointer determines how that memory is accessed, whether it is a pointer to a primitive variable type, an array of char or an array of another structure altogether. Ultimately, the structure is only concerned with the pointer variable, not what it points at.

This then allows your structures to make better use of memory. There is no need to allocate an array far larger than you actually need, simply to cater for the "worst-case".

Other uses for pointers within structures include the ability to point at other structures of the same type, such as is required in linked lists. You cannot do this with references because the reference must be completely defined in advance; a structure that references its own type would require infinite memory because that reference contains another reference, which contains another reference... But it can point at another instance of the structure, including itself, because pointers only need to be declared, not completely defined.

The same applies to classes with member pointer variables. The only difference is that structures have public access by default while classes have private access by default. But classes can point to structures and vice versa. Apart from that, classes are just like any other variable type; they are a fixed size.

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What is the role of a changed variable in an experiment?

Independent variable


What is the role of responding variable in a controlled experiment?

it is basically asking what the definition of responding variable is and the book says, The variable that changes because of the manipulated variable is the responding variable.


Role of instruction pointer in 8086?

Its role is to point to the next instruction to be executed in the CPU. It always points to the next instruction to be executed in the CPU


What role does the responding variable do in a controlled experiment?

changes result!


What role does independent and dependent variable play in controlled experiments?

The independent variable in an controlled experiment is what you are changing (for example, amount of water or sunlight a plant gets). The dependent variable changes because of the independent variable. Its the outcome of the independent variable.


What role do z-scores play in the transformation of data from multiple distributions to the standard normal distribution?

None.z-scores are linear transformations that are used to convert an "ordinary" Normal variable - with mean, m, and standard deviation, s, to a normal variable with mean = 0 and st dev = 1 : the Standard Normal distribution.


As we begin thisweek's forum, please consider the role of data Data allows organizationsto more effectively determine the cause ofAs we begin this week's forum, please consider the role of data?

As we begin this week's forum, please consider the role of data? Data allows organizations to more effectively determine the cause ofAs we begin this week's forum, please consider the role of data


What is a role of a software?

A software engineer is a person who builds or designs software. A software engineer must, of course, be a good programmer, be well versed in data structures and algorithms, and be fluent in one or more programming languages.


What is the role of a software?

A software engineer is a person who builds or designs software. A software engineer must, of course, be a good programmer, be well versed in data structures and algorithms, and be fluent in one or more programming languages.


What are some of the structures inside a cell the help it to live and perform its role in an organism?

Some of the structures inside a cell that help it to live and preform it's role in an organism are neucleus and mitochondria


What is node class?

When creating linked lists (singly- or doubly-linked), the elements in the list are known as nodes. A node class defines how these nodes work. Generally, a node will contain a member pointer to the data it represents (which is typically an abstract data type rather than a concrete data class), as well as a pointer to the next node in the list, and the previous node if it is doubly-linked. The list itself maintains a member pointer to the head node in the list, and the tail node if doubly-linked. In this way, the list is only concerned with the nodes, not the data within those nodes. By the same token the nodes are only concerned with their nearest neighbouring nodes, not the list, nor the data they contain. Similarly, the data is only concerned with itself and other data, it is not concerned with the fact it may be part of a list or a node. All work concerning nodes can then be delegated to the nodes themselves, while any work relating to the data can be delegated to the data. Thus each class plays a small but clearly defined role within the list; no class does any more than it has to, which greatly simplifies the list interface.


Nature of data?

khabarbabal.online/file/ZWE3ZjFmOWQt