answersLogoWhite

0

Relation between array and pointer

Updated: 8/10/2023
User Avatar

Wiki User

13y ago

Best Answer

A pointer identifies the location of some data in memory, similarly to an envelope with a street address on it. An array is several values of the same type, stored next to each other in memory. A useful consequence is that all of an array's elements may be found by simply counting up from the address of the first element.

Note that, when an array value is made available outside of its original scope, it decays to a pointer to its first element. So, for example, sizeof returns a different size for an array depending on which scope you call it in. In the array's original scope, it returns the number of elements in the array; in any other scope it returns the number of bytes used to store the pointer.

User Avatar

Wiki User

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

Wiki User

13y ago

Array name is a static pointer to the array.Meaning of static here is that we can not modify the pointer (array name) value to any other value.

Array name is the base address of the array in memory.

for example if we have array a[5] of float starting at address 1000 in memory then

a tells us the base address of array or address of a[0] i.e. 1000.

a+1 tells the address of element a[1] and so on.

we can access the element of an array by considering the array name as pointer.

*(a+2) will access the third element of an array a i.e. a[3].

but we can't achieve it by applying ++ operator 3 times since the array pointer can't be modified.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

No. An array is a contiguous memory allocation that may contain one or more elements of a given type. Arrays can be named if they are fixed-length and allocated in static memory or in local memory (the stack), however the name only refers to the start of the array. If allocated on the heap (whether fixed-length or variable-length) the array has no name, thus we must use a (separate) pointer variable to refer to its start address. However the array itself is not a pointer, it's simply a named or anonymous region of memory.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A pointer is normally considered an atomic data type. Therefore, a pointer cannot "be indexed like an array" in that it is not possible to access portions of a pointer through an index.

However, a pointer can point to data which can be accessed through pointer operations as well as through index operations. Further, multiple pointers can be arranged in an array of pointers, and each individual pointer within that array of pointers can be reached through its array index.

Example 1: an array of pointers

const char* const array_of_pointers[] = {

"Hello, World", "Hello, Venus", "Hello, Mars"

};

In this example, each of array_of_pointers[0], array_of_pointers[1] or array_of_pointers[2] evaluates to one pointer, selected with the array index 0..2.

Example 2: access to data through pointer using an array-style index

void example(char* const text) {

*text = 'a'; // set first character pointed to to 'a'

*(text+1) = 'b'; // second char becomes 'b'

// the following are equivalent to the above, but use index notation:

text[0] = 'a';

text[1] = 'b'; }

Key to example 2 is the declaration of the pointer as a constant pointer.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

It's actually quite hard NOT to reference an array using a pointer. All array types will implicitly convert to a pointer at the slightest provocation:

void f (const int* arr, const unsigned size) {

for (int i=0; i<size; ++i) {

printf ("%d\n", arr[i]);

}

}

int a[100]; /* static array (global) */

int main (void) {

int b[10]; /* local array */

int* c=malloc (1000 * sizeof(int)); /* dynamic array */

/* the following function calls are equivalent */

f (a, 100); /* a is a reference to the start address of the array named a */

f (&a, 100); /* &a is the address of the array named a */

f (&a[0], 100); /* &a[0] is the address of the first element of the array named a */

/* the following function calls are equivalent */

f (b, 10); /* b is a reference to the start address of the array named b */

f (&b, 10); /* &b is the address of the array named b */

f (&b[0], 10); /* &b[0] is the address of the first element of the array named b */

f (c, 1000); /* c is a pointer to the start address of an anonymous array */

f (&c, 1000); /* undefined behaviour: &c is the address of the pointer variable, not the array */

free (c);

c=0;

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

Anything stored in memory can be accessed using a pointer, however pointers are most useful when dealing with anonymous variables. An anonymous variable is simply a variable that doesn't have a name. This is useful because it would be impractical to name a thousand integers. But we can easily create an array of a thousand integers. That is, an array is simply a collection of anonymous variables.

Note that while we can name a fixed-length array, that name only refers to the start address of the array and thus to the first element in that array; all other elements are anonymous. However, all elements are the same length so if we know the start address we can easily refer to any element in the array using a zero-based memory offset. This is precisely how the array suffix operator works.

Consider the following array:

int x[1000];

This array has 1000 elements of type int. The first element will be found at memory offset 0 * sizeof (int) bytes from the start of the array. The next is offset by 1 * sizeof (int) bytes and the next is offset by 2 * sizeof (int) bytes. The last element is offset by 999 * sizeof (int) bytes. However, we don't actually need to know what sizeof (int) is because we already told the compiler this was an array of type int, thus sizeof (int) is an implicit scalar. All we really need to know is the offset index of each element, which in this example is in the range 0 to 999.

To access the nth element we use array suffix operator with the argument n-1. Thus the 42nd element will reside at index 41:

x[41] = 1066;

Here we've assigned the value 1066 to the 42nd element.

While array subscripts are intuitive, they are nothing more than sugar-coating. Behind the Scenes, the compiler treats x[41] = 1066 just as if we'd actually written *(x + 41) = 1066. Writing code like this may be less intuitive, but it can improve compile times (slightly) because we've already done part of the translation the compiler would have done. It's not going to improve runtime performance any because the resultant machine code is exactly the same regardless. Nevertheless, it is useful to know what's really going on "under the hood".

Most interestingly, for every built-in array a and integer j within the range of a, we find the following:

a[j] j[a]

In other words, x[41] and 41[x] are equivalent. This often surprises people, however you have to remember that the array subscript operator is nothing more than sugar-coating for pointer arithmetic. The compiler will convert j[a] to *(j+a) just as easily as it will convert a[j] to *(a+j). Such cleverness does not belong in production code of course.

Unlike fixed-length arrays which can be named, variable-length arrays are entirely anonymous. This is because a variable-length array must be allocated on the heap and all variables on the heap are anonymous; they only exist at runtime so we cannot name them at compile time.

This means we must use a pointer to store the start address at runtime:

int* p = malloc (1000 * sizeof (int));

Fortunately, C allows us to use the array suffix operator on pointers just as easily as we can with named arrays:

p[41] = 1066;

Remember that p[41] is the same as *(p + 41) as far as the compiler is concerned, hence the syntax is exactly the same.

Arrays cannot be passed to functions by value. That's simply impractical because passing an array with a 1000 integers would mean copying all those integers. If we really needed to copy an array then we can easily do so manually, as and when required. But we don't expect to copy arrays when passing them to functions, so the language simply doesn't allow it.

Converting an array to a pointer means all length information is lost. A pointer only tells us (and the compiler) the length of the first element (in bytes), but it doesn't tell us how many elements there are. We could interrogate the system's memory manager to find out how much memory was allocated, however that's a costly operation. It's much simpler to just store the length in a separate variable when the array was allocated or reallocated.

Functions that accept an array have the following prototype:

void func (int*, const unsigned);

The pointer type will obviously vary according to the element type of the array being passed. Depending on what operation the function performs, there may also b one or more additional arguments.

Let's suppose we want to fill an integer array with a specific value (thus initialising the array). We can do so with the following function:

void init (int* a, const unsigned sz, int value) {

for (unsigned i=0; i

}

Let us also suppose we wish to use a scalar to increment the elements of an array:

void incr (int* a, const unsigned sz, int value) { for (unsigned i=0; i

}

Calling these functions with a fixed-size array is very simple:

const unsigned sz = 1000;

int x[sz];

init (x, sz, 0); // zero-fill the array

incr (x, sz, 42); // add 42 to each element

Variable-length arrays are passed in exactly the same way:

unsigned psz = 1000;

int* p = malloc (psz * sizeof (int));

if (!p) { /* handle error */ }

init (p, psz, 0);

incr (p, psz, 42);

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

An array is nothing more than a reference to a contiguous block of memory of sufficient length to store a given number of elements of a given type. The elements themselves are anonymous (they have no names) so the only way to refer to them is through pointer variables.

Consider the following array:

int A[5] {5, 10, 15, 20, 25};

Here we've allocated sufficient storage to hold 5 integers. The array name, A, is a reference to the start address of the allocation, and thus to the first element only:

assert (A == &A);

assert (A == &A[0]);

To refer to any other element we must use a zero-based index because the first element is always offset by zero elements from the start address while the nth element is offset by n-1 elements.

assert (&A[3] == A+3); // the fourth element!

When we refer to A[3], we obtain the value stored at the address A+3:

assert (A[3] == 20);

assert (*(A+3) == 20);

Thus it can be seen that the array subscript operator is really just sugar-coating for pointer arithmetic.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

yes.. an array can be accessed through pointer

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

An array can be regarded as a constant pointer, example:

int a[10], *p;

p= a; p[1]= a[1]; *a= *p; /* all okay */

a= p; /* WRONG */

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Relation between array and pointer
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Passing an array name to a pointer assigns the first memory location of the array to the pointer variable?

Yes, passing an array name to a pointer assigns the first memory location of the array to the pointer variable. An array name is the same as a pointer to the first location of the array, with the exception that an array name is a r-value, while a pointer is an l-value.


Which two pointer does not increment or decrement in arithmetic array?

constant pointer and character pointer


How do you return an array from function?

By returning a pointer to the first element of the array.


How do you convert from array notation to pointer notation?

In the C and C++ languages the array notation arr[i] is completely equivalent to the pointer notation *(arr + i).


Does mentioning the array name gives the base address in all the contexts?

Mentioning the array name in C or C++ gives the base address in all contexts except one. Syntactically, the compiler treats the array name as a pointer to the first element. You can reference elements using array syntax, a[n], or using pointer syntax, *(a+n), and you can even mix the usages within an expression. When you pass an array name as a function argument, you are passing the "value of the pointer", which means that you are implicitly passing the array by reference, even though all parameters in functions are "call by value". There is, however, one very important distinction. While an array name is referentially the same as a pointer, it is not a pointer in that it does not occupy program referential space in the process. This means that, while you can change the value of a pointer, and thus the address to which it points, you can not change the value of an array name. This distinction is what we call R-Value (array or pointer) as opposed to L-Value (pointer only), i.e. can the object appear on the left sign of an assignment operator.

Related questions

Passing an array name to a pointer assigns the first memory location of the array to the pointer variable?

Yes, passing an array name to a pointer assigns the first memory location of the array to the pointer variable. An array name is the same as a pointer to the first location of the array, with the exception that an array name is a r-value, while a pointer is an l-value.


What is a pointer in the array?

A pointer into an array of elements of type E is a pointer to a single element of type E:typedef ..... E;E array[123];E* const pointer = &array[18]; // points to the 19th element inside 'array'An array of pointers is an array whose elements are pointers:typedef .... E;E* array[123];E** const pointer = &array[18]; // points to the 19th pointer within 'array'Referencing the name of the array variable without use of the index operator itself is a constant pointer to its first element. Therefore, the following if-clause is always true:typedef .... E;E array[123];if (array &array[N]) { // ALWAYS true ...}


Why you use an array of pointer to pointer?

because u freakin can


Which two pointer does not increment or decrement in arithmetic array?

constant pointer and character pointer


Why can't we increment an array like a pointer?

once we initialize the array variable, the pointer points base address only &amp; it's fixed and constant pointer


How do you return an array from function?

By returning a pointer to the first element of the array.


How do you convert from array notation to pointer notation?

In the C and C++ languages the array notation arr[i] is completely equivalent to the pointer notation *(arr + i).


What is the relationship between an array and a pointer?

All variable names are an alias for the value stored at the memory address allocated to them. To get the memory address itself, you must use the address of operator (&). The value returned from this can then be stored in a pointer variable.Arrays are different. The array name is an alias for the start address of the array, thus you do not need the address ofoperator to obtain the memory address (although you can if you want to). This means that when you pass an array name to a function, you pass the memory address of the array rather than passing the array itself (which would require the entire array to be copied, which is a highly inefficient way to pass an array). In essence, the array is passed by reference rather than by value.Consider the following code. This shows how a primitive variable name differs from the name of an array of primitive variables. The final portion shows how a pointer can be used to achieve the same results you got by accessing the array elements directly from the array name itself. This is in fact how the compiler implements arrays, using pointers, but there's no need to do this in your code. Accessing array elements directly by their index is a programming convenience.#include using namespace std;int main(){int i = 10;cout


What is pointer to a structure?

A pointer is a variable that holds address information. For example, in C++, say you have a Car class and another class that can access Car. Then, declaring Car *car1 =new Car() creates a pointer to a Car object.. The variable "car1" holds an address location.


What is the easiest way to pass arrays as argument in c?

the simple and efficient way to pass an array is pointer to an array like that int (*p)[30] ; // pointer to an array of integer having 30 element


How do you declare a string array and add elements to it in C plus plus?

You cannot add elements to a fixed array in C or C++. If, however, the array is declared as a pointer to an array, you can add elements by allocating a new array, copying/adding elements as needed, reassigning the new array to the pointer, and deallocating the original array.


When does an array behave a pointer?

An array behaves like a pointer when you use its name in an expression without the brackets.int a[10]; /* a array of 10 ints */int *b = a; /* a reference to a as a pointer, making b like a */int c = *(a+3); /* a reference to a[3] using pointer semantics */myfunc(a); /* pass a's address, a pointer to myfunc */Note very carefully that, while an array name and a pointer can almost always be interchanged in context, the are not the same, in that a pointer is an l-value, such as b, above, and can be assigned, whereas a is an r-value and can only be referenced, such as in the same statement, the second statement. Also, an array name does not take up memory, while a pointer does.