CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 6th, 2009, 06:10 PM
    Cosizzle Cosizzle is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 4
    Cosizzle is an unknown quantity at this point (<10)
    getting array size

    Hey guys,

    Sorry for the simplistic question, I come from a PHP/Java background.

    What im trying to figure out is how I can find out the array size of an array being passed into a class function.

    so:

    int cmds[] = {5,5,4,5,9,2}
    ...
    turtleOne.processTurtleMoves(cmds);

    =====[in the class now]=====
    void TurtleGraphics:: processTurtleMoves(const int commands[])
    {
    // how would I get the size here (6)
    // my understanding is that I should be able to go:
    cout << sizeof commands / sizeof(int);
    // however I get 1 as a result. Is this because of the constant?
    }

    Thank you kindly for your help!
    -Cody
    Reply With Quote
      #2    
    Old November 6th, 2009, 06:13 PM
    Cosizzle Cosizzle is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 4
    Cosizzle is an unknown quantity at this point (<10)
    Re: getting array size

    sorry, I should be more specific - I'm looking for the array length.
    Reply With Quote
      #3    
    Old November 6th, 2009, 06:21 PM
    ZuK ZuK is offline
    Senior Member
     
    Join Date: Oct 2002
    Location: Austria
    Posts: 1,082
    ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)
    Re: getting array size

    The reason why that doesn't work is that you cannot pass arrays to functions. You're only passing a pointer to it's first element.
    But then why do you want to calculate the size of an arry ?. You know the size whan you create it..
    And yes you have to pass that size to any functions that uses the array as an additional parameter.

    Kurt
    Reply With Quote
      #4    
    Old November 6th, 2009, 06:30 PM
    Cosizzle Cosizzle is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 4
    Cosizzle is an unknown quantity at this point (<10)
    Re: getting array size

    Thank you for your reply.

    I don't know the size when I create it because I'm not specifying a size - it's dynamic. In my example it's 6.. in my application its about 50 give or take.

    Why I need to calculate the length of the array is because each number in that array needs to do something. Therefore I need to loop through it, and within the loop create a switch statement that checks the current number and performs actions based on the case.
    Reply With Quote
      #5    
    Old November 6th, 2009, 06:33 PM
    ZuK ZuK is offline
    Senior Member
     
    Join Date: Oct 2002
    Location: Austria
    Posts: 1,082
    ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)
    Re: getting array size

    Quote:
    Originally Posted by Cosizzle View Post
    I don't know the size when I create it because I'm not specifying a size - it's dynamic. In my example it's 6..
    Shure you know. You just said it's 6 in that example.

    Kurt
    Reply With Quote
      #6    
    Old November 6th, 2009, 06:35 PM
    Cosizzle Cosizzle is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 4
    Cosizzle is an unknown quantity at this point (<10)
    Re: getting array size

    Yes... but that number could change, so opposed to hard coding that number in is there any way to have it dynamic.
    Reply With Quote
      #7    
    Old November 6th, 2009, 06:49 PM
    ZuK ZuK is offline
    Senior Member
     
    Join Date: Oct 2002
    Location: Austria
    Posts: 1,082
    ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)ZuK is a glorious beacon of light (400+)
    Re: getting array size

    Quote:
    Originally Posted by Cosizzle View Post
    Yes... but that number could change, so opposed to hard coding that number in is there any way to have it dynamic.
    There isn't anything like a dynamic array in c++. ( ok a std::vector is ).

    You could do something like this

    Code:
    int cmds[] = {5,5,4,5,9,2};
    int num_cmds = sizeof(cmds) / sizeof(cmds[0]) ;
    But that works only as long as cmds is an array. If you pass cmds to a function it becomes a pointer.
    Kurt
    Reply With Quote
      #8    
    Old November 6th, 2009, 07:00 PM
    Lindley Lindley is offline
    Elite Member
    Power Poster
     
    Join Date: Oct 2007
    Location: Fairfax, VA
    Posts: 6,519
    Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)Lindley is a name known to all (1000+)
    Re: getting array size

    The bottom line is that you can always know the array size at the point of creation, but it's necessary to pass that information around along with the array when using it elsewhere. This can either be done via a separate parameter (which is the common C approach), or by putting both the array pointer and the size into a class object (the C++ approach).

    Two such classes are already provided by the C++ standard library: std::tr1::array for fixed-size arrays, and std::vector for dynamic-size arrays.
    Reply With Quote
      #9    
    Old November 7th, 2009, 12:37 PM
    cilu's Avatar
    cilu cilu is offline
    Moderator/Reviewer/MS MVP
    Power Poster
     
    Join Date: Oct 2002
    Location: Timisoara, Romania
    Posts: 13,400
    cilu has a reputation beyond repute (3000+)cilu has a reputation beyond repute (3000+)cilu has a reputation beyond repute (3000+)cilu has a reputation beyond repute (3000+)cilu has a reputation beyond repute (3000+)cilu has a reputation beyond repute (3000+)cilu has a reputation beyond repute (3000+)cilu has a reputation beyond repute (3000+)cilu has a reputation beyond repute (3000+)cilu has a reputation beyond repute (3000+)cilu has a reputation beyond repute (3000+)
    Re: getting array size

    I suggest you take a look at this article: http://www.codeguru.com/cpp/cpp/cpp_...le.php/c15257/.
    __________________
    Marius Bancila
    Home Page | Blog
    My CodeGuru articles

    My latest articles: A TR1 Tutorial: array, tuple, unordered containers, random number generators, regex, smart pointers
    Customizable alert window

    Try my VSBuildStatus add-in for Visual Studio 2005, 2008 & 2010 (v1.1).
    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.
    Reply With Quote
      #10    
    Old November 8th, 2009, 08:31 AM
    gokmen's Avatar
    gokmen gokmen is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 4
    gokmen is an unknown quantity at this point (<10)
    Re: getting array size

    If it is possible, You can send the size of array to called function as a second parameter like:
    Code:
    int cmds[] = {5,5,4,5,9,2}
    int size = sizeof(cmds) / sizeof(cmds[0]) ;
    ...
    turtleOne.processTurtleMoves(cmds, size);
    .
    .
    .
    
    
    
    void TurtleGraphics::processTurtleMoves(const int commands[], const int size)
    {
         cout << size;
    }
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:35 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009