answersLogoWhite

0


Best Answer

An array of pointers is that for eg if we have array of 10 int pointers ie int *a[10] then each element that which is stored in array are pointed by pointers. here we will have ten pointers. In pointer to an array for eg int(*a)[10] here all the elements that is all the ten elements are pointed by a single pointer.

User Avatar

Wiki User

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

Wiki User

14y ago

There is no such thing as an "array to pointer." What you might be asking is "array of pointers."

An array of pointers is just that, an array in which the variables are pointers. In C this would be an array of pointer variables that are each 4 bytes in size. It is declared like this:

int *pointers[3];

A pointer to an array is a pointer that points to the whole array. For example, in C if you have

int numbers[5][10];

int (*pointerToArray)[10] = numbers + 2;

pointerToArray points to the third element of numbers, which is itself an array.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The following program demonstrates the difference and also highlights one of the dangers of getting the two mixed up.

The program begins by declaring two static arrays, each with 5 elements. The first, a, is an array of int, while the second, b, is an array of pointer to int. We then initialise both arrays with values 1 to 5. Note that since array b is an array of pointers, we must give it some memory to point at. We could simply point at the individual elements of a if we wished, but in this example we'll allocate new memory from the heap instead.

Next, we create some pointers to point at these arrays. p1 points to a, while p2 points to b. Note that becuase b is an array of pointers, p2 needs an extra level of indirection, hence we use ** rather than * as we did with p1. We'll come to p3 later, but note that there is only one level of indirection, and that it is currently pointing at element b[0].

Next we print all the details relating to all our variables. You will note that the address of a is the same as the value of a, and that the address of b is the same as the value of b. This is because a and b are both references to the arrays; that is, they each refer to the start address of the memory allocated to the arrays. They are not variables like pointers, so you cannot change what address they refer to once they are assigned.

When we dereference a (using *a), we're effectively looking at the value of a[0] (which is 1). You will note that &a, a, and &a[0] all refer to the same memory address. By the same token, when we dereference b (using *b) we're dereferencing the value of b[0] which is a pointer. So to dereference that pointer we use an extra level of indirection (**b) to obtain the value.

After listing all the addresses and values of the array elements (including the dereferenced pointers in b), we examine the pointers. Note that the address of p1 and p2 are different to that of &a and &b. Remember that p1 and p2 are pointers, and therefore they each have their own memory address. However, the remaining details are the same as for a and b.

At this stage, p3 is a valid pointer so its value is the same as *b, b[0] and *p2, while its dereferenced value (*p3) is the same as **b, *b[0] and **p2. However, it has undefined behaviour, as we'll see after we increment the pointers.

Although p1 is a pointer to an array of int, it is really just a pointer to int. So when we increment p1 it increments by sizeof(int) and therefore points at the address of a[1] with the value 2. Similarly, p2 is a pointer to a pointer to an int, thus it ends up pointing at the pointer stored in b[1] which points to an integer elsewhere in memory (on the heap) with the value 2.

p3 is also a pointer to an int, like p1, but we assigned it the memory address stored in b[0], which was 0x00350990 (see the example output below the program). This is never a good idea. Although that address is perfectly valid, as soon as we increment p3 we're in uncharted waters because we end up pointing at memory address 0x00350994 (a difference of 4 bytes, or sizeof(int) bytes). But the address we probably expected was the one stored in b[1], which is 0x003509C0, a full 48 bytes away! Remember that we allocated memory to b's elements one at a time, so there's no guarantee those allocations will be contiguous. In fact, I'd be more surprised if they were!

The lesson here is that if you're going to point at arrays of pointers and wish to navigate through the array, do make sure you point at the array itself (as we did with p2), and never to the value in one of its elements (as we did with p3). Otherwise there's no telling what you'll end up pointing at.

#include<iostream>

int main()

{

int a[5]; // array of int

int* b[5]; // array of pointer to int

// Initialise arrays:

for(int i=0; i<5; ++i)

{

a[i] = i+1;

b[i] = new int(i+1);

}

int* p1 = a; // pointer to array of int

int** p2 = b; // pointer to array of pointer to int

int* p3=b[0]; // undefined behaviour!

std::cout<<"ARRAYS\n"<<std::endl;

std::cout<<"&a=0x"<<&a;

std::cout<<"\ta=0x"<<a;

std::cout<<"\t*a="<<*a<<std::endl;

std::cout<<"&b=0x"<<&b;

std::cout<<"\tb=0x"<<b;

std::cout<<"\t*b="<<*b;

std::cout<<"\t**b="<<**b<<std::endl;

std::cout<<"\nELEMENTS\n"<<std::endl;

for(int i=0; i<5; ++i)

{

std::cout<<"&a["<<i<<"]=0x"<<&a[i];

std::cout<<"\ta["<<i<<"]="<<a[i]<<std::endl;

}

std::cout<<std::endl;

for(int i=0; i<5; ++i)

{

std::cout<<"&b["<<i<<"]=0x"<<&b[i];

std::cout<<"\tb["<<i<<"]=0x"<<b[i];

std::cout<<"\t*b["<<i<<"]="<<*b[i]<<std::endl;

}

std::cout<<"\nPOINTERS\n"<<std::endl;

std::cout<<"&p1=0x"<<&p1;

std::cout<<"\tp1=0x"<<p1;

std::cout<<"\t*p1="<<*p1<<std::endl;

std::cout<<"&p2=0x"<<&p2;

std::cout<<"\tp2=0x"<<p2;

std::cout<<"\t*p2=0x"<<*p2;

std::cout<<"\t**p2="<<**p2<<std::endl;

std::cout<<"&p3=0x"<<&p3;

std::cout<<"\tp3=0x"<<p3;

std::cout<<"\t*p3="<<*p3<<std::endl;

std::cout<<"\nIncrementing pointers...\n"<<std::endl;

++p1;

++p2;

++p3;

std::cout<<"&p1=0x"<<&p1;

std::cout<<"\tp1=0x"<<p1;

std::cout<<"\t*p1="<<*p1<<std::endl;

std::cout<<"&p2=0x"<<&p2;

std::cout<<"\tp2=0x"<<p2;

std::cout<<"\t*p2=0x"<<*p2;

std::cout<<"\t**p2="<<**p2<<std::endl;

std::cout<<"&p3=0x"<<&p3;

std::cout<<"\tp3=0x"<<p3;

std::cout<<"\t*p3="<<*p3<<std::endl;

std::cout<<std::endl;

for(int i=0; i<5; ++i)

delete( b[i] );

}

Output:

ARRAYS

&a=0x002CFBBC a=0x002CFBBC *a=1

&b=0x002CFBA0 b=0x002CFBA0 *b=00350990 **b=1

ELEMENTS

&a[0]=0x002CFBBC a[0]=1

&a[1]=0x002CFBC0 a[1]=2

&a[2]=0x002CFBC4 a[2]=3

&a[3]=0x002CFBC8 a[3]=4

&a[4]=0x002CFBCC a[4]=5

&b[0]=0x002CFBA0 b[0]=0x00350990 *b[0]=1

&b[1]=0x002CFBA4 b[1]=0x003509C0 *b[1]=2

&b[2]=0x002CFBA8 b[2]=0x003509F0 *b[2]=3

&b[3]=0x002CFBAC b[3]=0x00350A20 *b[3]=4

&b[4]=0x002CFBB0 b[4]=0x00350A50 *b[4]=5

POINTERS

&p1=0x002CFB88 p1=0x002CFBBC *p1=1

&p2=0x002CFB7C p2=0x002CFBA0 *p2=0x00350990 **p2=1

&p3=0x002CFB70 p3=0x00350990 *p3=1

Incrementing pointers...

&p1=0x002CFB88 p1=0x002CFBC0 *p1=2

&p2=0x002CFB7C p2=0x002CFBA4 *p2=0x003509C0 **p2=2

&p3=0x002CFB70 p3=0x00350994 *p3=-33686019

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A pointer is a variable that may contain a memory address, or NULL (zero).

An array is collection of data, usually residing in a contiguous block of memory, where every element is accessed as an offset from the beginning of the array.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

pointer is use to hold address of another variable whereas array is a collection of elements of similar datatype

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Pointer holds an address Array holds values

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between array of pointers and pointer to an array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why array is a implicit pointer?

An array of pointers is exactly what it sounds like - one or more pointers arranged in order in memory, accessible through a common base name and indexed as needed. Philosophically, there is no difference between an array of pointers and an array of objects...int a[10]; // 10 integers, named a[0], a[1], a[2], ..., a[9]int *b[10]; // 10 pointers to int, named b[0], b[1], b[2], ..., b[9]If you initialize the array of pointers...int i;for (i = 0; i < 10; i++) b[i] = &a[i];... then *b[0] would be the same as a[0], etc.


Difference between array and pointers in c plus plus?

A pointer is simply a variable that can store a memory address and has the same purpose in both languages. The only real difference is that C++ pointers can point at objects (instances of a class) and indirectly invoke their methods, whereas pointers in C (which is not object oriented) cannot.


Pointer to 3 dimensons array?

If the array is static you can simply point at the first element. For dynamic arrays you can allocate a contiguous block to a single pointer which can then be subdivided using a one-dimensional array of pointer to pointers, each of which points to a one-dimensional array of pointers, each of which points to a separate object within the array. For extremely large arrays, however, it is better to split the elements into separate one-dimensional arrays, by creating a one-dimensional array of pointer to pointers first, then allocating each of those pointers to a separate one-dimensional array of pointers, each of which points to a separate one-dimensional array of objects. Either way, you must destroy all the individual arrays in the reverse order of creation.


What is Arrays of Pointers?

An array of pointers is a contiguous block of memory that contains pointers to other memory locations. They essentially allow non-contiguous memory locations to be treated as if they were an actual array.


What are the array operations?

There are no array operations in C. Arrays implicitly convert to pointers, thus any operation you might attempt upon an array you would actually perform on a pointer.

Related questions

Why array is a implicit pointer?

An array of pointers is exactly what it sounds like - one or more pointers arranged in order in memory, accessible through a common base name and indexed as needed. Philosophically, there is no difference between an array of pointers and an array of objects...int a[10]; // 10 integers, named a[0], a[1], a[2], ..., a[9]int *b[10]; // 10 pointers to int, named b[0], b[1], b[2], ..., b[9]If you initialize the array of pointers...int i;for (i = 0; i < 10; i++) b[i] = &a[i];... then *b[0] would be the same as a[0], etc.


Why does array index start with zero in C language?

Because of pointers and that all arrays are really pointers. A pointer something like *pointer can also be written as pointer[0] and *(pointer + 1) can also be written as pointer[1]


Difference between array and pointers in c plus plus?

A pointer is simply a variable that can store a memory address and has the same purpose in both languages. The only real difference is that C++ pointers can point at objects (instances of a class) and indirectly invoke their methods, whereas pointers in C (which is not object oriented) cannot.


Pointer to 3 dimensons array?

If the array is static you can simply point at the first element. For dynamic arrays you can allocate a contiguous block to a single pointer which can then be subdivided using a one-dimensional array of pointer to pointers, each of which points to a one-dimensional array of pointers, each of which points to a separate object within the array. For extremely large arrays, however, it is better to split the elements into separate one-dimensional arrays, by creating a one-dimensional array of pointer to pointers first, then allocating each of those pointers to a separate one-dimensional array of pointers, each of which points to a separate one-dimensional array of objects. Either way, you must destroy all the individual arrays in the reverse order of creation.


What is the difference between a pointer and an array in C programming?

An array is a contiguous block of memory containing one or more elements of the same type. The array identifier is a reference to that block of memory. References do not require any memory other than the memory they reference. References are not variables -- they are merely aliases for memory addresses. A pointer is a variable that can store any reference and provides indirect access to that reference. Pointers can be used to allocate arrays dynamically, which means memory is required not only for the array, but also for the pointer to store the starting memory address of the array. Once allocated, the array can be referenced directly, but the pointer is still required in order to release the memory allocation when it is no longer required. Arrays employ pointer arithmetic to locate individual elements by their zero-based index, which is essentially an offset from the reference multiplied by the size of the element type. However arrays and pointers are not the same from a programming standpoint. Your compiler may well implement references as pointers, but that is only of concern to compiler authors, not programmers. Two-dimensional dynamic arrays make use of a pointer-to-pointer variable to point to a one-dimensional array of pointers, each of which points to a one-dimensional array of the actual elements. A three-dimensional array employs a pointer-to-pointer-to-pointer to point to a one-dimensional array of pointer-to-pointer variables, each of which points to a two-dimensional array. And so on. Static arrays use less memory as there is no need to maintain arrays of pointers, but static arrays are only useful when the number of elements and dimensions are known at compile time. At runtime, arrays of pointers are required over and above the array elements themselves, in order to both allocate and deallocate the memory, as well as obtain references to the elements in the array. Pointers also have uses beyond that of dynamic arrays, including allocating any type of memory of any size, and pointing at functions which can then be passed as arguments to other functions.


What is Arrays of Pointers?

An array of pointers is a contiguous block of memory that contains pointers to other memory locations. They essentially allow non-contiguous memory locations to be treated as if they were an actual array.


Array of pointers?

a pointer is a variable that contains memory location of another variable.the value u assign to the pointers are memory address of other variable.


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 ...}


What are the array operations?

There are no array operations in C. Arrays implicitly convert to pointers, thus any operation you might attempt upon an array you would actually perform on a pointer.


What is an array of pointers to pointers?

A pointer is a variable that stores value of address of a variable. Since a pointer itself is a variable, it is allocated a memory location.Pointer to pointer means a pointer which points to the address of a pointer. In other words a pointer to a pointer has the address of the address of a variable.We can have pointers to int, and pointers to char, and pointers to any structures we've defined, and in fact pointers to any type in C, it shouldn't come as too much of a surprise that we can have pointers to other pointers. If we're used to thinking about simple pointers, and to keeping clear in our minds the distinction between the pointer itself and what it points to, we should be able to think about pointers to pointers, too, although we'll now have to distinguish between the pointer, what it points to, and what the pointer that it points to points.


Is there a difference between an array and a pointer?

Array is a data type which can be represented as a[1],a[2],a[3].... Pointer is also a data type. The speciality of pointer is instead of address of the variable it will give the reference to the address of the variable. E.g. int *test Here test is a pointer variable. which will be the reference to the address == One dimensional arrays are constant pointers, you cannot change their values, eg: int a[10], *pa = &amp;a[3]; p[0]= a[0]; // ok a[0]= p[0]; // ok p= a; // ok a= p; // INVALID for a is constant


How the pointers is used with arrays?

A pointer can point to any element of the array, the array itself is a constant pointer. Eg.: int a[10], *p; p= &amp;a[3]; p= a; /* the same as p= &amp;a[0] */ a[2]= *p; a[3]= p[4]; a= p; /* WRONG! */