Jump to content

anyone know c++?


aspnetguy
 Share

Recommended Posts

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...