Solved

Java programmer needs help with C ++

Posted on 2003-11-20
5
223 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
[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
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

688 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