LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-22-2006, 09:23 PM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
Is it really worth it to use chars, especialy since I can strings?


I'm getting tired of using chars, though, I know strings are just arrays of chars, it is just a lot easier for me pass arguements and copy one string to another, and do all other sorts of complex string manipulation that otherwise would have been too time consuming with chars.

From what I understand, c has libraries that mimic (or are. someone clarify?) c++ libraries. (such as stdlib.h, which [from what I understand] is cstdlib, or vice versa) So I may just be able to use a library (if one exists) that can deal with strings, in c, if I ever have to code in it.

My questsion is: Is it really needed that I use chars (I'm still going to learn more I can about it, along with c/c++ in general), or do you think that I could get by in many(if not most) programs with just string types. I realize that functions in SDL seem to need char*'s, but I know how to convert a string into a char, so that should not be a problem for me.
 
Old 07-22-2006, 09:27 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
C++ has a string class. As C lacks support for classes, I'm not sure exactly what type of string you have in mind.

That being said, if working with c-strings (char arrays/pointers) is too troublesome for you, you might wish to explore just using C++ or some other higher-level language. Is there a particular reason for using straight C?
 
Old 07-22-2006, 11:25 PM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
if you are using c++ you are better off to use std::string as much as possible. it is safer, and writes alot quicker. there is no reason that you have to use c strings, and if there was some reason that came up, like you said you know how to convert a std::string to a null terminated string so you would be ok.
 
Old 07-22-2006, 11:52 PM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
As an aside note, the std::string class stores data internally as a cstring and internally uses much the same functions as you would use manipulating a cstring (strcpy, etc.). Its advantage revolves around the fact that buffer management is handled and it reduces the risk of off-by-one errors.
 
Old 07-23-2006, 06:19 AM   #5
blackzone
Member
 
Registered: Jun 2004
Posts: 256

Rep: Reputation: 30
I'm not sure what your question is but in "c" you always use characters.

String is just an array of characters...

There are function such as strcpy, strcat, strlen which is just functions which is used to manuplate array of characters. You never need to do any sort of characters.

I'm not familiar with c++, so I can't comment on that.
 
Old 07-23-2006, 12:38 PM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
From what I understand, c has libraries that mimic (or are. someone clarify?) c++ libraries. (such as stdlib.h, which [from what I understand] is cstdlib, or vice versa) So I may just be able to use a library (if one exists) that can deal with strings, in c, if I ever have to code in it.
sorry i overlooked this part earlier.. actually you have it backwards, the cstdlib is a c++ version of stdlib.h, which is just stdlib.h in the std namespace.. the same as cstring is string.h, ctime, cmath, etc..

you can make c++ use c style programming, but there is no way to make c use the functionality of c++.. when you introduce c++ to c, you no longer have c, you have c++.. but once again like i mentioned before, if c++ is an option, then by all means use all the things it has to offer.. including std::string and the STL.. it makes life much easier and safer..
 
Old 07-23-2006, 12:52 PM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by RHLinuxGUY
Is it really worth it to use chars, especialy since I can strings?
Before the poor char get discarded to the wastebin of life, it still has a valid role when you only want a single character. Otherwise if you are using C++ then yes, by all means, go with the string class. However character arrays can be useful if you are doing very little in the way of manipulation and you wish to avoid the overhead of a class.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
international chars awl Slackware 8 09-01-2005 01:17 PM
Fun with strings & chars in C Scrag Programming 4 05-19-2004 03:03 PM
chars } and $ won't work.. among others.. apax Linux - Newbie 8 11-26-2003 09:20 AM
Red hat runs kinda slow especialy the games.... SaVien Linux - General 4 02-07-2003 05:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration