-5

I want to create an array of pointers, and each pointer should keep memory addresses of all elements from another array. But something is goind wrong.

Here is the code:

int main()
{
    int a[] = { 1, 2, 3, 4, 5 };
    int *b = new int[sizeof(a)];

    for (int i = 0; i < sizeof(a); i++)
    {
        b = &a[i];
    }

    std::cout << &b[3] << std::endl;
    std::cout << &a[3] << std::endl;

    return 0;
}

When I expect is similar memory addresses, and values. Bt everything is different.

Demaunt
  • 1,183
  • 2
  • 16
  • 26
  • 3
    You don't have an array of pointers. – juanchopanza Nov 10 '17 at 16:47
  • 2
    You have 2 arrays of `int`s, both with other memory addresses. You are just setting their values to the addresses of `a`. – Hatted Rooster Nov 10 '17 at 16:47
  • 3
    This doesn't address the problem, but `sizeof(a)` gives the number of **bytes** in the array, which, for an array of `int`, is larger than the number of elements. So `new int[sizeof(a)]` allocates a larger array than you need. If you insist on hand-allocating arrays (instead of using `std::vector`), the number of elements in an array is `sizeof(a)/sizeof(*a)`. – Pete Becker Nov 10 '17 at 16:48
  • 1
    &a[3] = b[3]; content of pointer is address – user6386155 Nov 10 '17 at 16:48
  • 1
    duplicate of [How do I create an array of pointers?](https://stackoverflow.com/questions/620843/how-do-i-create-an-array-of-pointers) – underscore_d Nov 10 '17 at 16:50
  • new is totally unnecessary, sizeof performs a compile time operation – George Nov 10 '17 at 16:50
  • The subscript operator is just syntactic sugar. Meaning: `&a[i] => &*(a + i) => a + i` – scohe001 Nov 10 '17 at 16:51
  • the obvious giveaway here is that each time round that loop you overwrite `b`. So your `new` does nothing (except leak) sice you immediately throw away its return value.. And you only have one `b` value, the last one. You are missing an extra level of indirection – pm100 Nov 10 '17 at 16:54

2 Answers2

0

sizeof(a) returns the size IN BYTES of a.

To get the number of elements you should divide that for the sizeof(int).

So if you have 5 elements, for example, you'll have sizeof(a) equal to 5*sizeof(int). Divide that for sizeof(int) (which could vary depending on the machine you're working on) and you'll have the exact number of elements.

And I thing you wanna assign b[i] = &a[i], in order to store the addresses in the different elements of b.

magicleon94
  • 4,887
  • 2
  • 24
  • 53
  • 1) Thank you for help with size of arrays; 2) Still I dont understand how to write address of a-elements into pointers (b-elements); – Demaunt Nov 10 '17 at 17:05
0

I think you wanted to do this

int main(int argc, char *argv[])
{

    int a[] = { 1, 2, 3, 4, 5 };
    unsigned max=sizeof(a) / sizeof(a[0]);
    int **b = new int*[max];

    for (int i = 0; i < max; i++)
    {
        b[i] = &a[i];
    }

    unsigned max=sizeof(a) / sizeof(a[0]);
    for (int i = 0; i < max; i++)
    {
         std::cout << "b[" << i << "]:" << *b[i] << std::endl;
         std::cout << "a[" << i << "]:" << a[i] << std::endl;
    }
    delete[] b;

    return 0;
} 

Two mistakes. First, when you declare array of pointers you use

int **b;

Secondly, to determine size of array you can use

sizeof(a) / sizeof(a[0])

but be careful here. After passing your array as argument into function it won't work any longer.

Victor
  • 461
  • 1
  • 5
  • 14
  • 1
    Since it's all in your main it will be freed anyway. But in general you will need to call delete where you used new previously. If you used new[] use delete[]. As in here delete[] b; (corrected answer) – Victor Nov 10 '17 at 17:16
  • 1
    @Victor Don't you think `for (int i = 0; i < sizeof(a); i++)` should be `for (int i = 0; i < (sizeof(a) / sizeof(a[0])); i++)`? – Killzone Kid Nov 10 '17 at 18:33