Solved

Java programmer needs help with C ++

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

763 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