-4

I'm trying to return a string, which is the year.

This code is no where near my GUI layer, as such I'm attempting the following

#define COMPANY_COPYRIGHT            "Copyright © " + getYear() + "; 

And when it is called I have

void getCopyRight(BSTR* copyRight) 
{
    CString CopyRightStr    =   (CString)COMPANY_COPYRIGHT;
}

I'm struggling with my getYear() function

#include <time.h>

const char getYear()
{
    time_t rawtime;
    struct tm * timeinfo;

    time (&rawtime);
    timeinfo = localtime (&rawtime);

    int year = timeinfo->tm_year + 1900;

    char buf[8];               //I think 8 as I expect only 4 characters, each is 2 bit

    sprintf(buf,"%d", year);   //I can see year shows 2015. But not sure why it's %d because this should be the output format, surely it should be %s but this errors

    return buf;                //I can see buf shows 2015
}

The above errors with

'return' : cannot convert from 'char[4] to 'const char'

I understand the error message but not what to do. If I add a cast, such as

    return (const char)buf;                //I can see buf shows 2015

Then it seems to return a single ASCII character, which isn't what I want.

All I want is, instead of returning 2015 as an int, it returns only the value "2015" as a 'string'...

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • Your code makes no sense at all if this is C. Are you sure this isn't C++? Consider making that macro a local variable, or if not possible, at least make it a proper macro: `#define COMPANY_COPYRIGHT ( CString("Copyright © ") + getYear() )`. (assuming C++, otherwise the macro is nonsense) – Lundin May 07 '15 at 12:29
  • The duplicate cited is what I'm doing! – MyDaftQuestions May 07 '15 at 12:31
  • 1
    There are multiple issues: the type `char` is a single character, returning a pointer to a local variable is invalid, `4` is not enough to store a 4 character string. I suggest you reading a good C book. – Yu Hao May 07 '15 at 12:31
  • `strptime`, `snprintf` might be useful functions, once you corrected your undefined behavior issues. – Basile Starynkevitch May 07 '15 at 12:32
  • You added a comment *I think 8 as I expect only 4 characters, each is 2 bit*. Not, that's not true, you need at least `buf[5]` because C strings are null-terminated. As I said, go read a good C book, it's worth spending the time. – Yu Hao May 07 '15 at 12:38
  • @YuHao, yes, agreed. – MyDaftQuestions May 07 '15 at 12:46

1 Answers1

1

Keeping aside any other issue, your code produces undefined behaviour, at least.

In your getYear() function, you're trying to return the address of a local variable buf. This is not possible.

Instead, you can

  • define buf as char pointer.
  • allocate memory dyncamiccly using malloc()/calloc()
  • use and return buf from getYear()
  • use the returned pointer in the caller.

That said, if I understood your question correctly, you can make use of strtol() to convert a string to int. You need to change your function signature also.


EDIT:

OK, I got it wrong regarding the conversion part, what you want is to convert an int to a string. Your best bet is sprintf()

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Yes, I am doing that, but, it was the pointer bit which was the answer. I thought the pointer to a char would be a pointer to that single character :( ... Any way, thanks – MyDaftQuestions May 07 '15 at 12:51