Solved

Java programmer needs help with C ++

Posted on 2003-11-20
5
217 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

743 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

11 Experts available now in Live!

Get 1:1 Help Now