ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I'm a java programmer and I also (agh!) use C#. The thing is that now I'm on a project using C++ and I have come up with a quite silly, yet tricky question... how to find out the length of the array in C++?
lets say, I receive an array as a parameter in a method:
void method(string array);
and I wanna traverse it as with a for loop, normally in java I would:
for(int i = 0; i < array.length; i++);
but there's no length for arrays in c++ is there? I'm particulary interested on arrays of strings....
i dont know about java as i have not worked on it but if your array is of char type
eg char name;
you can find the size of the array using string lib funxtion
int len = strlen(name);
it will return the size of the array as integer
As you know arrays are objects in Java and they have a property(.length) which gives you the length of the array. If you are going to use arrays in a wide range in your future programs, I suggest you define a class for arrays and make a method name ,for example, "add_element" to add anew element to you array.Then you can increase a private variable (e.g. int len) in this method and so you always have the length of your array. To avoid defining a class for arrays of each data type, you may define members of the array in your class "void" so it can be used to save any data type.The following function is an example of the method I said,for an array of integers:
void myarray::add_element(int x)
a[len]=x;//a is private int a;
and then you can easily get the length of your array with:
but if you want a quick way to find out the length of an string, it's better to define a rule for your arrays,for example at first assign ""(null string to all members of your string array, so when it is passed to a function,the function assumes that where array finishes is the first "".Then you can find the length of your array with a simple while():
void a(string str)
int count =0;
while(strcmp(str[count],"")!=0)//str[count] != ""
yep yep, I'd like to know the size of an array of strings, as I receive an array of strings as parameter and I want to traverse it.... is it posible?
perhaps I can get a work around with your proposal Mr J_Pooria.... and perhaps I could also use vectors or even create a template class... if there's no direct way to find out the array length I might actually combine these two sugestions, since a new class containing an array and a private atribute len wouldn't let me dinamically change the size would it?
I was thinking about creating a class with a vector inside, where I can add and eliminate elements dinamically and then as accessor method make it return an array (with now the length as a atribute).
Just thinking if nobody have had this problem or why isn't there a solution already provided by standard libraries hehehe
The C way to get the length of an array passed as a parameter is either
* Pass the length as another parameter
* Use a sentinel value (often NULL) to mark the end of the array.
To be C++ish about it you would use some object instead of an array, perhaps <vector>. For a traditional array, the language really does not store information about its length anywhere; you have to do it yourself.