Thread: Replace string with const char *

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    84

    Replace string with const char *

    Hi all,

    I want to replace the "string word" within the code below with "const char * word". But when I do that,I get an error message that a const char * can' t be on the left side of "."
    Can someone help me with that please?

    Code:
    #include <iostream>
    #include <string>
    #include <string.h>
    #include <new>
    #include <stdlib.h>
    #include <exception>
    using namespace std;
    struct Tnode
    {
            char word[64+1];
            int count;
            Tnode* left;
            Tnode* right;
    };
    class Tree
    {
            public:
            Tnode* addNode(Tnode* root, string word);
            void printTree(Tnode* root);
            void alphabeticPrintTree(Tnode* root);
            void bprintTree(Tnode* root);
            void freeTree(Tnode* root);
    };
    
    Tnode* Tree::addNode(Tnode* root, string word)
    {
            Tnode* node;
            if (!root)
            {
                    node = new Tnode;
                    size_t copyLen = min(sizeof(node->word)-1, word.siz());
                    strncpy(node->word, word.c_str(), copyLen);
                    node->word[copyLen] = '\0';
                    node->left = 0;
                    node->right = 0;
                    node->count = 1;
                    return (node);
            }
            int cmp = strcmp(word.c_str(), root->word);
            if (cmp < 0)
            {
                    node = addNode(root->left, word);
                    if(!root->left)
                    {
                            root->left = node;
                    }
            }
            else if (cmp > 0)
            {
                    node = addNode(root->right, word);
                    if(!root->right)
                    {
                            root->right = node;
                    }
            }
            else
            {
                    root->count++;
            }
            return (node);
    }
    
    ...
    
    int main(int argc, char* argv[])
    {
            Tree tree;
            Tnode* root = 0;
    
            string word;
    
            try
            {
                    for(int i = 1; i < argc; i++)
                    {
                            word = string(argv[i]);
                            Tnode* node = tree.addNode(root, word);
                            if (!root)
                            {
                                    root = node;
                            }
                    }
                    cout << "Tree:" << endl;
                    tree.printTree(root);
                    cout << "Alphabetical:" << endl;
                    tree.alphabeticPrintTree(root);
                    tree.freeTree(root);
            }
            catch (exception& e)
            {
                    cout << "Standard exception: " << e.what() << endl;
            }
            return (0);
    }
    Thank you in advance.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Joe1903 View Post
    I want to replace the "string word" within the code below with "const char * word".
    Why would you do that?
    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.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    Quote Originally Posted by Elysia View Post
    Why would you do that?
    I want to get familiar with that construst (const char*). But when I just replace it,it doesnt work.Do you know what I am fdoing wrong?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Joe1903 View Post
    I want to get familiar with that construst (const char*).
    Why would you want to do that?

    Quote Originally Posted by Joe1903 View Post
    But when I just replace it,it doesnt work.Do you know what I am fdoing wrong?
    Yes, I do. const char* is neither a class nor a struct--it doesn't have member functions. Look closely at the line where the compiler is complaining. What can you see?
    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.

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    84
    You mean that word has arguments?Could you pls give me an example with just 1 line?

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    You can't simply replace instances of std::string (a class type) with const char * (a basic pointer type). This is not a good way to "get familiar with" pointers, whether pointers to char or int or whatever. The semantics are not the same. Also, std::string is NOT based on char * but on a template type CHAR_TYPE which could be basically any C++ type that fits the bill.. usually a integral type capable of supporting all Unicode code points, or some encoding of that.. maybe UTF-8, most likely.

    What is your goal here, to learn about std::string implementations or to learn about char pointers or pointer types in general? They are completely different things.

    Ammendum:

    If you want to get familiar with strings of characters impemented as char* types, then I suggest you ditch C++ and go to straight C. Learn how strings are implemented as char arrays (or equivalently, null-termined char pointers) in the C language. Also check out the man pages for and include <string.h>. Then make a note of how antiquated this idea that all characters in an alphabet of all languages in the world, could be represented in a byte. Then check out Unicode, which retifies that problem, though probably not optimally, certainly capably. Then you'll see why using char* is really better described as using byte* and is NOT a replacement for std::string
    Last edited by MacNilly; 11-28-2016 at 08:15 AM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MacNilly View Post
    If you want to get familiar with strings of characters impemented as char* types, then I suggest you ditch C++ and go to straight C. Learn how strings are implemented as char arrays (or equivalently, null-termined char pointers) in the C language. Also check out the man pages for and include <string.h>. Then make a note of how antiquated this idea that all characters in an alphabet of all languages in the world, could be represented in a byte. Then check out Unicode, which retifies that problem, though probably not optimally, certainly capably. Then you'll see why using char* is really better described as using byte* and is NOT a replacement for std::string
    It's not necessary to ditch C++ just to mess around with char arrays.
    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
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by Elysia View Post
    It's not necessary to ditch C++ just to mess around with char arrays.
    True, but at least there won't be any possibility of confusing with C++ strings..

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Changing language won't eliminate the possibilities of confusion. I'd say it's unlikely to help at all.
    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
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Ok, then the OP can stay with C++ but the differences between char* and std::string; conceptually, arrays of bytes and sequences of characters that I mentioned are still relevant whether C or C++ or any other language.

    EDIT: For me at least, I would rather learn in an environment where the options were more limited. Then I would hit the walls faster, and thus realize why programming is so complex in the first place. Gotta start somewhere..
    Last edited by MacNilly; 11-28-2016 at 10:06 AM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Programming is plenty complex without introducing artificial limitations due to lacking language features.
    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 MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    I would counter with...

    Given the fact that programming a solution to a complex problem is already complex (prior to introducing the machine-human interface, i.e., programming languages), it is better to have less features in a programming language, rather than a plethora of features that overlap one another, as in C++ (and other languages, I'm not dissing C++ here...). However, the features of C++, IMHO is quite overly complex for what you reap in benefits for the learning it takes to use it properly and optimally.

    Now, I know I just did not start a C++ debate with Elysia.

    I just thought the OP could use a little history lesson on how C's naive (but adequate for the time!) char* implementation of human language text relates to a modern language like C++ (or any language that supports Unicode, for that matter). I'm sure you'll mention something about "abstraction" and all that, but I think it is critical to understand the reasons for abstraction and also to understand a bit about the rationale behind the abstractions; why they are present in something like std::string. Only then will someone like the OP understand why std::string exists and how it is irrelated (except at the implementation level) to char*...

    Oh, this is not a thread hijack.. I think (to the OP) that you should just use std::string and forget const char* arrays, if your use case is a sequence of human language text. If you just want an array of bytes, then don't use std::string for that, just use straight byte arrays...

    EDIT:
    Crap.. one more edit.. it wasn't the C language (or any programming language) that decided on 8 (well, really 7.. gotta have the error correction bit...) bit characters to rule the world.. that was America, better known as ASCII .. American Standard Code of International something or another lol... Now Unicode is the one ring to rule them all...
    Last edited by MacNilly; 11-28-2016 at 12:56 PM.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MacNilly View Post
    Given the fact that programming a solution to a complex problem is already complex (prior to introducing the machine-human interface, i.e., programming languages), it is better to have less features in a programming language, rather than a plethora of features that overlap one another, as in C++ (and other languages, I'm not dissing C++ here...). However, the features of C++, IMHO is quite overly complex for what you reap in benefits for the learning it takes to use it properly and optimally.
    Well, I'll disagree. You can simply ignore the complicated parts of the language and use something a little suboptimally, but still far better than with less abstractions. Even if you have a hundred tools in your toolbox, and only use a few of them, it's still better than using only a single tool to do your job.

    I just thought the OP could use a little history lesson on how C's naive (but adequate for the time!) char* implementation of human language text relates to a modern language like C++ (or any language that supports Unicode, for that matter). I'm sure you'll mention something about "abstraction" and all that, but I think it is critical to understand the reasons for abstraction and also to understand a bit about the rationale behind the abstractions; why they are present in something like std::string. Only then will someone like the OP understand why std::string exists and how it is irrelated (except at the implementation level) to char*...
    Knowing abstractions is good, I agree with you. But there's no reason to know char*. There are plenty of other abstractions of higher-level that need be taught and used.
    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.

  14. #14
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by Elysia View Post
    Well, I'll disagree. You can simply ignore the complicated parts of the language and use something a little suboptimally, but still far better than with less abstractions. Even if you have a hundred tools in your toolbox, and only use a few of them, it's still better than using only a single tool to do your job.
    Thats fine, but who knows how to pick the right tools, or .. when faced with so many, simply picks the first that "works"? Perhaps C or C++ is not even the right tool for the job. I think maybe C++ programmer like yourself is like a neurosurgeon who knows his (or her) tools so well.

    I mean, I went over the source code to the C++ collections framework that's included in my linux distro thats a GCC specific distro (I forgot what its called). It's old, granted, but it came with a design documentation.. and it's very sound.. super efficient and flexible library... but the code.. my GOD, it's complexly makes it unreadable.. too much use of template classes, traits structs, and preprocessor magic.. I'm sorry, it's not designed for mortals. We can't understand that. I'll take a Java HashMap any day, even if its not as "optimal" as that, LOL.

    Knowing abstractions is good, I agree with you. But there's no reason to know char*. There are plenty of other abstractions of higher-level that need be taught and used.
    No, there are many a reason to know char* semantics, even given std::string... I think that a big problem is programming to abstractions that aren't understood.. and there is always the leakly abstraction layer.. it's not possible to implement a non-leaky abstraction; some things must be known about the implementation because it affects the client.. even the STL realized this (especially in terms of big-O complexity of operations), but its not possible to encode or enforce that into the language.

    For example, think about the painter who always keeps his paint at one point, getting slower and slower every day... (there's a name for that idiot, I forgot now)... but you won't use a generic class List that doesn't specify it's lookup time .. list.get(i).. if it was O(N).. when you have a large amount of data... I'm saying that you must know something about the implementation of List in order to use it effectively and practically. It could mean the difference between taking 10 seconds or 10 years on a data set; however, this requires a knowledges of the implementation.. it cannot be hidden behind an abstraction layer.
    Last edited by MacNilly; 11-28-2016 at 01:22 PM.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    But there's no reason to know char*.
    I disagree, you need to know about char* even if you never use them in your own code.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. replace char by string
    By rveger in forum C Programming
    Replies: 13
    Last Post: 09-04-2011, 03:29 PM
  2. Best way to return a const char* from a string
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2009, 08:44 PM
  3. String and const char *
    By sawer in forum C++ Programming
    Replies: 5
    Last Post: 03-05-2006, 02:16 AM
  4. Convert Const Char * to string
    By winsonlee in forum C++ Programming
    Replies: 3
    Last Post: 08-25-2004, 02:38 PM
  5. const char[] or string?
    By prog-bman in forum C++ Programming
    Replies: 10
    Last Post: 07-11-2004, 06:13 PM

Tags for this Thread