Why can't I access the size of this C++ array?












-2















I am new to C++, and I am trying to write a function to solve this challenge. The code I have looks like this:



#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;

void printKMax(int arr, int n, int k){
deque<int> subarray;
int i = arr.size();
int maxValues[n];

while(i >= 0) {
while (subarray.size() < n) {
subarray.push_back(arr[i]);
arr.pop();
--i;
}
maxValues.push(max_element(begin(subarray), end(subarray)));
subarray.pop_front();
}

for (array<int>::iterator it=maxValues.begin(); it!=maxValues.end();
it++) {
cout << maxValues[*it] << " ";
}

}

int main(){

int t;
cin >> t;
while(t>0) {
int n,k;
cin >> n >> k;
int i;
int arr[n];
for(i=0;i<n;i++)
cin >> arr[i];
printKMax(arr, n, k);
t--;
}
return 0;
}


This code is throwing, among other things, an error saying:




"request for member ‘size’ in ‘arr’, which is of non-class type
‘int*’ int i = arr.size();"




Can someone please help me understand what this means? Am I trying to reference something from outside of the class, so that I need to use a pointer? Is it a problem with the way the array is declared?



Thanks in advance!










share|improve this question




















  • 2





    You need a std::vector or std::array to do that. Raw arrays don not have any class member functions.

    – πάντα ῥεῖ
    Nov 27 '18 at 7:18






  • 1





    Possible duplicate of How do I find the length of an array?

    – abcalphabet
    Nov 27 '18 at 7:23
















-2















I am new to C++, and I am trying to write a function to solve this challenge. The code I have looks like this:



#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;

void printKMax(int arr, int n, int k){
deque<int> subarray;
int i = arr.size();
int maxValues[n];

while(i >= 0) {
while (subarray.size() < n) {
subarray.push_back(arr[i]);
arr.pop();
--i;
}
maxValues.push(max_element(begin(subarray), end(subarray)));
subarray.pop_front();
}

for (array<int>::iterator it=maxValues.begin(); it!=maxValues.end();
it++) {
cout << maxValues[*it] << " ";
}

}

int main(){

int t;
cin >> t;
while(t>0) {
int n,k;
cin >> n >> k;
int i;
int arr[n];
for(i=0;i<n;i++)
cin >> arr[i];
printKMax(arr, n, k);
t--;
}
return 0;
}


This code is throwing, among other things, an error saying:




"request for member ‘size’ in ‘arr’, which is of non-class type
‘int*’ int i = arr.size();"




Can someone please help me understand what this means? Am I trying to reference something from outside of the class, so that I need to use a pointer? Is it a problem with the way the array is declared?



Thanks in advance!










share|improve this question




















  • 2





    You need a std::vector or std::array to do that. Raw arrays don not have any class member functions.

    – πάντα ῥεῖ
    Nov 27 '18 at 7:18






  • 1





    Possible duplicate of How do I find the length of an array?

    – abcalphabet
    Nov 27 '18 at 7:23














-2












-2








-2








I am new to C++, and I am trying to write a function to solve this challenge. The code I have looks like this:



#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;

void printKMax(int arr, int n, int k){
deque<int> subarray;
int i = arr.size();
int maxValues[n];

while(i >= 0) {
while (subarray.size() < n) {
subarray.push_back(arr[i]);
arr.pop();
--i;
}
maxValues.push(max_element(begin(subarray), end(subarray)));
subarray.pop_front();
}

for (array<int>::iterator it=maxValues.begin(); it!=maxValues.end();
it++) {
cout << maxValues[*it] << " ";
}

}

int main(){

int t;
cin >> t;
while(t>0) {
int n,k;
cin >> n >> k;
int i;
int arr[n];
for(i=0;i<n;i++)
cin >> arr[i];
printKMax(arr, n, k);
t--;
}
return 0;
}


This code is throwing, among other things, an error saying:




"request for member ‘size’ in ‘arr’, which is of non-class type
‘int*’ int i = arr.size();"




Can someone please help me understand what this means? Am I trying to reference something from outside of the class, so that I need to use a pointer? Is it a problem with the way the array is declared?



Thanks in advance!










share|improve this question
















I am new to C++, and I am trying to write a function to solve this challenge. The code I have looks like this:



#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;

void printKMax(int arr, int n, int k){
deque<int> subarray;
int i = arr.size();
int maxValues[n];

while(i >= 0) {
while (subarray.size() < n) {
subarray.push_back(arr[i]);
arr.pop();
--i;
}
maxValues.push(max_element(begin(subarray), end(subarray)));
subarray.pop_front();
}

for (array<int>::iterator it=maxValues.begin(); it!=maxValues.end();
it++) {
cout << maxValues[*it] << " ";
}

}

int main(){

int t;
cin >> t;
while(t>0) {
int n,k;
cin >> n >> k;
int i;
int arr[n];
for(i=0;i<n;i++)
cin >> arr[i];
printKMax(arr, n, k);
t--;
}
return 0;
}


This code is throwing, among other things, an error saying:




"request for member ‘size’ in ‘arr’, which is of non-class type
‘int*’ int i = arr.size();"




Can someone please help me understand what this means? Am I trying to reference something from outside of the class, so that I need to use a pointer? Is it a problem with the way the array is declared?



Thanks in advance!







c++ algorithm data-structures deque






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 0:01









jww

53.7k40231508




53.7k40231508










asked Nov 27 '18 at 7:16









adam troppadam tropp

8818




8818








  • 2





    You need a std::vector or std::array to do that. Raw arrays don not have any class member functions.

    – πάντα ῥεῖ
    Nov 27 '18 at 7:18






  • 1





    Possible duplicate of How do I find the length of an array?

    – abcalphabet
    Nov 27 '18 at 7:23














  • 2





    You need a std::vector or std::array to do that. Raw arrays don not have any class member functions.

    – πάντα ῥεῖ
    Nov 27 '18 at 7:18






  • 1





    Possible duplicate of How do I find the length of an array?

    – abcalphabet
    Nov 27 '18 at 7:23








2




2





You need a std::vector or std::array to do that. Raw arrays don not have any class member functions.

– πάντα ῥεῖ
Nov 27 '18 at 7:18





You need a std::vector or std::array to do that. Raw arrays don not have any class member functions.

– πάντα ῥεῖ
Nov 27 '18 at 7:18




1




1





Possible duplicate of How do I find the length of an array?

– abcalphabet
Nov 27 '18 at 7:23





Possible duplicate of How do I find the length of an array?

– abcalphabet
Nov 27 '18 at 7:23












2 Answers
2






active

oldest

votes


















1














A C-style array isn't an object and you can't call size on it. It degenerates to nothing more than a pointer, and if you pass it here



void printKMax(int arr, int n, int k){


Then this function doesn't even know the size. To solve the problem, you need to pass the size separately, as another parameter. Alternatively, you could just use a data structure such as std::vector instead, I would recommend this over using a plain array in this situation. Instead of int arr[n]; you would have std::vector<int> arr;. You can then query the size of the std::vector in your printKMax function. Accessing the vector uses the same syntax as accessing an array, and filling the vector works similar to how you're doing it now, with cin >> arr.






share|improve this answer

































    0














    you can use this statement to get length of array



    int length = (sizeof(arr)/sizeof(*arr));






    share|improve this answer
























    • Will not work in this case.

      – DeiDei
      Nov 27 '18 at 9:09











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53494526%2fwhy-cant-i-access-the-size-of-this-c-array%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    A C-style array isn't an object and you can't call size on it. It degenerates to nothing more than a pointer, and if you pass it here



    void printKMax(int arr, int n, int k){


    Then this function doesn't even know the size. To solve the problem, you need to pass the size separately, as another parameter. Alternatively, you could just use a data structure such as std::vector instead, I would recommend this over using a plain array in this situation. Instead of int arr[n]; you would have std::vector<int> arr;. You can then query the size of the std::vector in your printKMax function. Accessing the vector uses the same syntax as accessing an array, and filling the vector works similar to how you're doing it now, with cin >> arr.






    share|improve this answer






























      1














      A C-style array isn't an object and you can't call size on it. It degenerates to nothing more than a pointer, and if you pass it here



      void printKMax(int arr, int n, int k){


      Then this function doesn't even know the size. To solve the problem, you need to pass the size separately, as another parameter. Alternatively, you could just use a data structure such as std::vector instead, I would recommend this over using a plain array in this situation. Instead of int arr[n]; you would have std::vector<int> arr;. You can then query the size of the std::vector in your printKMax function. Accessing the vector uses the same syntax as accessing an array, and filling the vector works similar to how you're doing it now, with cin >> arr.






      share|improve this answer




























        1












        1








        1







        A C-style array isn't an object and you can't call size on it. It degenerates to nothing more than a pointer, and if you pass it here



        void printKMax(int arr, int n, int k){


        Then this function doesn't even know the size. To solve the problem, you need to pass the size separately, as another parameter. Alternatively, you could just use a data structure such as std::vector instead, I would recommend this over using a plain array in this situation. Instead of int arr[n]; you would have std::vector<int> arr;. You can then query the size of the std::vector in your printKMax function. Accessing the vector uses the same syntax as accessing an array, and filling the vector works similar to how you're doing it now, with cin >> arr.






        share|improve this answer















        A C-style array isn't an object and you can't call size on it. It degenerates to nothing more than a pointer, and if you pass it here



        void printKMax(int arr, int n, int k){


        Then this function doesn't even know the size. To solve the problem, you need to pass the size separately, as another parameter. Alternatively, you could just use a data structure such as std::vector instead, I would recommend this over using a plain array in this situation. Instead of int arr[n]; you would have std::vector<int> arr;. You can then query the size of the std::vector in your printKMax function. Accessing the vector uses the same syntax as accessing an array, and filling the vector works similar to how you're doing it now, with cin >> arr.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 27 '18 at 7:26

























        answered Nov 27 '18 at 7:21









        BlazeBlaze

        6,8081732




        6,8081732

























            0














            you can use this statement to get length of array



            int length = (sizeof(arr)/sizeof(*arr));






            share|improve this answer
























            • Will not work in this case.

              – DeiDei
              Nov 27 '18 at 9:09
















            0














            you can use this statement to get length of array



            int length = (sizeof(arr)/sizeof(*arr));






            share|improve this answer
























            • Will not work in this case.

              – DeiDei
              Nov 27 '18 at 9:09














            0












            0








            0







            you can use this statement to get length of array



            int length = (sizeof(arr)/sizeof(*arr));






            share|improve this answer













            you can use this statement to get length of array



            int length = (sizeof(arr)/sizeof(*arr));







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 27 '18 at 7:25









            JinJin

            9311323




            9311323













            • Will not work in this case.

              – DeiDei
              Nov 27 '18 at 9:09



















            • Will not work in this case.

              – DeiDei
              Nov 27 '18 at 9:09

















            Will not work in this case.

            – DeiDei
            Nov 27 '18 at 9:09





            Will not work in this case.

            – DeiDei
            Nov 27 '18 at 9:09


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53494526%2fwhy-cant-i-access-the-size-of-this-c-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Lallio

            Unable to find Lightning Node

            Futebolista