How do I dereference a pointer to an array

Pages: 12
closed account (48T7M4Gy)
Don't worry about it Ch1156.

IMHO the Array Police go on patrol here occasionally and Kevvy has an ego nearly as big as his arrogance here and on another thread I've encountered with him.

You enjoy arrays, vectors whatever you like. C++ is for everybody who takes an interest.

I hope your OP problem is solved to your satisfaction despite the static. Just post any more questions if not :)
Last edited on
> Even the god of C++, the man himself, devotes substantial time in the introductions to arrays and then later
> goes on to talk about the limitation and pitfalls along with ...

Unadulterated rubbish.

Programming: Principles and Practice using C++ (Second Edition) - Bjarne Stroustrup

...

4.6 Vector (page 225)
...
We'll get to see a variety of ways of storing collections of data ...
Here, we'll start with one of the simplest, and arguably the most useful, of storing data: a vector.

...

17.3 Memory, addresses and pointers (page 960)

A computer's memory is a sequence of bytes. We can number the bytes from 0 to the last one.
We call such "a number that indicates a location in memory" an address. ...
An object that holds an address value is called a pointer. ...

...

18.6 Arrays (page 1058)
...
Use std::vector where you have a choice - and you have a choice in most contexts. However, arrays existed long before vectors and are roughly equivalent to what is offered in other languages (notably C), so you need to know arrays and know them well, to be able to cope with older code and with code written by people who don't appreciate the advantages of vector.

So, what is an array? How do we define an array? How do we use an array? An array is ...
...

Ok, well yes my original question was never really answered. I output the numbers and all i get is addresses, which is expected, but I cant seem to output the actual array. Also, am I creating a pointer to the entire array or to each element in the array or to the first element only?

Also Oddly enough one of the addresses is just 0x8 whereas most of the other addresses are longer, and one is just 0, why is that? is that normal?

Here is my code again:

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

void Pointer(int* arr[]);

int main()
{
    int* arr[20];

    Pointer(arr);

	return 0;
}

void Pointer(int* arr[])
{
    for(int i = 0; i < 20; i++)
    {
        std::cout << arr[i] << std::endl;
    }
}
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
#include <iostream>

void print_1( const int* array, std::size_t array_size ) ; // declaration (pass pointer to first element)
void print_1( const int array[], std::size_t array_size ) // definition of above
{
    for( std::size_t i = 0 ; i < array_size ; ++i ) std::cout << array[i] << ' ';
    std::cout << '\n' ;
}

const std::size_t ARRAY_SIZE = 20 ;
using array_type = int[ARRAY_SIZE] ; // array of ARRAYSIZE int

void print_2( const array_type& array ) // pass reference to array
{
    for( int value : array ) std::cout << value << ' ';
    std::cout << '\n' ;
}

int main()
{
    int arr[ARRAY_SIZE] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    print_1( arr, ARRAY_SIZE ) ;

    print_2(arr) ;
}

http://coliru.stacked-crooked.com/a/93612058784bbe42
closed account (E0p9LyTq)
Kevin C wrote:
I'm also wondering whether, if you were teaching C++, would you decide to go arrays-first or vectors-first?


If I were to ever teach someone C++ I'd teach the C++ containers first, period. Including the new Array template class.

I'd much later teach the dangers of C-style arrays and why they should be avoided.
closed account (48T7M4Gy)
Trust Borges to come up with the most long winded batch of unintelligible code. Nobody bothers reading it after the first line. Always hours after the fact and never making a worthwhile contribution. Almost 7100 examples of how to waste a keyboard.


lol I was trying to figure out what was happening in the code and why but idk. My line of thinking is that the easier the better. I mean that is the whole point of programming right? to write code that works well with it being as simple as possible? I cant say I understand whats happening in his code, but what I do know is that there is an easier way, there always is. thats the beauty of programming, there is usually always a lot of different ways to approach 1 problem.
Last edited on
closed account (48T7M4Gy)
The problem you raised in your OP is interesting and I will have a play around with it. The problem is the abnormal way of declaring arr. If you initialize it with = {0} then you get 20 zeroes, but if you initialise it with a set of values it crashes which is why it's tricky.

I think MiiniiPaa can crunch this problem very easily.

Stay with it and meanwhile #%¥! the Array police.
Well, im a little rusty with pointers. This whole thing is mainly a pointer excercize. I know how to pass arrays and vectors to functions and such, but pointers are hard to figure out.
You have to initialize the pointers, and dereference the pointers in the Pointer function

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
28
29
30
31
#include <iostream>
#include <string>

void Pointer(int* arr[]);

int main()
{
    int* arr[20];

    //Initialize the pointer
    for(int i = 0; i < 20; ++i)
        arr[i] = new int(i);

    Pointer(arr);


    //Delete the pointers
    for(int i = 0; i < 20; ++i)
        delete arr[i];

	return 0;
}

void Pointer(int* arr[])
{
    for(int i = 0; i < 20; i++)
    {
        //Dereference the pointer
        std::cout << *arr[i] << std::endl;
    }
}


[Edit] added a for loop to delete the pointers
Last edited on
closed account (48T7M4Gy)
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>

void Pointer(int* arr[]);

int main()
{
    int* arr[5];
    
    int brr[] = { 5, 17, 98, 20, 32};

    //Initialize the pointer
    for(int i = 0; i < 5; ++i)
        arr[i] = &brr[i];

    Pointer(arr);

	return 0;
}

void Pointer(int* arr[])
{
    for(int i = 0; i < 5; i++)
    {
        //Dereference the pointer
        std::cout << *arr[i] << std::endl;
    }
}


This is another permutation of what Yanson so ably showed. ( I picked 5 for ease of typing )
Last edited on
awesome, i knew there were simple solutions to this problem, thank you.
closed account (48T7M4Gy)
woo hoo we got there.
Cheers
woo hoo we got there.


Not exactly. There are no pointers to arrays in the code tendered. On the other hand, you have demonstrated how to dereference a pointer that is an element of an array and a pointer to an element of an array.
closed account (48T7M4Gy)
Another policeman crawls out of the woodwork. Do you ever do anything but stroke your own dismal ego cire. Try contributing positively for a change instead of being a C++ wanker.
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
28
29
30
#include <iostream>
#include <algorithm>

int main()
{
    constexpr std::size_t N = 6 ;
    using array_type = int[N] ; // type: 'array of N int'

    array_type array { 23, 67, 54, 89, 12, 99 } ; // array of N int
    std::cout << array[0] << '\n' ;

    array_type& ref_array = array ; // reference to array of N int
    std::cout << ref_array[1] << '\n' ;

    array_type* ptr_array = &array ; // pointer to array of N int
    std::cout << (*ptr_array)[2] << '\n' ;


    using array_of_pointers_t = int*[N] ; // type: 'array of N pointer to int'

    array_of_pointers_t array_of_ptrs {} ; // array of N pointer to int
    std::transform( array, array+N, array_of_ptrs, std::addressof<int> ) ;
    std::cout << "address: " << array_of_ptrs[3] << "  value at that  address: " << *array_of_ptrs[3] << '\n' ;

    array_of_pointers_t& ref_array_of_ptrs = array_of_ptrs ; // reference to array of N pointer to int
    std::cout << "address: " << ref_array_of_ptrs[4] << "  value at that  address: " << *ref_array_of_ptrs[4] << '\n' ;

    array_of_pointers_t* ptr_array_of_ptrs = &array_of_ptrs ; // pointer to array of N pointer to int
    std::cout << "address: " << (*ptr_array_of_ptrs)[5] << "  value at that  address: " << *(*ptr_array_of_ptrs)[5] << '\n' ;
}

http://coliru.stacked-crooked.com/a/3a902e4b75e13bde
I think this is what the OP wanted:

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

void Pointer(int (*arrP)[20])
{
    for(int i = 0; i < 20; i++)
    {
        std::cout << (*arrP)[i] << std::endl;
    }
}

int main()
{
    int arr[20] = {9,8,7,6,5,4,3,2,1,0,9,8,7,6,5};
    int (*arrP)[20] = &arr;

    Pointer(arrP);

	return 0;
}


Edit: the parenthesis in type declaration are needed because the operator [] has higher priority than the operator *.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

void Pointer(int *arrP)
{
    for( int i = 0; i < 20; i++ )
        std::cout << *arrP++ << std::endl;
}

int main()
{
    int arr[20] = { 9, 8, 7,  6, 5, 4,  3, 2, 1,  0, 9, 8,  7, 6, 5 };
    int *arrP = &arr[0];

    Pointer( arrP );

	return 0;
}
Topic archived. No new replies allowed.
Pages: 12