Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array/pointer problem 1

Status
Not open for further replies.

javaguy1

Programmer
May 14, 2003
136
US
im having trouble passing my string. here is the code

void main()
{
int day;
int month;
int year;
char dayOfWeek[10] = {'\0'};
DateToDay thisDay;

cout << &quot;Enter the Month as a number: &quot;;
cin >> month;
cout << &quot;Enter the Day as a number: &quot;;
cin >> day;
cout << &quot;Enter the Year as a number: &quot;;
cin >> year;
thisDay.getDay(day, month, year, dayOfWeek);
cout << month << &quot;/&quot; << day << &quot;/&quot; << year << &quot; falls on a &quot; << dayOfWeek << endl;
}


void DateToDay::getDay(int day, int month, int year, char theDay[])
{
int century;
int numberInWeek; //the number returned by the formula
char *daysOfWeek[7] = {&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;};

century = year / 100;
year = year % 100;
numberInWeek = (day + 25 * (month + 1) / 10 + year + year / 4 + century / 4 + 5 * century) % 7;
theDay = daysOfWeek[numberInWeek];
}

i can print out theDay from inside the function, but the array in the calling function, dayOfWeek, that was passed from main(), remains all nulls


 
should i change the scope of

char *daysOfWeek[7] = {&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Friday&quot;, &quot;Saturday&quot;};

make it a private member variable or something?
 
javaguy,

The problem is that your passing the variable &quot;dayOfWeek&quot; by value and not by reference. In essence your giving the member function a copy of the string ang not the actual address to where it is in memory.

To correct this I would change the calling function to:
thisDay.getDay(day, month, year, &dayOfWeek);

The member definition to:
void DateToDay::getDay(int day, int month, int year, char* theDay)

Try this out and let me know if it works.


Jeff

- Now that I answered a question can somebody answer my thread??? (thread116-555949)




 
nah...that give a compiler error

C:\My Courses\Cs133u\Lab6\ZellerCongruence\Driver.cpp(32) : error C2664: 'getDay' : cannot convert parameter 4 from 'char (*)[10]' to 'char *'
 
besides, arrays are always passed by reference...right?
 
This might be a better approach than the original:

char * DateToDay::getDay(int day, int month, int year)
{
char *daysOfWeek[3] = {&quot;Sunday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;};
return daysOfWeek[1];
}

//main
cout << thisDay.getDay( /*blah, blah*/ ) << endl;

I've trimmed the logic a little of course, but it should get you started. Additionally, if you made day, month, and year members of DateToday, then you can pass 0 parameters to the getDay() function (if that works for you).



 
If you stick with the original approach, you may want to look into strcpy() instead of the '=' operator.
 
that works, but i read an article that said it is bad to return pointers because the memory space has been deallocated. it said you should pass pointers or arrays into the function instead.
 
ok ill look into strcpy() we are supposed to be studying that right about now anyway
 
strcpy(theDay, daysOfWeek[numberInWeek]);

did the trick thanks
 
d'oh! i should read ALL of the instructions first

&quot;Use a switch statement to evaluate this integer and return (or provide) the proper day of the week as a String (ie Sunday, Monday and so forth).&quot;
 
&quot;that works, but i read an article that said it is bad to return pointers because the memory space has been deallocated. &quot;

You're right. Very bad habit to get into. :)
 
That's why the string class exists. Prefer to return a string, not a char*.


If you have to return a char*, it's best to require the caller to pass a pre-allocated char* to the function.

E.g.

Code:
char* getAString( char* buf, int length );
char* buffer = new char[ BUFSIZE ];

cout << getAString( buffer, BUFSIZE );

Barring that, you have to have the returned char* be a static variable of that function. Not very good, since it changes every time you call the function, but better than returning data that isn't guaranteed to exist.
 
from what my teacher said there are hundreds or thousands of String classes that people have created. i had concidered using the reserved word static...it would work, but....

why doesnt C++ have Strings like Java does? grrr
 
Yeah, there are a lot.

There's only one standard one, though: std::string.

I hear a lot about classes called CString and AnsiString (which is ironically not part of the ANSI Standard). Those seem to be the most popular otherwise.

And yes, there are tons of other different and not-so-different, useful and not-so-useful variants.

I'd suggest std::string over the other two. Not that std::string is necessarily better than the others (I haven't used either of the others I mentioned, and std::string is horribly designed), but that it's guaranteed to be there in every Standard library implementation.

If, for whatever reason, you find another more specific, better designed, or easier-to-use string class that you deem to be more suitable to your purpose than std::string, then, by all means, use it. Just know that if it's a proprietary string class (like I think CString is), you won't be able to link your project on platforms where you don't have access to that library. With something that's open source, you're a little better off, since you can at least port the string library along with your application.

Put another way, default to std::string, then use others as the situation requires.

Use static as a last resort only. The C library did that with several functions, and that's why a large portion of that library is thread-unsafe.
 
One comment on the use of CString...

CString is part of a standard, just not an ANSII standard. It is part of the MFC standard, which if you're using Visual C++ should already be included in your project. If you are using a console app, then you're pretty much stuck with strcpy() and the std::string.

But if you were to use a CString you could use equate operation like you originally tried to use. Example:

CString myStr;
mStr = &quot;Hello&quot;;

There's also a bunch of good member functions in CString that allow you to search the string, concat, and a whole lot more. I definitely recommend using CStrings (if you are using an MFC based app)

Sorry I didn't catch the strcpy() error soon enough.

Jeff
 
To clarify, strcpy and std::string are completely unrelated.

strcpy operates on variables of type char*, not type std::string. It's part of the <cstring> (or <string.h>) header file.

std::string is a class, and part of the <string> header file. std::string does have an assignment operator, so the example of assignment of a string literal to a CString applies equally well to an std::string. std::string also has member functions to do useful things (too many things, according to some).


And yes, when I said &quot;standard&quot; above, I meant specifically the ANSI/ISO C++ Standard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top