Solved

Java programmer needs help with C ++

Posted on 2003-11-20
5
218 Views
Last Modified: 2010-04-01
Could someone help me please!? :)
This may be the easiest question every, but how do you get the length of an array in C++? I'm a Java programmer, trying to get the length of an args array passed into 'main'. In Java it would be a case of just coding 'args.length' but C++ didn't like it when i tried this.
Can someone help?
Cheers,
Darren.
0
Comment
Question by:led_zephrylin
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9787110
Static array:

int array[10];

int n1 = sizeof(array)/sizeof(int);        // number of array elements
int n2 = sizeof(array);                        // length of array in bytes

Dynamic array:

int nSize = 10;
int* pArray = new int[nSize];

int n1 = nSize;                       // number of array elements
int n2 = nSize * sizeof(int);    // length of array in bytes
0
 
LVL 13

Accepted Solution

by:
SteH earned 50 total points
ID: 9787487
If you have your main declared as
int main(int argc, char* argv[])
then argc contains the number of arguments available in argv[]. But argv[0] is the program name. So unsual for C/C++ the arguments are found in
argv[1] ... argv[argc].
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9787714
> how do you get the length of an array in C++?

Ask the person who supplied it :-)

In C++ and C, all you get when you are passed an array as a parameter is the address of its first element. If you declare the array locally, you can use sizeof(), but sizeof() just gives you the size of a pointer, when it is passed as a parameter. The code snippet I posted at http:/Programming/Programming_Languages/C/Q_20797844.html#9757783 illustrates the point.

C++ programmers now benefit from something in the standard library called a vector which is much more like the arrays which you are familiar with in Java... or perhaps Java vectors anyhow :-)  If you use a vector, you have can determin the number of elements from its size().
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9788596
> argv[1] ... argv[argc].

You mean:

argv[1] ... argv[argc-1]

The argc count includes the executable name. argv[0] has the executable name.
0
 
LVL 13

Expert Comment

by:SteH
ID: 9788676
Haven't used the command line for too long. Thx for the correction.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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.

863 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

22 Experts available now in Live!

Get 1:1 Help Now