Trouble finding size of pointer array passed to function

Hi, I'd like to get the size of the pointer array passed to the function.
(On line 20)
 
arrSize=sizeof(inputArr)/sizeof(inputArr[0]) ;

did not work.
On line 20, if I use
 
arrSize=std::array::size(inputArr);

it will say
"print n of n back\main.cpp|20|error: 'template<class _Tp, unsigned int _Nm> struct std::array' used without template parameters|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|"

but if I write
 
arrSize=size(inputArr);

it says size is not declared

here is my entire code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <array>
using namespace std;

template <class T, size_t N> class array;

struct node{
int value;
int n;
node* next;
};
void printN(node *inputArr[],int m) {
    // cout << "I'm in" << endl;
    int numRep=inputArr[m-1]->n;
    // cout << "I'm in 2" << endl;
    int refPos=m-1;
    // cout << "I'm in 3" << endl;
    int arrSize=std::array::size(inputArr);
    // cout << "I'm in 4" << endl;
    cout << "refPos, numRep, arrSize: " << refPos << " " << numRep <<
    " " << arrSize << endl;
    // cout << sizeof(node) << endl;
    refPos=refPos-(numRep%arrSize);
    // cout << "I'm in 5" << endl;
    if (refPos<0) {
        refPos=refPos+arrSize;
        // cout << "I'm in 6" << endl;
    }

    numRep==1 ?
    cout << "The value of n at "<< numRep <<
    " block before m=" << m << " is: " << inputArr[refPos]->n << endl:
    cout << "The value of n at "<< numRep <<
    " blocks before m=" << m << " is: " << inputArr[refPos]->n << endl;
    return;
}


int sizeArr, maxN, input;

int main()
{   cin >> sizeArr >> maxN;
    srand (time(NULL));
    node *head=NULL,*last=NULL;
    node *arr[sizeArr];
    for (int createNode=0; createNode<sizeArr ;createNode++) {
        arr[createNode]=(node*)malloc(sizeof(node));
        if (head==NULL) {
            head=arr[createNode];
        }
        arr[createNode]->value=createNode+1;
        arr[createNode]->n=rand()%(maxN+1);
        if (last!=NULL) {
            last->next=arr[createNode];
        }
        last=arr[createNode];
    }
    arr[sizeArr-1]->next=head;
    for (int loop=0; loop<sizeArr;loop++) {
        cout << arr[loop]->value << "\t" << arr[loop]->n << endl;
    }
    cout << endl;
    while (1) {
        cin >> input;
        if (input==0) {return 0;}
        printN(arr,input);
    }

    return 0;
}
Last edited on
I did, it didn't work

"
print n of n back\main.cpp|19|error: request for member 'size' in 'inputArr', which is of non-class type 'node**'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| "
I think you'll have to pass the size as a parameter.
void printN(node *inputArr[], int m, int size) {
Last edited on
1
2
3
4
5
#include <array>
...
template <class T, size_t N> class array;
...
int arrSize=std::array::size(inputArr);

Are you re-defining std::array? What are you trying to achieve? You are mixing C and C++ headers and programming styles together. Could you please describe what your code should do?
I want to create an array of pointers, and find its size in a function without passing its size to the function (only pass the array of pointers to the function)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <array>

struct node*{
int value;
node* next;
}

void someFunc (node* arr[], int irrelevant) {
int arrSize= ... // I need to find the size of the array here
}

int inputArrSize, someNumber=0;

int main() {
cin >> inputArrSize;
...
node (*arr)[inputArrSize];
...
someFunc (arr,someNumber)

return 0;
}
Last edited on
std::array<node*, inputArrSize> my_std_array_of_pointer;

A std::array knows its size, so you don't need to pass that information:
my_std_array_of_pointer.size();

gunnerfunner has already given you a link for further references.
oldspiderdude wrote:
I want to create an array of pointers, and find its size in a function without passing its size to the function (only pass the array of pointers to the function

size of any array (both T[N] and array<T,N>) is known at compile time, so your function has to be a template in order to be callable with arrays of different size

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <array>
#include <iostream>
struct node{ int value; node* next; };
template<size_t N>
void someFunc(node*(&arr)[N]) { std::cout << "size: " << N << '\n'; }
template<size_t N>
void someFunc(std::array<node*, N>& arr) { std::cout << "size: " << N << '\n'; }
int main() {
  node* a[10];
  std::array<node*, 10> b;
  someFunc(a);
  someFunc(b);
}

live demo: https://wandbox.org/permlink/kPNqsceJ4WSJ3EU5

oldspiderdude wrote:
void someFunc (node* arr[], int irrelevant)

This function does not take an array. It's the old C bug/feature/hack for backwards compatibility with B that arrays cannot be used as function parameters, but instead of making that an error, the C compiler quietly replaces that function declaration with one taking a pointer - the function you declared is really void someFunc (node** arr, int irrelevant), arrays are not involved in any way.

1
2
cin >> inputArrSize;
node (*arr)[inputArrSize];

Even if you fix that to create an array of pointers (currently it creates a pointer to an array) this is invalid: inputArrSize is not a constant and you cannot declare an array of non-constant size in C++. If you're using a compiler that allows that, fix it by using commandline option -pedantic-errors

If your size is not known until runtime, use std::vector<node*>
Last edited on
Topic archived. No new replies allowed.