0
#include <iostream>
using namespace std;

class idk{
    public:
        int x;
        int y;
};

void obj(idk* obj[]){
    obj[0]-> x = 1000;
    obj[0]-> y = 30;
    
}

int main(){

    idk *z[5];
    obj(z);
    cout << z[0]->x;
    
    return 0;
}

I am just trying out how to use pointers. The problem is when I set my array 'z' size to 5 or any number it doesn't do anything, however when I make it 10 it then prints out the correct output. Ive tried pasting the code into an online compiler and it also plays up there but with other numbers. Is my code wrong or missing some things?

  • 2
    You have undefined behaviour; you don't actually initialize the pointers. First, forget using the array, and just use a pointer. Learn from the start. – ChrisMM Oct 19 '22 at 11:58
  • I think `idk* obj[]` is redundant. Either use `idk* obj` or `idk obj[]`. You wrote a pointer to a pointer. Aside from this: Don't use raw pointers. This is bad outdated c++. Use references or if you must smart pointers (here references will do) – Stein Oct 19 '22 at 12:00
  • lesson 0 of pointers: Pointer point somewhere. If you want an object of type `idk` then you need to create one. Merely declaring a pointer is not sufficient – 463035818_is_not_an_ai Oct 19 '22 at 12:00
  • 3
    @Stein its not redundant. Its an array of pointers and decays to `idk**` – 463035818_is_not_an_ai Oct 19 '22 at 12:01
  • Forget that C-style arrays exist in the language and don't ever use them. Use `std::array` and/or `std::vector` instead, always. – Jesper Juhl Oct 19 '22 at 12:37
  • 1
    [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Jesper Juhl Oct 19 '22 at 12:39
  • https://en.cppreference.com/w/cpp/language/ub – Jesper Juhl Oct 19 '22 at 12:40

1 Answers1

2

In this

idk *z[5];

you declare 5 idk pointers. These are only pointers that you can assign to point at idks, but you have not created any actual idks. When you later dereference the first pointer you get undefined behavior since it's not actually pointing at an idk:

void obj(idk* obj[]){
    obj[0]-> x = 1000; // BOOM

Making the array of pointers actually point at idk instances can be made in many different ways. Here's one:

#include <iostream>

class idk{
public:
    int x;
    int y;
};

void obj(idk* obj[]){
    obj[0]-> x = 1000;
    obj[0]-> y = 30;
    
}

int main(){
    idk instances[5];

    idk *z[5]{
        &instances[0],
        &instances[1],
        &instances[2],
        &instances[3],
        &instances[4],
    };                 // now all five point at one idk instance each
    obj(z);
    std::cout << z[0]->x;    
}

Another option would be to skip the pointer array completely:

#include <iostream>

class idk {
public:
    int x;
    int y;
};

void obj(idk obj[]) {
    obj[0].x = 1000;
    obj[0].y = 30;
}

int main() {
    idk z[5];
    obj(z);
    std::cout << z[0].x;
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108