tags:

views:

275

answers:

10

Hi,

I am trying to return a C String from a function but its not working. Here is my code.

char myFunction()
{

    return "My String";

}

and then in main i am calling it like this.

int main()
{

  printf("%s",myFunction());

}

I have also tried some other ways on myFunction but they are not working. E.g;

char myFunction()

 {
        char array[] = "my string";
        return array;
 }

Note: I am not allowed to use pointers!

Little Back Ground on this problem: There is function which is finding out which month it is e.g; if its 1 then it return January etc etc.

So when its going to print, it's doing like this. pritnf("Month: %s",calculateMonth(month));, No problem is how to return that string from calculateMonth Function.

+2  A: 

Your function return type is a single char. You should return a pointer to the first element of the character array. If you can't use pointers, then you are screwed. :(

hrnt
I am not allowed to use pointers :@ that's the problem.
itsaboutcode
+5  A: 

Your problem is with the return type of the function - it must be:

char *myFunction()

...and then your original formulation will work.

Note that you cannot have C strings without pointers being involved, somewhere along the line.

Also: Turn up your compiler warnings, it should have warned you about that return line converting a char * to char without an explicit cast.

caf
I think the signature should const char* since the string is a literal but if I'm not mistaken the compiler will accept this.
Luke
+1  A: 

A char is only a single one-byte character. It can't store the string of characters, nor is it a pointer (which you apparently cannot have). Therefore you cannot solve your problem without using pointers (which char[] is syntactic sugar for).

Nick Bedford
+8  A: 

your function signature needs to be:

const char * myFunction()
{

    return "My String";

}
cmroanirgo
A: 

Your function prototype states your function will return a char. Thus, you can't return a string in your function.

Hayato
+5  A: 

A C string is defined as a pointer to an array of characters.

If you cannot have pointers, by definition you cannot have strings.

Crashworks
A: 

Note this new function:

char* myFunction()

 {
        static char array[] = "my string";
        return array;
 }

I defined "array" as static, otherwise when the function end, the variable (and the pointer you are returning) gets out of scope. Since that memory is allocated on the stack, it will get corrupted. The downside of this implementation is that the code is not re-entrant and not thread-safe.

Another alternative would be to use malloc to allocate the string in the heap, and then free on the correct locations of your code. This code will be re-entract and thread safe.

elcuco
+1  A: 

If you really can't use pointers, do something like this:

char get_string_char(int index)
{
    static char array[] = "my string";
    return array[index];
}

int main()
{
    for (int i = 0; i < 9; ++i)
        printf("%c", get_string_char(i));
    printf("\n");
    return 0;
}

The magic number 9 is awful, this is not an example of good programming. But you get the point. Note that pointers and arrays are the same thing (kinda) so this is a bit cheating.

Hope this helps!

Regards,

Sebastiaan

Sebastiaan Megens
It's using a pointer in sheep's clothing! :P
Twisol
I count exactly three pointer subexpressions in that code.
caf
Usually if you need to implement such solutions to homework problems, then your preliminary assumptions are wrong.
hrnt
+3  A: 

Based on your newly-added backstory with the question, why not just return an integer from 1 to 12 for the month, and let the main() function use a switch statement or if-else ladder to decide what to print? It's certainly not the best way to go - char* would be - but in the context of a class like this I imagine it's probably the most elegant.

Twisol
You are right, i am also thinking in these terms.
itsaboutcode
A: 

Or how about this one:

void print_month(int month)
{
    switch (month)
    {
        case 0:
            printf("january");
            break;
        case 1:
            printf("february");
            break;
        ...etc...
    }
}

And call that with the month you compute somewhere else.

Regards,

Sebastiaan

Sebastiaan Megens