aspnetguy Posted April 10, 2006 Share Posted April 10, 2006 if I do this #include <iostream>using namespace std;int main(){ int i; int arr[4]; arr[0] = 5; arr[1] = 10; arr[2] = 15; arr[3] = 20; int len = sizeof(arr)/sizeof(arr[0]); for(i=0;i<4;i++) { cout << arr[i] << endl; } cin.get(); return 0;} len equals 4 (the size of the array) like it should.Let me point out I am doing this because c++ arrays have no function to get the length of the array except sizeof(arr)/sizeof(arr[0]). So I want ot write a function to handle this.If I do this #include <iostream>using namespace std;int ArrayLength(int[]);int main(){ int i; int arr[4]; arr[0] = 5; arr[1] = 10; arr[2] = 15; arr[3] = 20; int len = ArrayLength(arr); for(i=0;i<4;i++) { cout << arr[i] << endl; } cin.get(); return 0;}int ArrayLength(int theArray[]){ return sizeof(theArray)/sizeof(theArray[0]);} len equals 1. It is like theArray[] is not getting the contents of arr and is declaring a new array or something.I am just starting to learn c++ and if anyone can explain this it would be a big help because I pass arrays to functions alot and need to know how to do it the right way. Thanks, Link to comment Share on other sites More sharing options...
Little Goat Posted April 10, 2006 Share Posted April 10, 2006 I personally think you should search google for a programming forum or find a yahoo group. LG Link to comment Share on other sites More sharing options...
Little Goat Posted April 10, 2006 Share Posted April 10, 2006 http://www.codecomments.com/forum272/try this.LG Link to comment Share on other sites More sharing options...
aspnetguy Posted April 11, 2006 Author Share Posted April 11, 2006 Thanks for the suggestions, I am in another C++ forum right now but the answer has not yet come.I may have to resort to emailing an old teacher. Link to comment Share on other sites More sharing options...
aspnetguy Posted April 11, 2006 Author Share Posted April 11, 2006 I have found the 'vector' it is a c++ dynamic array like ArrayList in C#This solves my problems Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now