(char *) Function Argument

This is a discussion on (char *) Function Argument within the C++ Programming forums, part of the General Programming Boards category; Ok, I have an assignment where I have been given a class definition (for a custom string class) and I ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    11

    Question (char *) Function Argument

    Ok, I have an assignment where I have been given a class definition (for a custom string class) and I need to simply write the member function definitions and such. One of the constructors is confusing me. This is it:
    Code:
    Mystring(char *);  // Convert a C-style string
    Ok, now what is char * exactly? I would think that there needs to be another variable in there. Is it just a pointer or something? Second, what is meant by "Convert a C-style string"? Does it mean to just take the information and place it in an array within my class? Lastly, once I know what (char *) is, how can I incorporate it in the function to do the intended task since it has no variable name?

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    Just in case, here is the entire class definition:
    Code:
    class Mystring{
    
    public:
    
         friend Mystring operator+(const Mystring&,const Mystring&);
    
         friend ostream& operator<<(ostream &, const Mystring &);
    
         Mystring(); // Set to empty String
    
         Mystring(char *);  // Convert a C-style string
    
         Mystring(const Mystring &);   //Copy Constructor
    
         ~Mystring();
    
         Mystring& operator=(const Mystring &);  //Assignment operator
    
         bool operator<(const Mystring &)const; //Member to compare for inequality ignoring case
    
         Mystring Str_upper()const; //Convert all alpha to upper case
    
         Mystring Str_lower()const; //Convert all alpha to lower case
    
         void PrintString()const;
    
    private:
    
         char * ptr;
    
         int length;  
    
    };

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >now what is char * exactly?
    A pointer to char. Most often, this designates a C-style string where the string is stored in an array of characters and terminated by the '\0' character. Because an array passed to a function degrades to a pointer to the first element, the two functions are equivalent:
    Code:
    void foo ( char a[] );
    void bar ( char *a );
    So your constructor is expecting an array of characters terminated by '\0'.

    >I would think that there needs to be another variable in there.
    In function declarations (but not definitions), the names of parameters can be omitted:
    Code:
    void foo ( char * ); // Legal!
    void foo ( char * ) // Illegal!
    {
    }
    
    void bar ( char *a ); // Legal
    void bar ( char *a ) // Legal
    {
    }
    >Does it mean to just take the information and place it in an array within my class?
    Basically, yes.

    >how can I incorporate it in the function to do the intended task since it has no variable name?
    When you write the definition you'll need to name it:
    Code:
    Mystring::Mystring(char *str)
    {
      ...
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    Thank you Prelude, that helped. Now, I`m stuck on another part. It invloves converting the string to upper case. Here`s the prototype:
    Code:
    Mystring Str_upper()const; //Convert all alpha to upper case
    I don`t really know how to approach this. I believe that it can be done using the ASCII decimal values, but I don`t know how to find out what value a certain character has in the array. Could someone please give me some pointers on this function?
    Last edited by MegaVortex; 04-14-2004 at 02:33 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Well, if you're sure that you're going to be using ASCII then you could simply use character arithmetic on each character in the string if it's a lower case letter:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char str[] = "this is a test";
    
      for ( int i = 0; i < 14; i++ ) {
        if ( str[i] >= 'a' && str[i] <= 'z' ) {
          str[i] += 'A' - 'a';
        }
      }
      cout<< str <<endl;
    }
    A better way would be to use toupper in the <cctype> header:
    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main()
    {
      char str[] = "this is a test";
    
      for ( int i = 0; i < 14; i++ ) {
        str[i] = toupper ( str[i] );
      }
      cout<< str <<endl;
    }
    My best code is written with the delete key.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > In function declarations (but not definitions), the names of parameters can be omitted:
    > void foo ( char * ) // Illegal!
    Actually, this is legal C++ (though not legal C)
    C++ allows unused parameters to be type checked (you name the type), but unused (by omitting the name).

    Very useful for generic interfaces where parameters can sometimes go unused, and you don't want to do things like
    Code:
    void foo ( int unused ) {
      unused = unused;   // suppress unused warning
    }
    But if you want to use it, you've got to give it a name
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 09:21 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21