Array Pointer

I'm trying to change the value of an Int array based on the value input by
the user. i.e. if 3 is entered, Then element 3 (Iarr[2]) will have it's value changed to 1 (flag).
After several attempt's i'm stuck.

Also, I used a pointer to an array to input number's (int *point = iarr). When i tried to change it to it's initial value (*point = iarr) i had an invalid conversion. Would i have to use another pointer for the read?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <array>

int main()
{
    int iarr [4];
    int *point = iarr;
    int input = 0;
    for (int i = 0; i <= 4; ++i)
    {
         std::cout << "\nEnter a number: ";
         std::cin >> input;
         // If input = 3, Enter 1 into iarr[2] using a pointer.
    }
*point = iarr;
    for (int i = 0; i <= 4; ++i)
    {
        std::cout << iarr[i] << " ";
    }
}
In your case. *point = iarr is invalid conversion becaues. *point should hold a value and iarr will be having address of the first number in array.

- (point=iarr) will be correct

Thank's for your reply vintho.

Finally got it.

Is there a way to set every element in an array to the same number when initialising it, As there is in a vector.
Last edited on
There are three ways to intialize array to default value

- Using fill function std::fill(starting address,last address,value)
ex: int a[5];
std::fill(a,a+5,34)

-int b[5]={0};
But this will work oly when you want to set zero

-for loop(Normal way)
for(int i=0;i<5;i++)
{
b[i]=0;
}
Thank's vintho.
You included header <array> but do not use std::array in the program. So it is not clear why you included <array>. Also you are trying to access elements of the array that do not exist that is that are beyond the upper bound of the array.

If I have correctly understood whar you want then the code can look the following way (without testing)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

int main()
{
    const size_t N = 4;    
    int iarr [N] = {};
    int *point;;

    point = iarr;
    for ( size_t i = 0; i < N; ++i )
    {
         std::cout << "\nEnter a number in the range [0," << N << "): ";
         int input = 0;
         std::cin >> input;

         if ( 0 <= input && input < N ) *( iarr + input ) = 1;
    }

    point = iarr;
    for ( size_t i = 0; i < N; ++i )
    {
        std::cout << iarr[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}
Last edited on
The array was defined ok vlad from moscow. I used sizeof() which returned 4*array size.
I was only using the program to learn pointer's. In the finished program, Number's entered set a flag in the appropriate element, Then a for loop printed the number's in numerical order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <array>

int main()
{
    int iarr [100] = {0};
    int *point;
    point = iarr;
    int input = 0;
    for (int i = 0; i <= 4; ++i)
    {
        iarr[i] = 0;
    }
    for (int i = 0; i <= 4; ++i)
    {
         std::cout << "\nEnter a number: ";
         std::cin >> input;
         point[input -1] = 1;
    }
    for (int i = 0; i <= 100; ++i)
    {
        if (point[i] == 1)
            std::cout << i+1 << " ";
    }
}
Topic archived. No new replies allowed.