Thread: strcat(), searching for a name

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    strcat(), searching for a name

    Hi

    Please help me to understand the working of the code below. CODE 1 is working fine. There is an issue with CODE 2 which is just a variation of CODE 1.

    1: How does strcat() works?
    [code]char temp_str[30] = {0};[\code]

    temp_str[] contains 29 zeroes and 30th element would be '\0'. Correct?

    Where does strcat() start appending to the destination string from?

    2: The variations in CODE 2 are in red. I get the following warning and the code doesn't produce correct result. Also have a look on the OUTPUT below.

    warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x|

    What's wrong?

    Please help me with the above queries.

    CODE 1
    Code:
    // read students' record and search for data of a particular student.cpp
    // use structure and display all data about the searched student
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    const int C = 2;
    
    ///////////////////////////////////////////////////////////////////////////////
    struct Student {int rollno; char firstname[20]; char lastname[20]; string sex; float gpa;};
    //////////////////////////////////////////////////////////////////////////////
    
    Student stud[C];
    
    int main()
    {
            int i; char searchname[30];
    
            for (i=0; i<C; i++)
            {
                    cout << "\n\nEnter student #" << (i+1) << "\'s details below\n\n";
                    cout << "enter roll no.: "; cin >> stud[i].rollno;
                    cin.ignore();
                    cout << "enter first name: "; cin.get(stud[i].firstname, 20);
                    cin.ignore();
                    cout << "enter last name: "; cin.get(stud[i].lastname, 20);
                    cout << "enter sex: "; cin >> stud[i].sex;
                    cout << "enter GPA: "; cin >> stud[i].gpa;
            }
    
            cout << endl;
    
            cin.ignore();
    
            cout << "enter the name to be searched for: "; cin.get(searchname, 30);
            // name to be searched for could be, say, Jack Dawson
    
    
            for (i=0; i<C; i++)
            {
                    char temp_str[30] = {0};
                    strcat(temp_str, stud[i].firstname);
                    strcat(temp_str, " ");
                    strcat(temp_str, stud[i].lastname);
    
    
                    if ( strcmp(temp_str, searchname ) == 0 )
                    {
                            cout << "Data for the searched student is shown below\n";
                            cout << "roll no.: " << stud[i].rollno << endl;
                            cout << "first name: " << stud[i].firstname << endl;
                            cout << "last name: " << stud[i].lastname << endl;
                            cout << "sex: " << stud[i].sex << endl;
                            cout << "GPA: " << stud[i].gpa << endl;
                            break;
                     }
    
                     else if ( i == (C-1) )
                     {
                             cout << "Sorry, no student exists with such name" << endl;
                             break;
                     }
            }
    
            system("pause");
            return 0;
    }
    CODE 2
    Code:
    // read students' record and search for data of a particular student.cpp
    // use structure and display all data about the searched student
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    const int C = 2;
    
    ///////////////////////////////////////////////////////////////////////////////
    struct Student {int rollno; char firstname[20]; char lastname[20]; string sex; float gpa;};
    //////////////////////////////////////////////////////////////////////////////
    
    Student stud[C];
    
    int main()
    {
            int i; char searchname[30]; char temp_str[30] = {0};
    
            for (i=0; i<C; i++)
            {
                    cout << "\n\nEnter student #" << (i+1) << "\'s details below\n\n";
                    cout << "enter roll no.: "; cin >> stud[i].rollno;
                    cin.ignore();
                    cout << "enter first name: "; cin.get(stud[i].firstname, 20);
                    cin.ignore();
                    cout << "enter last name: "; cin.get(stud[i].lastname, 20);
                    cout << "enter sex: "; cin >> stud[i].sex;
                    cout << "enter GPA: "; cin >> stud[i].gpa;
            }
    
            cout << endl;
    
            cin.ignore();
    
            cout << "enter the name to be searched for: "; cin.get(searchname, 30);
            // name to be searched for could be, say, Jack Dawson
    
    
            for (i=0; i<C; i++)
            {
                    temp_str[30] = {0};
                    strcat(temp_str, stud[i].firstname);
                    strcat(temp_str, " ");
                    strcat(temp_str, stud[i].lastname);
    
    
                    if ( strcmp(temp_str, searchname ) == 0 )
                    {
                            cout << "Data for the searched student is shown below\n";
                            cout << "roll no.: " << stud[i].rollno << endl;
                            cout << "first name: " << stud[i].firstname << endl;
                            cout << "last name: " << stud[i].lastname << endl;
                            cout << "sex: " << stud[i].sex << endl;
                            cout << "GPA: " << stud[i].gpa << endl;
                            break;
                     }
    
                     else if ( i == (C-1) )
                     {
                             cout << "Sorry, no student exists with such name" << endl;
                             break;
                     }
            }
    
            system("pause");
            return 0;
    }
    OUTPUT for CODE 2
    Code:
    
    Enter student #1's details below
    
    enter roll no.: 1
    enter first name: jackson
    enter last name: heights
    enter sex: male
    enter GPA: 6
    
    
    Enter student #2's details below
    
    enter roll no.: 2
    enter first name: jack
    enter last name: dawson
    enter sex: male
    enter GPA: 2
    
    enter the name to be searched for: jack dawson
    Sorry, no student exists with such name
    Press any key to continue . . .
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. Again 0 and '\0' are the same. Your array contains 30 zeroes, which means it is currently a string of length 0 (there are no characters in your string, as the immediate first character in memory is the end-of-string character). strcat would then begin immediately.

    2.
    Code:
    temp_str[30] = {0};
    is just plain illegal. For one temp_str[30] doesn't exist; for another 0-in-squiggles is not a character.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by tabstop View Post
    1. Again 0 and '\0' are the same. Your array contains 30 zeroes, which means it is currently a string of length 0 (there are no characters in your string, as the immediate first character in memory is the end-of-string character). strcat would then begin immediately.

    2.
    Code:
    temp_str[30] = {0};
    is just plain illegal. For one temp_str[30] doesn't exist; for another 0-in-squiggles is not a character.
    Thank you, tabstop.

    1: That means strcat() starts appending from the NULL. If the NULL is at 5th position in the array string, then it would star appending from the 5th position by overwriting the NULL.

    What would it mean if it were
    Code:
    char temp_str[30]={'0'};
    instead?

    2: Why is "temp_str[30]={0}" wrong? I'm simply initializing it to a new value? Please help me.

    Code:
    int v = 20;
    v = 15;
    v = 2;
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by jackson6612 View Post
    What would it mean if it were
    Code:
    char temp_str[30]={'0'};
    instead?
    You would be setting all your arrays elements to the character 0, which is 48 in ASCII.

    Quote Originally Posted by jackson6612 View Post
    2: Why is
    Code:
    temp_str[30]={0}
    wrong? I'm simply initializing it to a new value? Please help me.
    You can't set all of your arrays elements at once. The only exception is when you're defining it.
    Devoted my life to programming...

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jackson6612 View Post
    1: That means strcat() starts appending from the NULL. If the NULL is at 5th position in the array string, then it would star appending from the 5th position by overwriting the NULL.

    What would it mean if it were
    Code:
    char temp_str[30]={'0'};
    instead?
    Separate NULL from '\0'!
    NULL is for pointers ONLY. There is no NULL in your array.

    2: Why is "temp_str[30]={0}" wrong? I'm simply initializing it to a new value? Please help me.

    Code:
    int v = 20;
    v = 15;
    v = 2;
    1) Index 30 does not exist (you've been told already!).
    2) {0} is not a character. Each element in the array is a character. You cannot assign a non-character to a character!
    It might be possible to assign {0} to the entire array, however. But this requires you to use -std=c++0x.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Elysia View Post
    Separate NULL from '\0'!
    NULL is for pointers ONLY. There is no NULL in your array.
    This would require some explanation who is not familiar with nulls. Telling him that NULL is not 0 is like telling that integer's 0->pointer conversion does not exist. NULL is supposed to be used for pointers only, but it is guaranteed to be defined as 0. So it is zero. It might be confusing to him even more if he had heard about the "NULL-terminated strings" before, where ASCII's 0 has also a mnemonical name NUL.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is guaranteed to be defined as 0, but you don't need to know that. The "semantics" of NULL is that it can be used to assign a pointer to point "nowhere." And that is all.
    It is possible to do
    int n = NULL;
    char myarray = { NULL };
    Is this a good thing? It certainly is not, but is permitted by the language due to how NULL is defined.
    Similarly, using 0 for "null" pointers is silly, since it's what NULL was designed for.

    And this is being fixed in the coming standard with nullptr. Therefore, it is, IMO, not a good idea to refer to '\0' and 0 as NULL. They are not NULL.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia
    Code:
    char temp_str[30]={'0'};
    Separate NULL from '\0'!
    NULL is for pointers ONLY. There is no NULL in your array.
    Separate NULL from '\0' and '\0' from '0'.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Wrong quote...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I'm pretty sure you made the mistake. The character '0', the one you see when you print 0, is nothing like the others. So complaining about NULL was pointless of you, even if you were right that '0' is not '\0'. You just didn't say it.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    I'm pretty sure you made the mistake. The character '0', the one you see when you print 0, is nothing like the others.
    I am well aware of that fact.
    I was pointing out that '\0' and 0 should not be called NULL.
    (I don't know if '\0' is guaranteed to be 0, either.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    I know, but NULL in general (not as a constant in the language) is used to denote that soemthing does not exist, saying there is no NULL in his character array requires some explanation.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    (I don't know if '\0' is guaranteed to be 0, either.)
    It would be broken if it wasn't. ASCIZ strings, the kind C uses, are terminated by zero; it's in the name.

  14. #14
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Elysia View Post
    I don't know if '\0' is guaranteed to be 0, either.
    It is not a special escape sequence like \t. It is just an integer constant 0. However, I am not sure if it is internally interpreted as octal or decimal, but the result is the same anyway. AFAIK '\0' is char L'\0' is wchar_t and 0 is int.

    EDIT: I was thinking that you can use decimals also like \255 but I just realized you cannot. So it is octal's 0...
    Last edited by kmdv; 06-17-2011 at 06:17 AM.

  15. #15
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by Sipher View Post
    Code:
    char temp_str[30]={'0'};
    You would be setting all your arrays elements to the character 0, which is 48 in ASCII.
    Since nobody corrected this, it sets temp_str[0] to '0' (value 48 as mentioned). temp_str[1] to temp_str[29] are set to '\0' (value 0).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcat
    By TGM76 in forum C Programming
    Replies: 3
    Last Post: 07-27-2010, 07:42 AM
  2. How to use strcat
    By theprophet in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2005, 10:05 AM
  3. strcat
    By warney_out in forum C Programming
    Replies: 7
    Last Post: 10-01-2004, 01:01 AM
  4. strcat
    By linuxdude in forum C Programming
    Replies: 1
    Last Post: 12-11-2003, 04:55 PM
  5. Help with strcat
    By mmondok in forum C Programming
    Replies: 1
    Last Post: 02-02-2003, 06:33 PM