6

I want to do something like this below:

int main() {
   int a[10];
   int *d = generateArrayOfSize(10) // This generates an array of size 10 on the heap
   a = d;
   print(a); // Prints the first 10 elements of array.
}

However above code gives compilation error (incompatible types in assignment of ‘int*’ to ‘int [10]’). What can I do to make the above code to work?

feuGene
  • 3,931
  • 2
  • 33
  • 46
Bourne
  • 1,905
  • 13
  • 35
  • 53
  • 3
    As the designer of C++, Bjarne Stroustrup, [says](http://www.stroustrup.com/bs_faq2.html#arrays), an array is "a very low level data structure with a vast potential for misuse and errors and in essentially all cases there are better alternatives". Try using an STL container. – Tom Blodget Jul 28 '13 at 15:04

8 Answers8

4

Arrays are non-assignable and non-copyable, so you'd have to copy each element by hand (in a loop), or using std::copy.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
3

Arrays are not pointers.

You can't do :

int a[10];
int *d;
a = d;

Change it to :

int *a;
int *d;
a = d;

Main differences between arrays and pointers in C programming :

Pointer                                    | Array
-------------------------------------------|-------------------------------------------
A pointer is a place in memory that keeps  | An array is a single, pre allocated chunk
address of another place inside            | of contiguous elements (all of the same
                                           | type), fixed in size and location.
-------------------------------------------|-------------------------------------------
A pointer can point to a dynamically       | They are static in nature. Once memory is 
allocated memory. In this case, the memory | allocated , it cannot be resized or freed
allocation can be resized or freed later.  | dynamically.
-------------------------------------------|-------------------------------------------

You have a quite good explanation here : https://stackoverflow.com/a/7725410/1394283

Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
  • 2
    I actually find the list of differences more confusing than helpful. *Pointer can’t be initialized at definition.* => of course they can: `int a = 3; int* b = &a;`, *The memory allocation can be resized or freed later* => not a characteristic of the pointers, but of *some* pointed-to memory. *The assembly code of Pointer is different than Array* => this does not mean anything to a beginner to start with. – Matthieu M. Jul 28 '13 at 13:14
  • Agree. `Pointer can’t be initialized at definition.` Downvoting for that alone. – 7stud Jul 28 '13 at 13:15
  • @MatthieuM. I removed the parts who were not clear. But I meant that the memory pointed-to by the pointer cannot be initialized during the pointer definition... – Pierre Fourgeaud Jul 28 '13 at 13:21
  • The second point raised by @MatthieuM. still applies. In fact, a pointer could point to an array, which, as you say, cannot be resized or freed. – Tom Blodget Jul 28 '13 at 14:59
  • @TomBlodget I tried to make it more clear... It's quite hard to explain this idea in few lines. But thanks to point out that it is not exact. – Pierre Fourgeaud Jul 28 '13 at 15:06
3

If you're using C++, then use C++ arrays rather than C style arrays and pointers. Here's an example

#include <array>
#include <iostream>

template<size_t N>
std::array<int, N> generateArrayOfSize(void)
{
    std::array<int, N> a;
    for (int n=0; n<N; ++n)
        a[n] = n;
    return a;
}

template<size_t N>
void print(std::array<int, N> const &a)
{
    for (auto num : a)
        std::cout << num << " ";
}

int main() {
   std::array<int, 10> a;
   std::array<int, 10> d = generateArrayOfSize<10>();
   a = d;
   print(a); // Prints the first 10 elements of array.
}

which outputs 0 1 2 3 4 5 6 7 8 9

cdmh
  • 3,294
  • 2
  • 26
  • 41
2

An array is not a pointer (although a name of an array often decays to a pointer to its first element).

To make the above code to work, you can declare a as a pointer: int *a;. The print function takes an int* (or a decayed array) anyway.

If you really want to have two arrays and copy contents from one array to another, you should copy the data in a loop.

nullptr
  • 11,008
  • 1
  • 23
  • 18
1

This will print in this way when you assign a string reference to a pointer you have to use *ptr to print the value of a pointer otherwise in your case print(d) that is like cout< in c++ it will only print the location of the d[0].

 int ary[5]={1,2,3,4,5};
 int *d;
 d=ary;
 for(int i=0;i<5;i++)
 cout<<*(d+i);
Root
  • 955
  • 1
  • 16
  • 39
0

Because array names are non-modifiable. So you can't do

a = d;

Declare it as a pointer like this:

int *a;
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
0

Little rusty with my C++ but try something like this.

int main() {
   int *a;
   int *d = generateArrayOfSize(10) // This generates an array of size 10 on the heap
   a = d;
   print(a); // Prints the first 10 elements of array.
}
Ryan Williams
  • 959
  • 7
  • 15
0

In C, it was always true when Thing X[10]; was declared, X was the constant address of the first element(i.e. &X[0]). So you could then say:

Thing *Y = X; // Equivalent to (Thing *Y = &X[0];)

But in C++, the compiler "remembers" that the Thing array X has 10 elements, and some C++ imposed type checking rules break. Imagine we add Thing Z[20]; to the discussion.

Thing *Y = X; and Thing *Y = Z; if both allowed, would imply that a single variable could be set to Thing Arrays of length 10 and 20, which are very different (ahem) "things", as a quick look at a 2D array will reveal. This sort of justifies why the C language assumed equivalent of X and &X[0] is broken in C++.

Well, at least for some versions of C++. So best not to assume it, and use Thing *Y = &x[0]; and Thing *Y = &Z[0] instead;

This approach has two advantages. It does what is wanted, and it actually compiles. :-)

NvRijn
  • 1