#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;
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
char x[32], *px;
px= x;
...
from now on simply use 'px' instead of 'x', and that will work perfectly well
It is not exactly a question, but here is a little hint:
printf ("%d: %p\n", i, &array[i]);
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.....
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.
yes
int array[10] = {...}; for (int i = 0; i < 10; ++i) { if (i % 2 == 0) array[i] += 5; else array[i] -= 10; }
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
No.
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.....
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.
yes
int array[10] = {...}; for (int i = 0; i < 10; ++i) { if (i % 2 == 0) array[i] += 5; else array[i] -= 10; }
Yes. Use cin and/or getline to read the formatted data into an array, compute the average then output the result using cout.
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.
truzi i Ghal
Java doesn't have pointers. C++ has pointers.
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
how to write a program that counts automorphic number from 1 to 999
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.