Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • C++ array limit
    There seems to be no way of finding the amount of elements in an array (obviously one where we are not given the size of the array) .. can somebody please explain it to me? I've been searching everywhere but I cannot find any solutions..
    canopener posted this question on 12/18/13 | array, cpp, size, elements
    Answers
  • +
  • 3
  • -
  • How is the array declared? If it's declared as follows:
    int arr[100];
    and for some reason that 100 is unknown, you can go
    sizeof(arr)/sizeof(arr[0])
    . If it's declared like this:
    int *arr=new int[100];
    you have no way of knowing :(
  • +
  • 2
  • -
  • Currently, the way arrays are passed can be a little tough for some people, but here's a way I've noticed people have gotten the size of an array in C++ (and C):
    int A[] = gets(stdin);
    int size = sizeof(A)/sizeof(int); // an example with an int array
    cout << ArrayAddition(A,size);
    return 0;
    
    and then the function header is:
    int ArrayAddition(int arr[],int size) { return size; }
    
    Log in to write an answer.