c and c++ string

This is a discussion on c and c++ string within the C Programming forums, part of the General Programming Boards category; umm just want to ask about the basic data types.... cause in c the declaration of string is like this ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    40

    c and c++ string

    umm just want to ask about the basic data types....
    cause in c the declaration of string is like this
    Code:
    char str[size];
    and in c++
    Code:
    String str;
    my question is how c++ allocates some space in storing characters in data type string?
    cause in c we all knew we should declare how many characters will be allocate and the
    problem with c strings is the SIZE for example if the SIZE is equal to 5 then we input
    more than 5 characters then we are sure that there would happen unusual unlike in
    c++...just want to know how the c++ syntax works...i hope my questions is clear..
    thanks

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    It's actually "string" or "std::string" and long answer short - it's a class.
    Internally, it uses a C-style string but with dynamic memory management.
    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
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sick View Post
    my question is how c++ allocates some space in storing characters in data type string?
    cause in c we all knew we should declare how many characters will be allocate and the
    problem with c strings is the SIZE for example if the SIZE is equal to 5 then we input
    more than 5 characters then we are sure that there would happen unusual unlike in
    c++...
    I actually don't know C++ so I don't know the syntax for that, but I do know that most languages (other than C) have a "string" datatype. C does not have a "string" datatype, a "C string" is a char array. Also, C arrays are not dynamic, so their SIZE must be defined at compile time.

    This is an aspect of the "low-levelness" of C, since you could implement a dynamic array or String datatype using low-level procedures:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef char* String;
    
    String newString (char *content) {
    	String new=malloc(strlen(content)+1);
    	strcpy(new,content);
    	return new;
    }
    
    int main() {
    	String eg=newString("hello world");
    	printf("%s\n",eg);
    	free(eg);
    	return 0;
    }
    Notice you can use this String datatype uncast with string functions since it is just a char pointer. However, it is committed to being a heap variable and so must be free()'d, and does not resize itself dynamically (you could write functions for that too, but probably the normal string functions will do).

    Other languages which do have such a datatype use routines like this internally to support them. For example, in C++ a String is an object. If you wade through the source for your C++ library or something you should be able to find the exact code which creates Strings and it probably has a char* somewhere in it...as Elysia implies.
    Last edited by MK27; 08-02-2009 at 09:28 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    It's better to say that "string" is a class, because an object is an instantiated class. And it's not "String," it's "string" since C++ is also case sensitive.
    And yeah, std::string pretty much works like that. It also keeps track of size.
    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
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    The std::string class is of the same class as std::basic_string. Also using char *string is more common than using a character array in C.
    Last edited by P4R4N01D; 08-03-2009 at 02:35 AM.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by P4R4N01D View Post
    Also using char *string is more common than using a character array in C.
    *string is a pointer to a character array.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by P4R4N01D View Post
    The string class is an instance of basic_string, which is a class.
    No it isn't. std::string is a typedef for std::basic_string<char, char_traits<char>, allocator<char> > not an instance.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    umm how about i use that function in input?? umm i think it couldn't be use right?? or it can be? thanks for enlightening me.......

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    umm btw what is the new keyword??

  10. #10

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,933
    Quote Originally Posted by sick
    umm btw what is the new keyword??
    Are you learning C or C++? If you are learning C, then do not worry about it since it will not concern you. If you are learning C++, then ask your question in the C++ forum (but you should be reading a tutorial instead of asking such a question). If you are trying to learn both simultaneously, then you are better off picking one to learn first.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Elysia View Post
    It's actually "string" or "std::string" and long answer short - it's a class.
    Internally, it uses a C-style string but with dynamic memory management.
    That's poor of you to make assumptions about its implementation

  13. #13
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    it's a class.
    I thought std::string is in fact a typedef for a template specialization of basic_string (I think it's std::basic_string<char, std::char_traits<char>, std:.allocator<char> >). Please correct me if I'm wrong, I just gotta revise all this stuff like basic_* and ios_base.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    Quote Originally Posted by zacs7 View Post
    That's poor of you to make assumptions about its implementation
    I suppose, but I don't see many implementations doing it differently.
    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.

Popular pages Recent additions subscribe to a feed

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