string returning function

Why this program is not working properly.

#include
#include

char returnStr();

int main()
{
char s[10];
*s = returnChar():
return 0;
}

char returnChar()
{
char s[] = "test";
return(*s);
}

Comments

  • : Why this program is not working properly.
    :
    : #include
    : #include
    :
    : char returnStr();
    :
    : int main()
    : {
    : char s[10];
    : *s = returnChar():
    : return 0;
    : }
    :
    : char returnChar()
    : {
    : char s[] = "test";
    : return(*s);
    : }
    :

    You have not declared any pointer(s) but you are referencing s like this:

    : [b]*s[/b] = returnChar():

    but I will test the code and tell you more...

    BattleGuard
    [hr]
    [size=3]You're looking at my sig, means that you wanna to steal it. Is that it? Well this stuff is now copyrighted.[/size]

    [size=3]Copyright(r) TM(r) by BattleGuard inc.[/size]
    :-D

  • : : Why this program is not working properly.
    : :
    : : #include
    : : #include
    : :
    : : char returnStr();
    : :
    : : int main()
    : : {
    : : char s[10];
    : : *s = returnChar():
    : : return 0;
    : : }
    : :
    : : char returnChar()
    : : {
    : : char s[] = "test";
    : : return(*s);
    : : }
    : :
    :
    : You have not declared any pointer(s) but you are referencing s like this:
    :
    : : [b]*s[/b] = returnChar():
    :
    : but I will test the code and tell you more...
    :
    : BattleGuard
    : [hr]
    : [size=3]You're looking at my sig, means that you wanna to steal it. Is that it? Well this stuff is now copyrighted.[/size]
    :
    : [size=3]Copyright(r) TM(r) by BattleGuard inc.[/size]
    : :-D
    :
    :

    Hi again,
    Here:

    1. You didn't declare returnChar.
    2. You can't dereference s like this : *s
    3. Line:

    *s = returnChar():

    error, replace : with ; ( colon with semicolon)

    4.You declared the function at the top as returnStr and you are defining it as return[b]Char[/b]. I can't really give you the corrected code, since I don't know you intentions...


    BattleGuard
    [hr]
    [size=3]You're looking at my sig, means that you wanna to steal it. Is that it? Well this stuff is now copyrighted.[/size]

    [size=3]Copyright(r) TM(r) by BattleGuard inc.[/size]
    :-D

  • [b][red]This message was edited by tom_sw at 2003-6-30 12:33:11[/red][/b][hr]
    : Why this program is not working properly.
    : [code]
    : #include
    : #include
    :
    : char returnStr();
    :
    : int main()
    : {
    : char s[10]; [red]// allocates a buffer of 10 chars [/red]
    : *s = returnChar()[red]; // sets contents of s[0] to the returned char [/red]
    : return 0;
    : }
    :
    : char returnChar() [red] // only returns 1 char, not a pointer to a set of chars[/red]
    : {
    : char s[] = "test"; [red]// allocates char array locally, invalid upon return[/red]
    : return(*s); [red]// returns contents of s[0], or 't';[/red]
    : }
    : [/code]

    I think what you'd rather do is:[code]
    #include
    #include
    #include

    int returnStr(char* str, int len);

    int main()
    {
    char s[10];
    returnStr(s, 10);
    printf("In main: %s
    ", s); [green]// show results[/green]
    return 0;
    }

    int returnStr(char* str, int len)
    {
    char s[] = "test"; [green]// declare local string[/green]
    strncpy (str, s, len); [green]// copy local string into string that was passed in[/green]
    printf(" In function: %s, %s, %d
    ", s, str, len); [green]// for debugging[/green]
    return (len(str)); [green]// returns length of new string [/green]
    }
    [/code]



  • [b][red]This message was edited by Code_Guru at 2003-6-30 12:30:58[/red][/b][hr]
    As BattleGuard mentioned, you are trying to use an array as a pointer. These are very different things, even though they are sometimes interchangeable. To get the code to compile, you could change it to something like this:

    #include
    #include

    char returnStr();

    int main()
    {
    char s[10];
    s[0] = returnChar():
    return 0;
    }

    char returnChar()
    {
    char* s = "test";
    return(*s);
    }

    However, I doubt this will give the result you want. The returnChar function will only return the first character of the word ('t' in this case) and copy it into the first slot of the char array in main().

    You need to first understand that char alone as a return type is NOT a whole string. In fact, the word string can be very misleading in C++ since there are several ways to store and manipulate string data. It looks like you are trying to use a char array or pointer to char, which are quite different even if they can sometimes be used interchangeably.

    A char array is just like any other array. The only reason it can be treated as a "string" is because the functions in the < cstring > library use a NULL character to indicate where the "string" ends. You cannot return any kind fo an array from a function.

    Before I go into too much detail, I suggest you read the chapters in your textbook about arrays and pointers. I'm sure there is plenty of information on the Web about them as well. These will help avoid unnecessary repetition in our ensuing discussion.

    Good luck and Keep Coding (TM)!


  • : [b][red]This message was edited by tom_sw at 2003-6-30 12:33:11[/red][/b][hr]
    : : Why this program is not working properly.
    : : [code]
    : : #include
    : : #include
    : :
    : : char returnStr();
    : :
    : : int main()
    : : {
    : : char s[10]; [red]// allocates a buffer of 10 chars [/red]
    : : *s = returnChar()[red]; // sets contents of s[0] to the returned char [/red]
    : : return 0;
    : : }
    : :
    : : char returnChar() [red] // only returns 1 char, not a pointer to a set of chars[/red]
    : : {
    : : char s[] = "test"; [red]// allocates char array locally, invalid upon return[/red]
    : : return(*s); [red]// returns contents of s[0], or 't';[/red]
    : : }
    : : [/code]
    :
    : I think what you'd rather do is:[code]
    : #include
    : #include
    : #include
    :
    : int returnStr(char* str, int len);
    :
    : int main()
    : {
    : char s[10];
    : returnStr(s, 10);
    : printf("In main: %s
    ", s); [green]// show results[/green]
    : return 0;
    : }
    :
    : int returnStr(char* str, int len)
    : {
    : char s[] = "test"; [green]// declare local string[/green]
    : strncpy (str, s, len); [green]// copy local string into string that was passed in[/green]
    : printf(" In function: %s, %s, %d
    ", s, str, len); [green]// for debugging[/green]
    : return (len(str)); [green]// returns length of new string [/green]
    : }
    : [/code]
    :
    :
    :
    :
    I agree. This code is probably much closer to what you intend than what I posted below. I also went into some discussion about char arrays and strings, if you are interested in reading it and/or adding your own comments.
  • : I agree. This code is probably much closer to what you intend than what I posted below. I also went into some discussion about char arrays and strings, if you are interested in reading it and/or adding your own comments.
    :
    I saw your comments. I think you gave a more comprehensive description of the problem than my few comments here. I also think that the lack of a specific string type in C has always been a weakness, and the various attempts in C++ to address this lack cause additional confusion (due to lack of real standards). And while char arrays vs char pointers differ in scope and allocation, once they are allocated, they are essentially the same thing. But who needs to split hairs, eh? :-)
  • : : I agree. This code is probably much closer to what you intend than what I posted below. I also went into some discussion about char arrays and strings, if you are interested in reading it and/or adding your own comments.
    : :
    : I saw your comments. I think you gave a more comprehensive description of the problem than my few comments here. I also think that the lack of a specific string type in C has always been a weakness, and the various attempts in C++ to address this lack cause additional confusion (due to lack of real standards). And while char arrays vs char pointers differ in scope and allocation, once they are allocated, they are essentially the same thing. But who needs to split hairs, eh? :-)
    :
    First of all, there IS a real C++ standard. It was published in 1997. I ardently advocate use of std::string over any other string class, unless you have a specific reason to do otherwise. For example, if you are developing Windows applications with MFC, then CString is crucial to learn since the MFC classes use it. I would hate to switch back and forth in such a situation. However, in a intro programming class, std::string should be emphasized more, imho. At least in the beginning. Arrays and pointers should be taught, too, but they tend to cause confusion if you try to use them in your first assignment.

    Also, I agree that pointers and arrays are essentially the same thing. However, the subtle differences CAN nip you in the bud as we have seen here. For instance, a function can return a pointer but not an array.
  • [b][red]This message was edited by tom_sw at 2003-6-30 13:59:21[/red][/b][hr]
    : First of all, there IS a real C++ standard. It was published in 1997. I ardently advocate use of std::string over any other string class, unless you have a specific reason to do otherwise. For example, if you are developing Windows applications with MFC, then CString is crucial to learn since the MFC classes use it. I would hate to switch back and forth in such a situation. However, in a intro programming class, std::string should be emphasized more, imho. At least in the beginning. Arrays and pointers should be taught, too, but they tend to cause confusion if you try to use them in your first assignment.
    :
    : Also, I agree that pointers and arrays are essentially the same thing. However, the subtle differences CAN nip you in the bud as we have seen here. For instance, a function can return a pointer but not an array.
    :
    I haven't had opportunity to use std::string much yet. All the stuff I've done has used either char arrays (of one sort or another), CString (MFC), _bstr, BSTR, CComBSTR, variant, WSTR (and other MS specific), and various "custom" string classes (embedded linux stuff). Switching back and forth between types can be a very common (and confusing) activity, depending on the application and the level of legacy code involved. (And I also hate switching back and forth, but there it is....:-()

    I totally agree that any real standard (std::string) should be used extensively (esp in learning env), but the basic C type char arrays are still out there all over the place (and are the basis for most of the other 'string' types), so that needs to be understood as well. (OK, not as the first assignment maybe, but still....:-)) I will need to do some more work with std::string so that I get fully comfortable with it.

    And we are in agreement as to the subtle differences of pointers and arrays. The nature and use of pointers is the biggest problem for most people (hence the efforts to eradicate the pointer from later languages). I think having a good understanding of what the machine is actually doing at a memory/register level is a major help in working with pointers, but that gets into the electronics stuff that is not standard for CompSci.


  • : [b][red]This message was edited by tom_sw at 2003-6-30 13:59:21[/red][/b][hr]
    : : First of all, there IS a real C++ standard. It was published in 1997. I ardently advocate use of std::string over any other string class, unless you have a specific reason to do otherwise. For example, if you are developing Windows applications with MFC, then CString is crucial to learn since the MFC classes use it. I would hate to switch back and forth in such a situation. However, in a intro programming class, std::string should be emphasized more, imho. At least in the beginning. Arrays and pointers should be taught, too, but they tend to cause confusion if you try to use them in your first assignment.
    : :
    : : Also, I agree that pointers and arrays are essentially the same thing. However, the subtle differences CAN nip you in the bud as we have seen here. For instance, a function can return a pointer but not an array.
    : :
    : I haven't had opportunity to use std::string much yet. All the stuff I've done has used either char arrays (of one sort or another), CString (MFC), _bstr, BSTR, CComBSTR, variant, WSTR (and other MS specific), and various "custom" string classes (embedded linux stuff). Switching back and forth between types can be a very common (and confusing) activity, depending on the application and the level of legacy code involved. (And I also hate switching back and forth, but there it is....:-()
    :
    : I totally agree that any real standard (std::string) should be used extensively (esp in learning env), but the basic C type char arrays are still out there all over the place (and are the basis for most of the other 'string' types), so that needs to be understood as well. (OK, not as the first assignment maybe, but still....:-)) I will need to do some more work with std::string so that I get fully comfortable with it.
    :
    : And we are in agreement as to the subtle differences of pointers and arrays. The nature and use of pointers is the biggest problem for most people (hence the efforts to eradicate the pointer from later languages). I think having a good understanding of what the machine is actually doing at a memory/register level is a major help in working with pointers, but that gets into the electronics stuff that is not standard for CompSci.
    :
    :
    :
    I haven't worked with much legacy code as of yet. Most of my coding has been for homework assignments or my own little pet projects. Needless to say, I create these from scratch. When writing new code, I think it is crucial to stick as closely to the standard as possible. I see your point that when dealing with legacy code, you need to switch back and forth. I guess that I'll eventually learn to live with such problems once I graduate with my CS degree.

    Also, "electronics stuff" is quite common in a CS cirriculum, if the university I attend is any measure. In fact, I have talked with other CS students from other schools who are required to take a computer architecture course. I think this class can help someone understand pointers at the machine level.

    I also agree that char arrays need to be learned eventually. Sooner than later, even. However, in a first programming course, they are simply frustrating, at the best. Using std::string to start with, can help overcome some of the hurdles you may encounter. Once you have a better understanding of cotrol structures, using char arrays can be beneficial. In fact, I think building your own string class is a great learning experience. As you said, most implementations are based off of char arrays. Writing your own string class, and supporting fucntions, is a great way to start understanding how char arrays can be useful.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories