Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Static arrays and sizeof

Static arrays and sizeof

Scheduled Pinned Locked Moved C / C++ / MFC
helplinuxdata-structures
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark Basedow
    wrote on last edited by
    #1

    Why can't I use sizeof to find the size of a static data array within a class. It works fine if I use sizeof outside the class. class myclass { public: static char myarray[]; void myfunction() { // VC6 compile error: illegal sizeof operand // GCC 3.2 'sizeof' applied to incomplete type 'char []' // cout << sizeof (myarray); } }; char myclass::myarray[] = "this is a string"; int main(int argc, char* argv[]) { myclass mc; cout << sizeof(mc.myarray); // works //mc.myfunction(); return 0; } another alternative I tried was to make the array const but this only works using GCC on linux; using VC6 I get errors. class myclass2 { public: //VC6 error C2258: illegal pure syntax, must be '= 0' //VC6 error C2252: 'myarray' : pure specifier can only be specified for functions static const char myarray [] = "this is another string"; void myfunction() { cout << sizeof(myarray); } }; int main(int argc, char* argv[]) { myclass2 mc2; cout << sizeof(mc2.myarray); // works mc2.myfunction(); return 0; } Thanks if anyone can help.

    Z 1 Reply Last reply
    0
    • M Mark Basedow

      Why can't I use sizeof to find the size of a static data array within a class. It works fine if I use sizeof outside the class. class myclass { public: static char myarray[]; void myfunction() { // VC6 compile error: illegal sizeof operand // GCC 3.2 'sizeof' applied to incomplete type 'char []' // cout << sizeof (myarray); } }; char myclass::myarray[] = "this is a string"; int main(int argc, char* argv[]) { myclass mc; cout << sizeof(mc.myarray); // works //mc.myfunction(); return 0; } another alternative I tried was to make the array const but this only works using GCC on linux; using VC6 I get errors. class myclass2 { public: //VC6 error C2258: illegal pure syntax, must be '= 0' //VC6 error C2252: 'myarray' : pure specifier can only be specified for functions static const char myarray [] = "this is another string"; void myfunction() { cout << sizeof(myarray); } }; int main(int argc, char* argv[]) { myclass2 mc2; cout << sizeof(mc2.myarray); // works mc2.myfunction(); return 0; } Thanks if anyone can help.

      Z Offline
      Z Offline
      Zdeslav Vojkovic
      wrote on last edited by
      #2

      myarray must be defined before it is used as a parameter to sizeof, so you can't use it inside a class definition. compiler needs to see the definiton of the array to know its size. Define the function in cpp file, after the myarray definition: // in cpp file, after "char myclass::myarray[] = "this is a string";" void myclass::myfunction() { cout << sizeof (myarray); };

      M 1 Reply Last reply
      0
      • Z Zdeslav Vojkovic

        myarray must be defined before it is used as a parameter to sizeof, so you can't use it inside a class definition. compiler needs to see the definiton of the array to know its size. Define the function in cpp file, after the myarray definition: // in cpp file, after "char myclass::myarray[] = "this is a string";" void myclass::myfunction() { cout << sizeof (myarray); };

        M Offline
        M Offline
        Mark Basedow
        wrote on last edited by
        #3

        :):):):):):):):):):):):):):)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups