-4

I can't find the final element of a particular string array. In this function I have an error in the execution when p reach the final element of the array. Here is my code. Can anyone help me?

string mesi[] = { "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre" };
printMesi(mesi);
void printMesi(string v[])
{
    for (string* p = v; *p != string(0); p++)
        cout << (*p) << "\n";

}
4
  • What is the error you are getting? Check this stackoverflow.com/questions/5739384/… please. Commented Dec 10, 2015 at 10:47
  • Only arrays constructed from string literals (char f[] = "f";) have any kind of termination marker. Commented Dec 10, 2015 at 11:01
  • If my answer worked for you, kindly accept it and close the question :) Commented Dec 10, 2015 at 14:58
  • I can't marked my question as resolved and closed it. Sorry. Can you help me also with it? Commented Dec 11, 2015 at 10:13

2 Answers 2

1

This should work for you. I'm passing the length of the array (which I have gotten from the main function)and then printing the data. In C++, when you pass an array as a parameter, you can't determine the size directly:

#include <iostream>
using namespace std;

void printMesi(string v[], int length)
{
    for(int i=0;i<length;i++)
        cout<<v[i]<<"\n";
}

int main()
{
    string mesi[] = { "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre" };
    printMesi (mesi, sizeof(mesi)/sizeof(mesi[0]));
    return 0;
}


Code in action: http://ideone.com/YcWAOA

Sign up to request clarification or add additional context in comments.

Comments

-1

This expresssion

string(0)

is already invalid and results in undefined behaviour.

The compiler considers this expression like

string( (const char * )0 )

but null pointer may not be used in this constructor.

The loop you wrote could work if you define one more element in the array - an empty string as for example

string mesi[] = 
{ 
    "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", 
    "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre", "" 
                                                                        ^^^
};

printMesi(mesi);
void printMesi(string v[])
{
    for (string* p = v; *p != ""; p++)
        cout << (*p) << "\n";
}

However first of all there is no sense to declare the array as an array of strings. It is an array of a fixed size with values that are supposed to be immutable.

So it is better to use string literals instead of objects of type std::string.

I would suggest to use std::array instead of the ordinary array. For example

#include <array>

//...

std::array<const char *, 12> mesi = 
{
    {
        "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", 
        "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre" 
    }
};


void printMesi( const std::array<const char *, 12> &mesi );
{
    for ( const char *m : mesi ) std::cout << m << "\n";
}

printMesi( mesi );

If your compiler does not support the class std::array then you can use an ordinary array declared the following way

const char * mesi[] = 
{ 
    "Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", 
    "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre", 0 
};

and the function can look like

void printMesi( const char *mesi[])
{
    for ( const char *m = mesi; m != 0; m++ )
        std::cout << m << "\n";
}

8 Comments

@ShreyansSheth The class std::array is introduced in C++ 2011.
Yes that's what. This could lead to compatibility issues
@ShreyansSheth Please do not imagine your own compatibility issues.
Umm..that's a bit rude. And my college still uses C++03. Just like a lot of other places. The above code would not compile there. Wouldn't you agree?
There is no point arguing with you. Besides, looking at your reputation, I would always be the one on the wrong side. All i'm saying is that 'It's not my personal problem' but a lot of places still use old compilers. We have to be wary of that and for such an easy question, we can suggest something that works on every compiler. That was the motive. That is all
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.