0

I am trying to access an element of an array through the array of pointers. First, I declare my main array with chars and the array of pointers, which will also contain names of other similar arrays:

char listSubjects[3][15] = {
    "sports", "movies", "fashion"
};
char* listPointers[1] = {
    listSubjects[3]
};

Now I am trying to print that "sports" from the first array (listNum here is zero):

bub1->writeStr(listPointers[listNum][0]);

I am getting this error:

invalid conversion from 'char' to 'char*'

Getting stuck. I am fairly new to pointers and don't quite understand what I am doing wrong

I also tried using star when I am trying to access an element. It compiled, but I didn't see any text on a screen. Not sure what value it even tries to output, in my program I just see a blank string

bub1->writeStr(*listPointers[listNum][0]);
t.niese
  • 32,069
  • 7
  • 56
  • 86
  • You probably want `bub1->writeStr(listPointers[listNum]);`. Anyways when dealing with strings in c++ you should use `std::string`, if you need a `const char*` pointer to interact with any API requiring it, you can always use the `std::string::c_str()` function or referring to `std::string::data()` address, if you need a non `const` access. – πάντα ῥεῖ Aug 03 '19 at 08:28
  • @David https://stackoverflow.com/questions/1461432/what-is-array-decaying – πάντα ῥεῖ Aug 03 '19 at 08:31
  • 1
    Yes, thank you, I was looking for a good duplicate. – David C. Rankin Aug 03 '19 at 08:33
  • 1
    @David I probably should have dupe hammered this in first place. Now it's going its way down ;-) – πάντα ῥεῖ Aug 03 '19 at 08:37
  • _@NikitaNikita_ As a general advice, stop using raw c-style pointers or arrays when programming in c++. There are way better features provided by the c++ standard library. – πάντα ῥεῖ Aug 03 '19 at 08:42

2 Answers2

1

So this is an error

char* listPointers[1] = {
    listSubjects[3]
};

because the size of the listSubjects array is three, so listSubjects[3] is an out of bounds array access. It's quite a common beginner mistake to use the size of of the array to try and refer to the whole array, and I think that's what you are trying here.

If you want a pointer to each item in the listSubjects array then you have to list them out individually

char* listPointers[3] = {
    listSubjects[0],
    listSubjects[1],
    listSubjects[2]
};

Then your code is simply

bub1->writeStr(listPointers[listNum]);

I think this is a case where unfamiliarity with the concepts resulting in you trying to write code that is more complicated than it needs to be.

But note, as has been said already, you're not really learning C++ here. This code is pure C. In C++ we do this kind of stuff with the much simpler std::string and std::vector.

john
  • 71,156
  • 4
  • 49
  • 68
  • Well, it was a solution before, but there will be more arrays like `listSubjects[]`. My `listPointers` later will look somewhat like this: ``` char* listPointers[3] = { listSubjects[], listInteractions[], listSomethingElse[] }; ``` Contents of these three arrays may varry. Depends on value of `listNum` my program will print some word out from one of these arrays. – Nikita Nikita Aug 03 '19 at 08:36
  • 1
    @NikitaNikita Then you are going to have to find some other solution. In C++ arrays cannot have a variable size. You should use `std::vector` (and `std::string`) instead. – john Aug 03 '19 at 08:38
  • @NikitaNikita Have a thorough look at the proposed duplicate, this will give you some insight what's going on. – πάντα ῥεῖ Aug 03 '19 at 08:38
  • Ok. I will learn vectors then. Thank you for your reply – Nikita Nikita Aug 03 '19 at 08:39
  • Don't just learn vectors, learn both or you will be lost when working on a majority of C++ code written in the last 20 years. `char (*listPointers)[15] = listSubjects;` would allow you to access each character string in the exact same manner without having to know how many strings there were at compile time, only the length of each row-array is required. – David C. Rankin Aug 03 '19 at 08:48
0

listPointers[][] results in a char, where your function expects a char*. What you want to pass is listPointers[listNum], without the second [].

All that said, quit that stuff ASAP and use std::string and std::vector.

Michael Chourdakis
  • 8,819
  • 2
  • 32
  • 61