answersLogoWhite

0


Best Answer

Data typing is static, but weakly enforced

User Avatar

Mellie Gulgowski

Lvl 10
3y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

With a normal data type, such as int, you can only store one value. With an array, you can store several values of the same type in contiguous memory, one after the other. So the first major difference is in the amount of memory allocated.


int a; // allocates 4 bytes

int b[2]; // allocates 2 x 4 bytes (8 bytes in total)


The preceding examples assume sizeof(int) is 4 bytes.


The second major difference is in how the memory is accessed. In the preceding examples, you can assign any integer value to the variable a directly, such as:


a = 5;


However you cannot do the same with b. This is because b is actually a reference to the start address of the array.


b = 5; // can't do this!


To assign a value you must dereference b:


*b = 5; // ok


Typically you will use the index operator to achieve the same thing:


b[0] = 5; // zero-based index


The index operator also allows access to any element in the array, not just the first element:


b[1] = 6; // assign to second element


Although the index operator is generally easier for the programmer, this is simply sugar-coating for what is actually going on Behind the Scenes: pointer arithmetic:


int * p = b; // point to the reference

++p; // increment the pointer

*p = 6; // assign the value


Note that ++p does not increment p by 1, it increments p by sizeof(int), because b was declared to be an array of int.


Another difference between an array and a normal data type is that it is impossible to determine how much memory is allocated to the array. It is therefore the programmer's responsibility to ensure he does not write to memory beyond the array, by maintaining the size of the array in a separate variable. This is also the reason why we must pass the size of an array as well as the array itself into external functions.


It should be noted that in C++ we should avoid arrays altogether as they are not part of the object-oriented paradigm; they were simply inherited from C. Instead we should use vectors. A vector is simply a template class that encapsulates and exposes all the functionality of an array, but combines it with common methods and members associated with arrays, including the size of the array. Passing a vector to an external function is therefore much safer than passing an array because the vector knows the size of the array -- we don't have to keep track of it ourselves. We can still use arrays, of course, but vectors are part of the C++ language and they ensure our programs conform to the object-oriented paradigm.


This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between array and normal data type?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

What is the difference between an array and structure?

An array is a collection of related data elements of same type.Structure can have elements of different types.An array is a derived data type.A structure is a programmer-defined data type.A struct can contain multiple data types, whereas an array can not.


What is the difference between array and enum?

Array is collection of data items of same data type.Enum is collection of data items of different data type.


What is the difference between array linklist?

Linked list consists of data nodes each pointing to next in the list .An array consist of contiguous chunk memory of predetermined size


What is the difference between varchar and nvarchar?

The difference between varchar and nvarchardatatypes is that Nvarchar stores UNICODE data. If you have requirements to store UNICODE or multilingual data, nvarcharis your choice. Varchar stores ASCII data and should be your data type of choice for normal use.


What is the difference between queues and circular queues?

A queue can use a dynamic array, or a linked list, but if using static memory, the queue becomes a circular queue because the underlaying data structure is a static circular array. This means the ends of the array are attached.


What is the Difference between array and vector processors?

Vector processor and Array processor are just the same thing, its a CPU design where instruction are set includes operations that can perform mathematical operations on multiple data elements simultaneously.


What is the difference between OLAP and data mining?

difference between Data Mining and OLAP


What is the difference between delete and remove in C?

Delete removes data forever, after using delete you will not be able to use deleted data again. Remove is different and action depends on where you are going to apply Remove. If you apply it for ListBox which are using data from an array. After you used applied the command to the ListBox you are still able to get access to the data using array. But if the ListBox the only source of data then Remove is equal to Detele.


What is the difference between allocation and search data structure?

difference between serch data structure and allocation data structure


When an array contains values other than primitive data types is it considered a control array?

Only if the non-primitive data types are actually controls, such as an array of label controls, or an array of edit boxes. However, a control array is still an array. The only difference is that the values will likely be resource handles (objects that refer or point to the actual object which will be stored elsewhere in memory) rather than an actual value itself. That is, an array of primitive data types stores the actual value in the array itself.


What is the difference between an array of structures and an array within a structure?

The main differences between an array and a structure are: An Array is a collection of similar data items.An array is derived data type.It behave like a built in data type. An array can be increased or decreased. A structure is a collection of dissimilar data items.It is a user defined data types.It must be declared and defined.A structure element can be added if necessary.


What is the difference between data bus and normal cable?

Data bus moves data inside your computer. Normal cable moves data external. Data buses are parallel and most network media is serial. Some media is also analog where as the data bus passes data digitally. There are many differences but to be more specific would depend on which 'normal cable' you are referring too.