answersLogoWhite

0

Arrays in c

Updated: 8/9/2023
User Avatar

Wiki User

10y ago

Best Answer

1>an array is a static data structure.after declaring an array it is impossible to change its size.thus sometime memory spaces are misused.

2>each element of array are of same data type as well as same size.we can not work with elements of different data type.

3>in an array the task of insertion and deletion is not easy because the elements are stored in contiguous memory location.

4>array is a static data structure thus the number of elements can be stored in it are somehow fixed.

User Avatar

Wiki User

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

Wiki User

13y ago

If you're asking, how do you make a 2D array in C, it's like this:

int array[10][10];

This will give you a 10x10 2D array. To access the values:

int value1 = array[0][0];

It is the same as with a one dimensional array, except that you use two sets of square brackets instead of one.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Arrays are used to store a sequence of homogeneous or covariant data types in contiguous memory.

Arrays may be allocated statically, on the stack or on the heap. Static arrays must be fixed-length however arrays on the stack or heap may be either fixed-length or variable-length:

int a[10]; /* static array of 10 elements (fixed-length) */

void f (const unsigned int x) {

int b[10]; /* stack array of 10 elements (fixed-length) */

int c[x]; /* stack array of x elements (fixed-length) */

int * d = malloc (10 * sizeof(int)); /* heap array of (initially) 10 elements (variable-length) */

int * e = malloc (x * sizeof(int)); /* heap array of (initially) x elements (variable-length) */

}

Note that pointers d and e are both allocated on the stack, but the arrays they point to are allocated on the heap.

The elements within an array are anonymous (unlike primitive variables, they have no names associated with them), however we can refer to them by their individual addresses. The name of the array (or a pointer to the array) serves as a reference to the start of the array, thus all elements are referenced via a zero-based offset from this address (where the first element is at offset 0). Given that every element is the same length (in bytes), it is trivial to calculate the address of any element within the array given that element's zero-based index (where the first element is at index 0). However, the array suffix operator [] provides a more convenient notation for the programmer without the need to use pointer arithmetic.

void f (void) {

int x = a[5]; /* store the value of the 6th element of a in x */

}

An array's name (if it has one) implicitly converts to a pointer when passed to a function. However, a function cannot determine the length of an array from a pointer alone, you must also pass the length of the array via a separate argument:

void f(const int const * a, const unsigned int n) {

/* a is a constant pointer to an array of n constant integers */

}

To avoid buffer overflows, it is important that the programmer keep track of all array lengths independently of the arrays themselves. For fixed-length arrays this is typically achieved through constants while variable-length arrays use variables. Every time an array is reallocated, the variable indicating its length must be updated according to the new length.

In order to maintain the relationship between an array and its length, we may use a data structure:

typedef struct my_array {

int* the_array;

unsigned int length;

};

Multi-dimensional arrays are represented as arrays of arrays:

int x[5][10];

Here, x is an array of 5 elements where each element is itself an array of 10 integers. We can think of this array as being a table of 5 rows and 10 columns holding 50 integers in total (just as if we'd declared the array int x[50]). The difference is that when we access element x[3], say, we are actually referring to an array of 10 integers rather than just a single integer. Mapping the integers multi-dimensionally allows us to access individual elements by row and column indices, just as we would a spreadsheet, grid or matrix.

Multi-dimensional variable-length arrays are a bit more complex than their fixed-length counterparts, but they are more flexible in that the inner arrays need not be the same length. For instance, an array of variable length strings is effectively a two-dimensional array because the inner arrays are null-terminated character sequences:

char* string[10];

Here, string is a contiguous array of 10 pointers to char, but each pointer can refer to any character array no matter where it is allocated. Thus string[4] would refer to the fifth character array while string[4][2] would refer to the third character in that array. Note that when we define multi-dimensional arrays non-contiguously, each individual array must be contiguous within itself and each array of pointers adds another level of indirection. Thus if we wanted a variable-length array of variable length strings, we must use a pointer to pointer to char:

char** string;

That is, string can now be used to point to a contiguous array of pointers to char.

Other than the physical limitations of the hardware, there is no limit to the number of dimensions we may define. A three-dimensional array can be imagined as being an array of grids or a grid of tables or as a cube (depending on what it is the array actually represents). We can extend this approach into four dimensions and beyond, such that a four-dimensional array may be thought of as being an array of cubes, or a table of tables, or a cube of arrays, and so on.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Array is a set of logically related elements belonging to same data types.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Although array provide an easy way to store multiple value of the same type together, they have certain limitations..

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Arrays in c
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the required syntax for creating C arrays?

The required syntax for creating C arrays include the brackets, array size, variety length arrays, codes like std:vector, classPTR, and many more to create C arrays.


What is an arrays in c?

array is collection of many data


What is the purpose of using arrays in C language?

The purpose of using arrays in C is to store multiple values in one variable. Then you can make programs that use arrays like lists, printing values from multiple arrays into one line. It take memory in continues block then we can know memory location easily. We can retrieve data quickly.


What are the differences between structures and arrays?

Arrays are collections of repeated data items. Structures are complex data items made up of other data items, including, potentially, other structures and arrays. You can, of course, also have arrays of structures. Array items can be accessed by using its subscript whereas structure items can be accessed using its dot or "arrow" operator in C, C++, C#, Java, and JavaScript.


What are the data type in c language?

Some of them are: 1. char, short, int, long, float, double 2. pointers to these 3. arrays of these 4. arrays of pointers 5. pointers to arrays ...


What is the Difference between arrays in c and c plus plus?

Nothing whatsoever. They are exactly the same.


What has the author John C Reynolds written?

John C. Reynolds has written: 'Reasoning about arrays'


Array in c plus plus must be defined at compil time?

No. Arrays can be defined at runtime, just as they can in C. It's just that it's generally more convenient to use vectors instead of dynamic arrays at runtime, thus arrays are generally used statically, at compile time.


How do you swap two arrays in c plus plus using string?

You can't. While a string is a character array, an array is not necessarily a string. Treating arrays as if they were strings simply to swap them is madness. The correct way to physically swap arrays A and B is to copy A to a new array, C, then copy B to A, then C to B. If the arrays are the same size this is not a problem. If they are different sizes, you can only swap them if they are dynamic (not static). This means you must reallocate them. To speed up the process, copy the smallest array to C, first. A much better approach would be to point at the two arrays and swap the pointers instead.


Are arrays in C created on the stack or the heap?

That depends on where you define them. Arrays defined inside functions are declared on the stack (like other variables defined in functions). Arrays defined outside of any function, or using the static keyword inside a function are allocated in the static data area of the program. Other arrays may be allocated using malloc() (or "new" in C++); these are allocated on the heap.


What are Strings in C programming?

Arrays of chars are strings. there is a built in librray, that handles string string.h but the data-type is held as arrays of chars. char[10] c="string"; translate to ['s','t','r','i','n','g',\0] Arrays of chars are strings. there is a built in librray, that handles string string.h but the data-type is held as arrays of chars. char[10] c="string"; translate to ['s','t','r','i','n','g',\0]


To enter n nos without using arrays in c?

Use a linked-list.