By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,168 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,168 IT Pros & Developers. It's quick & easy.

Question about char[] and char* in C++

Nepomuk
Expert 2.5K+
P: 3,112
Hi there!
I was trying something with strings in C++ and ran into a problem.
I wanted to write a program to do the following task: Read two "string" inputs and combine them into a third "string". Now, in the book I'm using, only char arrays and char pointers were used so far, so I wanted to solve the task with that.
I came up with the following solution:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. int main()
  5. {
  6.         char a[41];
  7.         char b[41];
  8.         char c[81];
  9.         std::cout << "Please enter 2 words:\n";
  10.         std::cin >> a;
  11.         std::cin >> b;
  12.         std::cout << "You entered " << a << " and " << b << ".\n";
  13.         std::strcpy(c,a);
  14.         std::strcat(c," ");
  15.         std::strcat(c,b);
  16.         std::cout << "Together that is: \"" << c << "\"\n";
  17. }
Now, I'm not really happy with the solution, because it limits each input to 40 characters.
I also tried something like the following:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. int main()
  3. {
  4.         char * a;
  5.         char * b;
  6.         char * c;
  7.         std::cout << "Please enter 2 words:\n";
  8.         std::cin >> a;
  9.         std::cin >> b;
  10. ...
It compiles fine, but when running it, I get a Segmentation fault.
Now, I'm assuming this is because I never declared a, b and c, just defined them. My question is: What can I do? I don't want to use the string class just yet, I'd prefer to understand the basics and be able to write a string class myself.

Greetings,
Nepomuk
Jun 26 '08 #1
Share this Question
Share on Google+
6 Replies


Expert 10K+
P: 11,448
It compiles fine, but when running it, I get a Segmentation fault.
Now, I'm assuming this is because I never declared a, b and c, just defined them. My question is: What can I do? I don't want to use the string class just yet, I'd prefer to understand the basics and be able to write a string class myself.
You have declared and defined those three pointers. But you didn't make them
point to a valid piece of memory hence the segmentation fault.

C is quite primitive when it comes to user input, i.e. it has no language facilities
for it, just a few functions. The fgets function allows you to read up to a max
number of chars or until a \n was read. You can use that knowledge and allocate
another char buffer if no \n was read yet. At the end you optionally have to glue
the several buffers together or adjust its size (get rid of the unused surplus).

Welcome to the 1970s and 1980s when those worms were invented ;-)

kind regards,

Jos
Jun 26 '08 #2

Nepomuk
Expert 2.5K+
P: 3,112
C is quite primitive when it comes to user input, i.e. it has no language facilities for it, just a few functions. The fgets function allows you to read up to a max number of chars or until a \n was read. You can use that knowledge and allocate another char buffer if no \n was read yet. At the end you optionally have to glue the several buffers together or adjust its size (get rid of the unused surplus).
Can't I do that with something like
Expand|Select|Wrap|Line Numbers
  1. char a[1024];
  2. cin >> a;
  3. char c[strlen(a)];
Of course, that still makes it limited to a certain size. (In this case: 1023 characters)
By the way, I checked on fgets and it seems to be thought for file input. The appropriate would be gets, I would guess, but that seems to be depreciated. (g++ gives a warning: "the `gets' function is dangerous and should not be used.")

Is it possible to read a certain amount of characters and then, if there's any left, read more? So something like:
Expand|Select|Wrap|Line Numbers
  1. char a[1024];
  2. cin >> a;
  3. char c[strlen(a)];
  4. strcpy(c,a);
  5. while(there is still input)
  6. {
  7.    cin >> a;
  8.    strcat(c,a);
  9. }
Greetings,
Nepomuk
Jun 26 '08 #3

Expert 100+
P: 671
I for one, would suggest you get a better C++ book, and then stick to the C++ library like std::string and std::copy. The C subset would be the more advanced portion.
Jun 26 '08 #4

Nepomuk
Expert 2.5K+
P: 3,112
I for one, would suggest you get a better C++ book, and then stick to the C++ library like std::string and std::copy. The C subset would be the more advanced portion.
Thanks for the suggestion, I must admit, the book I have ("C++ - Objektorientiertes Programmieren von Anfang an" by a german author called "Helmut Erlenkötter") is not very good in terms of actual C++, but didactically it's much easier than the other one I have ("The C++ Programming Language Special Edition" by "Bjarne Stroustrup", the creator of C++) and at the moment, I'd rather not spend any more money on books. (And I do want a printed book for the moment.)
I'm planing to finish the Erlenkötter and then get on to Stroustrup. Maybe use an on-line book (e.g. Addison Wesley's "C++ in Action") too.

But as I said, I'd like to try to solve the questions in the book with the material given in the book.
By the way, the book offers a solution like the one I presented first.

Oh, if you're in the mood to suggest an other book, feel free to do so.

Greetings,
Nepomuk
Jun 26 '08 #5

Expert 100+
P: 671
By the way, I checked on fgets and it seems to be thought for file input
The console input is bound to stdin, so you can use stdin as you would any other file descriptor (the FILE * argument).

What I would have done? Use a std::string. And the use getline(cin, nameOfString). getline has a return value, so you can use that in the while loop test.
Jun 26 '08 #6

Expert 100+
P: 671
but didactically it's much easier than the other one I have ("The C++ Programming Language Special Edition" by "Bjarne Stroustrup", the creator of C++
Much easier I think. I see recommendations for TC++PL sometimes being given out to beginners to the language, and I find that highly inappropriate. It’s designed as an advanced reference book, really.

I'm planing to finish the Erlenkötter and then get on to Stroustrup. Maybe use an on-line book
The Erlenkotter seems to be the type that teaches the C subset of C++. As long as you’re aware that C++ programming looks very different, sure, stay with it. You have to learn the material sometime or other, so you aren’t wasting your time by learning it early on. I actually don’t think going to Stroustrup right afterwards will help you. And on-line books are universally poor. Except for maybe one or two.

But as I said, I'd like to try to solve the questions in the book with the material given in the book.
When you learn the full C++ way to go about things, it’s worth going back to these questions and solving them the C++ way (iterators, std algorithms, RAII, and all the fun stuff) or just righting small programs.

Oh, if you're in the mood to suggest an other book, feel free to do so.
Sure. http://rudbek.com/books.html is pretty accurate. I also add C++ Primer, (4th Edition), by Stanley Lippman to the top of the list. Given that you have the Stroustrup book, and you want to save money, and that you are already reading from another C++ book, Koenig’s Accelerated C++ book may be the most useful for you. It’s designed more like a course, and it will teach you how to think in C++ (versus C).
Jun 26 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.