vinknee
vinknee

Reputation: 147

Assign a pointer to an array

I am trying to create an array that generates random values, then assign a pointer to that array in order to use it in other functions.

Question 1: Is this the right approach?

Question 2: When I run the code below, my pointer function generates values inconsistent with what the actual array's value is. What am I doing wrong?

int size = 100;
int theray[size];
for(int i=0; i<size; i++)
{
    theray[i] = (rand()%100);
}
//Output array
cout<<"The array: ";
for(int j=0; j<size; j++)
{
    cout<<theray[j]<<" ";
}
cout<<endl;

int (*parray)[100] = &theray;
cout<<"The array pointer: ";
for(int k=0; k<size; k++)
{
    cout<<*parray[k]<<" ";
}

Upvotes: 0

Views: 1459

Answers (4)

MvG
MvG

Reputation: 60868

Question 1

Is this the right approach?

Usually not. It depends on what you are trying to achieve.

For high level semantics you'd in most cases use std::vector<int> or, if the size is fixed and you are using C++11, std::array<int, size>. If you actually have to go down to the pointer level, you'd usually write it like this:

int *parray = theray;
cout<<"The array pointer: ";
for(int k=0; k<size; k++)
{
    cout<<parray[k]<<" ";
}

This works because arrays will degrade to pointers, and the […] subscripts work on these pointers just like they work on the original arrays.

Question 2

When I run the code below, my pointer function generates values inconsistent with what the actual array's value is, what am I doing wrong?

*parray[k] gets interpreted as *(parray[k]) while you intend to use it as (*parray)[k].

Upvotes: 2

juanchopanza
juanchopanza

Reputation: 227418

Question 1: is this the right approach?

No. The right approach is to use std::vector<int> if size is not known at compile time1, and std::array<int, size> if it is2. There is no need for pointers here.

void foo(const std::vector<int>& v)
{
  // do stuff with v
}

...

std::vector<int> v(size); // vector with size elements
// do something with v

// pass v to a function
foo(v);

Question 2: when I run the code below, my pointer function generates values inconsistent with what the actual array's value is. What am I doing wrong?

If you use C++ idioms you won't even encounter this problem, so I consider the question moot. However, in your case you have a problem of operator precedence: be explicit about applying de-reference * before access []:

cout<< (*parray)[k] << " ";

1 As shown in the example, you can use an std::vector as a fixed size array, where the size need not be known at runtime. Just bear in mind that it is possible to change it's size after construction.

2In your example, size is not a compile time constant so you cannot use std::array. However, if you had declared it as const int size = 100; then it would be considered a compile time constant.

Upvotes: 3

Andy Prowl
Andy Prowl

Reputation: 126432

Question 1: is this the right approach?

No. Use std::vector<> for arrays whose size can change dynamically (at run-time). Prefer avoiding pointers and manual memory management.

Question 2: when I run the code below, my pointer function generates values inconsistent with what the actual array's value is. What am I doing wrong?

First of all, the fact of creating pointers so you can pass the array to a function. This is not necessary. Here is how I would use classes from the C++ Standard Library to write that program (in C++11):

#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>

// Sample function that prints the vectors's content
void foo(std::vector<int> const& v)
{
    copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, " "));
}

int main()
{
    // Populate the vector...
    size_t sz = 10;
    std::vector<int> v(sz);
    generate(begin(v), end(v), [] () { return rand() % 100; });

    // Pass it to a function...
    foo(v);
}

Upvotes: 1

2to1mux
2to1mux

Reputation: 783

Your code is a bit off in three ways. First, there is no need to use &theray. Array names already reference a memory address. You can simply assign the pointer to theray. Second, you're declaring an array of 100 pointers. Based on your description, it sounds like you just want one pointer that points to the array. Your declaration should just be int *parray instead of int *parray [100]. Finally, once you have a pointer to the array, you can access elements of the array the same way you would with the original array, only with the name of the pointer, instead of the name of the array. Try changing your last block of code (starting with the pointer declaration to this:

int *parray;
parray = theray;
cout<<"The array pointer: ";
for(int k=0; k<size; k++)
{
    cout<<parray[k]<<" ";
}

Upvotes: 2

Related Questions