0

Starting to learn C++ and I am having trouble with an array pointer exercise. The array declarations in the header file look like

Car cars1_[numCars];
Car * cars2_;
Car * cars3_[numCars];
Car ** cars4_;

I want to initialize and set these pointers to arrays of size numCars but I need some insight on the syntax for it.

Additionally once I have those initialized, how can I call methods on the individual terms in the array?

for the first case I understand that I can do

Car cars1_[numCars]; //initialize the array
cars1_[0].fun(); //call a function on index 0

But what about the other cases?

i.e. how do initialize arrays and make these pointers point to them and call functions on the indices?

Car * cars2_;
Car * cars3_[numCars];
Car ** cars4_;
Michael Kemmerzell
  • 4,802
  • 4
  • 27
  • 43
Koala
  • 47
  • 4
  • Do you know anything about pointers at all? Pointers to array elements are exactly like other pointers. – molbdnilo Feb 07 '20 at 05:59
  • cars2_ = cars1_; cars2_[0].fun(); – idris Feb 07 '20 at 06:24
  • Maybe it's a language thing, but maybe it highlights some misunderstandings - the question title says "array to a pointer", while a pointer usually points "to an array". You can store the address of the first element of an array "in a pointer". Which makes me wonder about "call functions in the indices". Do you mean `cars_[0].fun()`, or `fun(cars_[0])`? One I would describe as "passing an element of an array to a function", and the other is "calling a function of the object at index zero of the cars_ array". – Steve Feb 07 '20 at 06:40
  • If this is homework, it's checking whether you understand these things. What is an array, what is a pointer, and what is a pointer to a pointer to a `Car`, and how these things are related. – Steve Feb 07 '20 at 06:43

2 Answers2

0

Try this,

#include<iostream>
#include<string>

using namespace std;

class car
{
 public:
      int i;
      fun()
      {
          cout<<"In car with value "<<i<<endl;
      }
};

int main()
{
    car cars1[2],cars11;
    car *cars2;
    car *cars3[2];
    car **cars4;

    cars1[0].i=0;
    cars1[1].i=1;
    cars11.i=2;

    cars3[0]=&cars1[0];
    cars3[1]=&cars1[1];
    cars2=&cars11;


    cars3[0]->fun();       //or   (*cars3[0]).fun();
    cars3[1]->fun();       //or   (*cars3[1]).fun();
    cars2->fun();          //or   (*cars2).fun();


    cars4=&cars2;

    (*cars4)->fun();       //or   (*(*cars4)).fun();

    return 0;
}

output:

In car with value 0
In car with value 1
In car with value 2
In car with value 2

Process returned 0 (0x0)   execution time : 0.232 s
Press any key to continue.
srilakshmikanthanp
  • 2,231
  • 1
  • 8
  • 25
0

Below snippet to usage of pointers,

Car  obj;
Car *p_Obj = &obj;
Car **pp_Obj = &p_obj;
  1. obj is simple object which stored the content.
  2. p_Obj is pointer and is pointing to memory location of obj
  3. pp_Obj is a pointer to a pointer to obj

In short,

enter image description here

Now coming to your case,

Car cars1_[numCars];
Car * cars2_;
Car * cars3_[numCars];
Car ** cars4_;
  1. cars1 is array of object. As you pointed out individual object can be accessed as cars1_[0].fun()

enter image description here

  1. cars2_ is a pointer to an object or array of object. assigned or updated as below,
Car cars1_;
Car * cars2_ = &cars1_;
cars2_ -> fun();
//    or
Car cars1_[numCars];
Car * cars2_ = cars1_;
cars2_[0].fun();

enter image description here

  1. cars3_ is a array of a pointers to object or array of objects
Car cars11_{10};
Car cars12_[4]{20,30,40};
Car * cars2_[4] = { &cars11_,&cars12_[0] };

cars2_[0]->fun(); //access cars11_
(cars2_[1]+0)->fun(); //access cars12_[0]
(cars2_[1]+1)->fun(); //access cars12_[1]
(cars2_[1]+2)->fun(); //access cars12_[2]

//or

cars2_[1][0].fun(); //access cars12_[0]
cars2_[1][1].fun(); //access cars12_[1]
cars2_[1][2].fun(); //access cars12_[2]

enter image description here

  1. cars4_ is a pointer to a pointer to a object or pointer to a array of pointers to a object or pointer to a pointer to a array of object
Car cars11_[4]{10,20,30};
Car *cars12_= &cars11_[0];
Car **cars4_ = &cars12_;

(*cars4_)[0].fun();  //access cars11_[0]
(*cars4_)[1].fun();  //access cars11_[1]
(*cars4_)[2].fun();  //access cars11_[2]

//or

cars4_[0][0].fun();  //access cars11_[0]
cars4_[0][1].fun();  //access cars11_[1]
cars4_[0][2].fun();  //access cars11_[1]

enter image description here

TruthSeeker
  • 1,539
  • 11
  • 24