Solved

Find the size of an array

Posted on 2006-06-11
3
249 Views
Last Modified: 2010-04-01
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
Comment
Question by:csound
3 Comments
 
LVL 3

Accepted Solution

by:
srinims earned 25 total points
Comment Utility
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

by:rstaveley
rstaveley earned 25 total points
Comment Utility
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

by:csound
Comment Utility
Thanx for you helps!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now