Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Find the size of an array

Posted on 2006-06-11
Medium Priority
284 Views
Could anyone tell me how to find the size of an array?

for example if we have

int main()
{
A []={new A(), new A(), new A()};
printf("Size of Array:%d", //function that finds the size of the array(should give 3 in this case));

}

class A
{
public:
A();
};

A::A(){}

is there a function which finds out the size of array?
0
Question by:csound
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 3

Accepted Solution

srinims earned 100 total points
ID: 16883353
try this--->

u can simply put A[] =,

u have to mention the identifier also, the valid statement is A *a[]=;

#include <stdio.h>

class A
{
public:
A();
};

A::A(){}
int main()
{

A *a[]={new A(), new A(), new A()};
printf("Size of Array:%d",sizeof(a)/sizeof(a[0])); //function that finds the size of the array(should give 3 in this case));

}

0

LVL 17

Assisted Solution

rstaveley earned 100 total points
ID: 16883853
srinims is right, but you'll notice that you can't determine the size of the array in a function which has the array passed to it as a parameter. Weird, isn't it?

C and C++ have an underhand way of dealing with arrays. C programmers say that they are passed as "references", because "reference" doesn't have the meaning in C that it has in C++. C++ programmers say that you shouldn't be using arrays, because they are evil and bore you with the wonderfulness of vectors.

In C++ you can treat an array passed as a parameter to a function as a pointer. You can even increment it!

--------8<--------
#include <iostream>
using std::cout;

void f(char[10]);

int main()
{
char a[10];
for (int i = 0;i < 10;i++)
a[i] = 'a'+i;
cout << a[5] << '\n';
cout << (a+1)[5] << '\n';
//a++;      // Can't do this!
cout << a[5]
<< " size "
<< " size " << sizeof(a)/sizeof(*a)      /* This looks sensible */
<< '\n';

f(a);
}

void f(char a[10])
{
cout << a[5] << '\n';
cout << (a+1)[5] << '\n';
cout << a[5]
<< " size " << sizeof(a)/sizeof(*a)      /* Deceive yourself with this */
<< '\n';
a++;      // Can do this!
cout << a[5]      /* Deceive yourself with this */
<< " size " << sizeof(a)/sizeof(*a)      /* Deceive yourself with this */
<< '\n';

}
--------8<--------
0

Author Comment

ID: 16890792
Thanx for you helps!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month10 days, 3 hours left to enroll