C++ char array

JC0133

Senior member
Nov 2, 2010
201
1
76
I have not used a char array in close to two years. I also have not coded in C++ in a while so I think I keep making dumb mistakes. I am just practicing converting char array to strings now.

I was running a few tests cases and I don't understand why some cases work and a couple do not.

I was trying to convert a string to a char array and I was trying to get the first 4 chars of a string and store it in a char array of size 4.

First off, why does this not work?

char testA[4];
testA[0] = "a";

It gives me an error in VS.

Next why does this work if the above case doesn't?

for (int i = 0; i < line.size(); i++) {

testA = line;
}

If I do this below, it will give me the first 4 characters but then more characters of gibberish behind it?

cout << testA <<endl;
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,284
3,905
75
"a" is a char array string. 'a' is a character.

Char array strings end in a 0, or '\0'. Cout keeps reading one until it finds that character.
 
Reactions: NTMBK

you2

Diamond Member
Apr 2, 2002
5,763
981
126
The below code should not compile:

for (int i = 0; i < line.size(); i++) {

testA = line;
}

-
If it does you are using a non-standard version of c++
 

Cerebus451

Golden Member
Nov 30, 2000
1,425
0
76
The below code should not compile:

for (int i = 0; i < line.size(); i++) {

testA = line;
}
-

If it does you are using a non-standard version of c++
The forum software is eating the [ i ] and creating the italics. The code likely looks like this:
Code:
for (int i = 0; i < line.size(); i++) {

testA[i] = line[i];
}
which has its own issues if line is longer than 4 characters but will compile perfectly fine.
 

mv2devnull

Golden Member
Apr 13, 2010
1,503
145
106
As said/implied, the "a" in code is a literal string constant. Somewhere in the binary and in the constant memory area of the runtime there will be an array of two characters that contains these:
Code:
{ 'a', '\0' }
Now that you write
Code:
foo[0] = "a";
the actual instructions attempt to assign the address (a const char*) of that literal constant, i.e. the address of the first character of that array to 'foo[0]'.

The 'foo' was an array of characters. Therefore, the foo[0] is a char. The compiler error message probably did say that it cannot convert 'const char*' into 'char'.


The operator << is overloaded; there are many versions for various types. One of them takes 'const char*' as its right operand. Furthermore, that version assumes that the pointer points to a C-string. C-string is an array of 'char' that has a null, '\0', as end-marker. The operator outputs every char from the array, up to the null.

An array decays to pointer in some situations.
Code:
char foo[4];
std::cout << foo;
The type of foo is array of char, but there is no << for arrays. There is the << for const char*.
On that output statement the foo decays to a pointer to its first element; converts to char*.
The char* converts to const char* as that what the << expects. (There are other <<, but this is the closest.)


That said,
You are looking char arrays and C++? Why? Why plain arrays of any type in modern C++?
There are std::string, std::array, std::vector, etc. Plenty of standard components to use in C++ code. More versatile and flexible and way less error-prone.
 

JC0133

Senior member
Nov 2, 2010
201
1
76
Thank you so much, this was helpful. I know I could use std string. I am just trying to understand/learn on my own. Figured it would be a good exercise to read in strings and convert them to char arrays and back.
 

sweenish

Diamond Member
May 21, 2013
3,656
60
91
I am a huge fan of the site learncpp.com

I highly recommend it as a reference if you are looking to learn C++.
 

you2

Diamond Member
Apr 2, 2002
5,763
981
126
The only disadvantage of using std::string is that they are expensive (memory/cpu) relatively speaking. BUT this only matters in critical situations where the machine is maximized and instead of buying 50,000 machines optimization of the code reduce your needs to 25,000 machines (and yes I worked for a company that buys 100,000's of machines for processings and saving 20,000 machines has a noticeable impact on bottom line).
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
The only disadvantage of using std::string is that they are expensive (memory/cpu) relatively speaking. BUT this only matters in critical situations where the machine is maximized and instead of buying 50,000 machines optimization of the code reduce your needs to 25,000 machines (and yes I worked for a company that buys 100,000's of machines for processings and saving 20,000 machines has a noticeable impact on bottom line).

Depends on the implementation, CPU wise they are about the same cost as char[]s. Memory wise, it depends. Lots of compilers now optimize away the cost for small strings, often the only thing extra you are carrying along is the size member.
 

you2

Diamond Member
Apr 2, 2002
5,763
981
126
They can be a lot more expensive if you have to edit them esp with regards to memory fragmentation. yes you can make char arrays behave just as bad as you could implement std::string with char array but there are optimizations you can perform that will make them significantly cheaper.

Depends on the implementation, CPU wise they are about the same cost as char[]s. Memory wise, it depends. Lots of compilers now optimize away the cost for small strings, often the only thing extra you are carrying along is the size member.
 

mv2devnull

Golden Member
Apr 13, 2010
1,503
145
106
Is that relevant here?

Stage 1 of learning is to get the syntax right. That is all about the language that is used. Learning the syntax of std::string seems a lesser burden than getting plain arrays/raw pointers right.

Second stage is to be able to write logically correct and maintainable programs. This is the most vital part.

Sure, there are many ways to implement a particular task and some of them are more efficient than others. However, if you don't already master the first two stages, then the price is irrelevant.
 

you2

Diamond Member
Apr 2, 2002
5,763
981
126
I sort of agree and disagree. It is not directly relevant to his question and I suspect from the issue he is having it will never be relevant but it is a learning experience and I think one should be at least moderately aware of the performance of ones program bother linearly and asymptotic. Having said that there is both the performance of the code and the performance to the coder (ability to write correct code quickly). Then again with respect to the later some folks would claim that C/C++ is never the correct tool.

So yes I concede it is not relevant (here).

Is that relevant here?

Stage 1 of learning is to get the syntax right. That is all about the language that is used. Learning the syntax of std::string seems a lesser burden than getting plain arrays/raw pointers right.

Second stage is to be able to write logically correct and maintainable programs. This is the most vital part.

Sure, there are many ways to implement a particular task and some of them are more efficient than others. However, if you don't already master the first two stages, then the price is irrelevant.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
They can be a lot more expensive if you have to edit them esp with regards to memory fragmentation. yes you can make char arrays behave just as bad as you could implement std::string with char array but there are optimizations you can perform that will make them significantly cheaper.
Ehhh.. I'll have to disagree with you here. When you start talking about editing, std::string is likely going to beat or be the same as whatever you make using just char[]s. Especially if you are talking about appending editing (individual character editing will be about the same).

If you can plan out how big you want your char[] to be, you can also reserve that amount in std::string. But if you don't/can't know it, then most std::string's amortized growth strategies will end up costing you less than what dynamically growing/copying a char[] would.

This along with the size variable is why I said that memory might be more.

As for memory fragmentation, std::string is going to do better than growing char[]s here. Especially if you are constantly shrinking and growing the string. Primarily because of the amortized growth which will end up in less allocations than what a manually managed char[] would do (or, reserve which would put you in the same boat).

I think the only place where I would expect a char[] to significantly out perform a std::string is when it comes to stack allocation. For most other circumstances, they'll be on par with each other.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |