Menu

Error in trying array script

2007-09-17
2012-09-26
  • Arithmomaniac

    Arithmomaniac - 2007-09-17

    Hello! I'm trying to learn C++, and wrote proof-of-concept code:

    include <cstdlib>

    include <iostream>

    using namespace std;

    int CountArray(int Array[]);

    int main(int argc, char *argv[])
    {
    int Array[8] = {5, 6, 7, 8, 0, 0, 0, 0};
    cout << CountArray(Array[8]);
    system("PAUSE");
    return EXIT_SUCCESS;
    }

    int CountArray(int Array[])
    {
    int Count;
    for(int x = 0; x < 8; x++)
    {
    if (Array[x] == 0)
    {
    break;
    }
    Count++;
    }
    return Count;
    }

    I get the following error, and since I'm still learning C++, I can't find the problem:

    main.cpp: In function int main(int, char**)': main.cpp:11: error: invalid conversion fromint' to int*' main.cpp:11: error: initializing argument 1 ofint CountArray(int*)'
    main.cpp:28:2: warning: no newline at end of file
    make.exe: *** [main.o] Error 1
    Execution terminated

    What am I doing wrong?

    Thanks,
    Arithmomaniac

     
    • Arithmomaniac

      Arithmomaniac - 2007-09-18

      It was just the case-sensitive problem.
      Thanks for your patience,
      Arithmomaniac

       
    • Anonymous

      Anonymous - 2007-09-17

      In line 11:

      the argument Array[8] is the 9th element of the Array array; a int, and also a illegal element in this array with 8 elements.

      Old Newbie

       
    • Anonymous

      Anonymous - 2007-09-17

      I guess you meant:

      cout << CountArray(Array)

      You are passing the array (or rather a pointer to it), not an array element.

      Beware not to kid yourself with the "int CountArray(int Array[])" notation, as the error message reveals the parameter 'degrades' to an int* - i.e. the array size information is lost.

       
    • Arithmomaniac

      Arithmomaniac - 2007-09-18

      Then how would I pass each of the 8 elements of Array[8] into CountArray?
      Thanks,
      Arithmomaniac

       
      • Osito

        Osito - 2007-09-18

        Why would you want to pass each of them? Your counting function looks like it counts until it finds a zero element, so you would want to pass the pointer to the array like Clifford mentioned, and step through each of the eight elements.

         
      • Osito

        Osito - 2007-09-18

        Also, I'm super-bored so I compiled your code and ran it - you will want to initialize Count to zero or you will get a meaningless number when you print it. And stick " << endl" at the end of your cout line to make it easier to read the output(good habit for console apps IMO).

         
    • Arithmomaniac

      Arithmomaniac - 2007-09-18

      I don't know pointers yet, so I'll do that first and then come back to array skills.
      Thanks,
      Arithmomaniac

       
    • Arithmomaniac

      Arithmomaniac - 2007-09-18

      I'm trying to figure out why the example in my book (C++ for dummies) Worked, so I copied it almost word-for-word:


      include <cstdlib>

      include <iostream>

      using namespace std;

      int SumArray(int InsertArrayHere[])
      {
      int sum = 0;
      for (int i = 0 ; i < 8; i++)
      {sum += InsertArrayHere[i];}
      return sum;
      }

      int main(int argc, char *argv[])
      {
      int Array[] = {1, 2, 3, 4, 5, 6, 7, 8};
      cout << sumArray(Array) << "\n";
      system("PAUSE");
      return EXIT_SUCCESS;
      }


      I still get
      [line 3 and file] `sumArray' undeclared (first use this function)

      Why is SumArray not working? it compiles by itself...
      Thanks,
      Arithmomaniac

       
    • Anonymous

      Anonymous - 2007-09-18

      sumArray and SumArray are not the same thing! The language is case sensitive.

      You pass Array rather than Array[8] to CountArray. Array is a pointer to the array, so that all 8 elements can be accessed. There is nothing ostensibly wrong with declaring the function as int SumArray(int InsertArrayHere[]), but IMO is often causes people to believe that they are passing an array rather than a simple pointer. In C/C++ an array and a pointer are almost the same thing. To see how they are different try this code:

      include <iostream>

      include <cstdlib>

      using namespace std;

      void function( int ss[] )
      {
      cout << "void function( int ss[] )" << endl ;
      cout << "size of ss = " << sizeof(ss) << endl ;
      cout << "ss[2] = " << ss[2] << endl << endl ;
      }

      int main()
      {
      int aa[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
      cout << "size of aa = " << sizeof(aa) << endl ;
      cout << "aa[2] = " << aa[2] << endl << endl ;
      function( aa ) ;

      system( &quot;pause&quot; ) ;
      

      }

      The output is:

      size of aa = 40
      aa[2] = 2

      void function( int ss[] )
      size of ss = 4
      ss[2] = 2

      Press any key to continue . . .

      Note how although aa is passed to function(), within function() it is a simple pointer (size is 4 bytes not 40). But observe also that all the members of aa can still be accessed through this pointer using array notation.

      So the square bracket notation is useful only as an indicator that an array is expected to be passed rather than just a pointer, but semantically it makes no difference to the compiler, and usually this is obvious from context. Often in such functions the size of the array is also passed as a parameter.

      Clifford

       
    • Anonymous

      Anonymous - 2007-09-18

      > Array is a pointer to the array [...]

      Perhaps, to a true beginner, is appropriate to point out that in C/C++ the name of an array can be interchanged with the notion of a pointer to it (the object represented by that name).

      Old Newbie

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.