-1

I'm having problems with a program that only accepts arrays. I'm having plenty of pointers to different arrays, but using *p seems to only give me the first element of the array. I want to return all the elements of the array. I know the length of the array, if that helps.

#include <typeinfo>

#include <iostream>

int i[10];
int* k=i;

cout<<typeid(i).name()<<'\n';

cout<<typeid(*k).name()<<'\n';

results in 'int [10]' and 'int' respectively. I want some way of returning k as 'int [10]'.

Marek R
  • 32,568
  • 6
  • 55
  • 140
nonsqu
  • 25
  • 2
  • If you want to stick with raw arrays, you need to return the size too, not just the pointer to the first element. Otherwise, use `std::array` which already does all the work for you. – Fareanor Feb 02 '23 at 15:42
  • Don't overuse pointers in C++. I think you should really learn about std::vector/std::array and range based for loops. – Pepijn Kramer Feb 02 '23 at 15:43
  • Anyway it sounds like you are learning C++ from an outdated source, look at [cppreference](https://en.cppreference.com/w/) for examples. Get a [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). When you've mastered the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date. – Pepijn Kramer Feb 02 '23 at 15:44
  • 5
    C functions operating on arrays normally take both a pointer and a size parameter, because a pointer has no information about the size. In C++ there are better containers than a C array. – interjay Feb 02 '23 at 15:44
  • 3
    C arrays do not have RTTI (runtime type information). Only classes with virtual member functions have this feature. So `k` do not have information to what exactly it points to. Logic of your application should maintain that information. – Marek R Feb 02 '23 at 15:50
  • 5
    There is no difference between "pointer to a solitary `int`" and "pointer to an `int` that happens to be the first element of an array". (Note that `int* k=i;` is equivalent to `int* k=&i[0];` - `k` is not a pointer to an array but to an `int`.) – molbdnilo Feb 02 '23 at 15:51
  • Somewhat analogously: you can't tell from someone's GPS coordinates whether they're first in line for the latest sneakers and how long that line is, or they're just standing there twittering with their fancy pocket telephone. – molbdnilo Feb 02 '23 at 16:00
  • The `typeid` of something is likely not actually useful for whatever you're trying to do. It would be better to show the actual stuff you're trying to do, such as what function you want to pass this data to. There's a very good chance that modern things like `std::array` or `std::vector` will be an appropriate solution. – TheUndeadFish Feb 02 '23 at 16:01
  • 2
    _@nonsqu_ _"I want some way of returning k as 'int [10]'."_ That's not possible since pointers aren't arrays and vice versa. – πάντα ῥεῖ Feb 02 '23 at 16:34
  • 2
    C++ can return a `std::array`, but it can not return a C style array. Neither can C. C style arrays have enough tricky rules that it's arguably best to avoid them in C++. – Drew Dormann Feb 02 '23 at 17:11

2 Answers2

4

Your k is a pointer to int. It points to the first element of the array. If you want a pointer to the whole array then you need to declare it as such.

#include <typeinfo>    
#include <iostream>
int main() {
    int i[10];
    int* k=i;
    int(*p)[10] = &i;


    std::cout<<typeid(i).name()<<'\n';
    std::cout<<typeid(*k).name()<<'\n';
    std::cout<<typeid(*p).name()<<'\n';
}

Output:

A10_i
i
A10_i

However, as others have said, std::array is much less confusing to work with. It can do (almost) anything a c-array can do without its quirks.

Certainly there is a solution to your actual problem that does not require to get the array from a pointer to a single integer.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
2

Example to show you how much more convenient C++ array/vector is then "C" style arrays with pointers :

#include <vector>
#include <iostream>

// with std::vector you can return arrays
// without having to think about pointers and/or new
// and your called cannot forget to call delete
std::vector<int> make_array()
{
    std::vector<int> values{ 1,2,3,4,5,6 };
    return values;
}

// pass by reference if you want to modify values in a function
void add_value(std::vector<int>& values, int value)
{
    values.push_back(value);
}

// pass by const refence if you only need to use the values
// and the array content should not be modified.
void print(const std::vector<int>& values)
{
    // use range based for loops if you can they will not go out of bounds.
    for (const int value : values)
    {
        std::cout << value << " ";
    }
}

int main()
{
    auto values = make_array();
    add_value(values, 1);
    print(values);
    std::cout << "\n";
    std::cout << values.size(); // and a vector keeps track of its own size.

    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19