-
January 5th, 2009, 11:47 AM
#1
char* name
Hi,
I am a newbie to c++ and I am confused when to use char* name. Can someone please explain me when to use char*
-
January 5th, 2009, 11:54 AM
#2
Re: char* name
Have you learned pointers yet? If not, it's easiest to think of a char* as a char array of unspecified length. (However, simply declaring a char* will *not* get you that array----you have to ask for it. Common mistake.)
A char* is often called a "C-style string", since it's usually used as the pointer to the first letter of a NULL-terminated character array representing a string. However, in C++ it's far easier and less error-prone to simply use a std::string instead of a char*.
-
January 5th, 2009, 12:00 PM
#3
Re: char* name
Use char* when you require a pointer to a char.
Code:
// Single char.
char c;
char *pc = &c;
// Array of char.
char a[10];
pc = &a[0]; // Pointer to the first element in the array.
pc = a; // Same as above.
// Dynamically allocated.
pc = new char [10];
delete pc;
Don't use char arrays for strings in C++, use std::string instead.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
January 5th, 2009, 12:02 PM
#4
Re: char* name
Thanks Lindley,
I am well versed with pointers but I got confused between the different style of declaring pointer
as
char* b
char * b
char *b
Can you please explain me the difference between the three too....
and how can we use std::string in a c++ programme
-
January 5th, 2009, 12:05 PM
#5
Re: char* name
There is no difference between those three things. And std::string is very intuitive to use, but here's a reference:
http://www.cplusplus.com/reference/string/string/
-
January 5th, 2009, 12:21 PM
#6
Re: char* name
Hey,
I took your advise and started writing a program using std::string it will not only save my program size with also save dynamic allocation problem of using char*, but the document I got does not inform me what lib file should I use and I will declare sub::string in the header file....so what should be the lib file.
-
January 5th, 2009, 12:24 PM
#7
Re: char* name
std::string is part of the C++ standard library. You need to #include <string>.
-
January 5th, 2009, 12:25 PM
#8
Re: char* name
All you need to do is #include <string> and (optionally) add
Code:
using std::string;
//or
using namespace std;
to your cpp file. (Don't put using statements in h files, though.) Either of these will allow you to simply declare "string name" rather than "std::string name".
-
January 5th, 2009, 12:53 PM
#9
Re: char* name
Hi,
I am still getting confused maybe since I am seeing a code after so many days maybe that is why
can someone please explain me public, private, protected declaration and it is private declaration be default.
What is int virtual
Code:
int virtual okSegment(Segment *t);
Last edited by Shapy_shockerz; January 5th, 2009 at 12:56 PM.
-
January 5th, 2009, 12:58 PM
#10
Re: char* name
Originally Posted by Shapy_shockerz
Hi,
I am still getting confused maybe since I am seeing a code after so many days maybe that is why
can someone please explain me public, private, protected declaration and it is private declaration be default.
What is int virtual
Code:
int virtual okSegment(Segment *t);
Don't confuse this board with documentation. MSDN and dozens of books all explain private, protected, public and virtual. RTFM!!!
-
January 5th, 2009, 01:45 PM
#11
Re: char* name
I know there are documentation on all this but I need hand on experience on this too..
Please someone help me out with how to use atoi() and what it is?
-
January 5th, 2009, 01:46 PM
#12
-
January 5th, 2009, 01:48 PM
#13
Re: char* name
Originally Posted by Shapy_shockerz
I know there are documentation on all this but I need hand on experience on this too..
Please someone help me out with how to use atoi() and what it is?
Then go get a teach yourself C++ book and work through it. We're not here to teach you the language. We're here to help when you get stuck. That means you take a shot at it and we'll help you find where you went wrong.
As to atoi, go look it up and ask specific questions about what you don't understand.
-
January 5th, 2009, 01:52 PM
#14
Re: char* name
Originally Posted by Shapy_shockerz
I know there are documentation on all this but I need hand on experience on this too..
It is virtually impossible to learn C++ taking the "random tidbits" approach you are using. There is only one way to reliably learn C++
1) Get a GOOD tutorial oriented book. The paper kind.
2) Read every word, starting at page 1. No sknimming, skipping, browsing.
3) TYPE in every line of code. Even if there is a DVD/CD, MANUALLY type it in.
4) Step through EVERY line of code with your debugger (yes, even the trivial hello world sample)
5) Do NOT proceed past the end of a chapter until you are confident that you understand, and retained the information.
6) Write at least one program from scratch using the topic of the current chapter. Be sure to carefully step through every line with the debugger.
C++ is a very detailed language, and attempting to pick things up as you go will (at best) leave holes in your knowedge, and (typically) have you make bad assumptions that are later nearly impossible to unlearn.
I hate to count the number of times someone comes back with a problem about a very basic item, when told that it was in chapter 2 or 3 of the book that they did you, the response is "I skipped that because I already knew all about....."....OBVIOUSLY they did not.
Once you get a good book, and begin carefully studying the material, posting problems you may encounter can be very effective. By stating the book and page you are up to, people can phrase the response in a manner that should be understandable to a person who has carefully studied, and comprehended all of the material up to that point.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 5th, 2009, 01:53 PM
#15
Re: char* name
Hi
I am reading a code and it says to read a file we need the following command as it makes a matrix to read the code, I am not sure why is it atoi(argv[2])
Code:
n = atoi(argv[2]);
strncpy(&option,argv[4],1);
i am struck here please guide me as in wht this line would do
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|