answersLogoWhite

0


Best Answer

#include
void

printarr(int

a[]);
void

printdetail(int

a[]);
main()
{
int

a[5];
for

(int

i = 0;i<5;i++)
{
a[i]=i;
}
printdetail(a);
}
void

printarr(int

a[])
{
for

(int

i = 0;i<5;i++)
{
printf("value in array %d\n"

,a[i]);
}
}
void

printdetail(int

a[])
{
for

(int

i = 0;i<5;i++)
{
printf("value in array %d and address is %8u\n"

,a[i],&a[i]);
}
}
void

print_usingptr(int

a[])
{
int

*b;

User Avatar

Wiki User

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

Wiki User

10y ago

The following program demonstrates how to create both static and dynamic arrays of pointers. In both cases, the pointers in the arrays are allocated new memory (which means the individual elements must be released), however the pointers could just as easily point to existing allocations (which means the individual elements must not be released, since the memory would not actually belong to these pointers).

Note that the static array, a, has the same address and value. This is because a is a reference to the start address of array. References are not variables so they have no address of their own, they are simply aliases for the memory they refer to. Compare with b which is not a reference, it is a pointer, and therefore has its own separate address from the array it points to.

The two dereferenced values show the value of the first element in the array, since the first element always resides at the start of the array. The first dereference is the value of the pointer in that element, the second is the indirect value of that pointer.

Note that after we destroy the elements in the dynamic array, b, we must also destroy the array itself.

#include<iostream>

int main()

{

// declare static array of pointer to int

int* a[5];

// initialise array

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

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

// print values

std::cout<<"Name\tAddress\t\tValue\t\tDereference\tDereference"<<std::endl;

std::cout<<"a\t"<<&a<<"\t"<<a<<'\t'<<*a<<'\t'<<**a<<'\n'<<std::endl;

std::cout<<"Name\tAddress\t\tValue\t\tDereference"<<std::endl;

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

std::cout<<"a["<<i<<"]\t"<<&a[i]<<"\t"<<a[i]<<"\t"<<*a[i]<<std::endl;

std::cout<<std::endl;

// release pointers

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

delete( a[i] );

// declare dynamic array of pointer to int

int** b = new int*[5];

// initialise array

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

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

// print values

std::cout<<"Name\tAddress\t\tValue\t\tDereference\tDereference"<<std::endl;

std::cout<<"b\t"<<&b<<"\t"<<b<<'\t'<<*b<<'\t'<<**b<<'\n'<<std::endl;

std::cout<<"Name\tAddress\t\tValue\t\tDereference"<<std::endl;

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

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

std::cout<<std::endl;

// release pointers

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

delete( b[i] );

delete[]b;

}

Output:

Name Address Value Dereference Dereference

a 0021FF10 0021FF10 00230990 1

Name Address Value Dereference

a[0] 0021FF10 00230990 1

a[1] 0021FF14 002309C0 2

a[2] 0021FF18 002309F0 3

a[3] 0021FF1C 00230A20 4

a[4] 0021FF20 00230A50 5

Name Address Value Dereference Dereference

b 0021FEE0 00333870 00230A50 1

Name Address Value Dereference

b[0] 00333870 00230A50 1

b[1] 00333874 00230A20 2

b[2] 00333878 002309F0 3

b[3] 0033387C 002309C0 4

b[4] 00333880 00230990 5

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

char x[32], *px;

px= x;

...

from now on simply use 'px' instead of 'x', and that will work perfectly well

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

It is not exactly a question, but here is a little hint:

printf ("%d: %p\n", i, &array[i]);

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c plus plus program of array to pointers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is it possibly to return an array of strings in a function without using pointers in C plus plus?

No.


Would you Write c plus plus program using array for Fibonacci number?

You can write a C++ fib pro using arrays but the problem is the prog becomes very complicated since u need to pass the next adding value in an array.....


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.


Array implementation of priority queue example program in c plus plus?

yes


Can you help me with the C plus plus code of the program which has 10 index of array it adds 5 into every even elements of the array and then it subtracts 10 into the odd elements of the array?

int array[10] = {...}; for (int i = 0; i &lt; 10; ++i) { if (i % 2 == 0) array[i] += 5; else array[i] -= 10; }


Can you write a program to read a set of scores from a file and compute the average and print it on the screenby c plus plus?

Yes. Use cin and/or getline to read the formatted data into an array, compute the average then output the result using cout.


How calculate the average scored by student for is subject in c plus plus program?

Put all the values in an array, iterate through the array with a for loop, sum all the values, then divide by the count of the values.


How to write a code for finding sum of left diagonals elements in an array using c plus plus?

truzi i Ghal


What is the difference between c plus plus and java programming?

Java doesn't have pointers. C++ has pointers.


How do you write a program in c plus plus to check if an array is symmetric?

To determine if an array is symmetric, the array must be square. If so, check each element against its transpose. If all elements are equal, the array is symmetric.For a two-dimensional array (a matrix) of order n, the following code will determine if it is symmetric or not:templatebool symmetric(const std::array& matrix){for (size_t r=0 ; r


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


Program in c plus plus to delete an element in sorted array?

If the array is dynamic then use a vector instead of an array. You can then use the vector::erase() function to delete an element or a range of elements. Remember that if the vector contains pointers to unshared memory, then you must release the pointer before erasing the element containing that pointer. If the array is static then you cannot delete elements. The assumption with static arrays is that you will neither add nor delete, you will only modify existing elements. However, you can emulate a deletion by shunting elements to the left, and keeping track of how many used elements there are (which must always be less than or equal to the upper bound plus one). Again, if the array contains pointers to unshared memory, you must release the pointer before shunting elements. You can also do the same thing with dynamic C-style arrays, but once you've shunted elements to the left you can reallocate the array with the new size to physically delete the final element.