468,504 Members | 1,986 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

C++ character arrays

i am trying to implement C style strings in C++ (from chapter 4 "C++
Primer 4/e"):

// reading from std::cin for a c-string
// (a null terminated character array)

#include <iostream>
#include <string>

int main() {
// reading c style character array from std::cin
const int arr_sz = 7;
char c;
int i = 0;
char ca[arr_sz];
for(char *pbegin = ca, *pend = ca + arr_sz;
pbegin != pend; ++pbegin)
{
if(i == 6)
*pbegin = '\0';
else
{
std::cout << "Enter a character: ";
std::cin >c;
*pbegin = c;
++i;
}
}

std::cout << "wrote the array, here is the output:\t";

// printing out array
for(char *pbegin = ca, *pend = ca + arr_sz;
pbegin != pend;
++pbegin)
{
std::cout << *pbegin;
}

std::cout << std::endl;

}
/* OUTPUT

unix@debian:~/programming$ ./a.out
Enter a character: A
Enter a character: R
Enter a character: N
Enter a character: U
Enter a character: L
Enter a character: D
wrote the array, here is the output: ARNULD^@
unix@debian:~/programming$

*/

i want to know what does the character "^@" represent, is this null
character, '\0', that i put in the code or something else. also this
is the output from "Emacs shell", BASH does not print this character.
Why?

thanks

-- "arnuld"
http://arnuld.blogspot.com

Oct 14 '06 #1
7 7528
/* OUTPUT
>
unix@debian:~/programming$ ./a.out
Enter a character: A
Enter a character: R
Enter a character: N
Enter a character: U
Enter a character: L
Enter a character: D
wrote the array, here is the output: ARNULD^@
unix@debian:~/programming$

*/

i want to know what does the character "^@" represent, is this null
character, '\0', that i put in the code or something else. also this
is the output from "Emacs shell", BASH does not print this character.
This is way off topic, but you could do something like this:
../a.out my_file.txt

od -c my_file.txt

This would show you the characters in a more readable format and let
you figure out what ^@ is. (It's quite probably \0, but that's
implementation dependent.)

Michael

Oct 14 '06 #2
Michael wrote:
This is way off topic,
oops! :-(
but you could do something like this: ./a.out my_file.txt
yes i did
od -c my_file.txt
i did it too :-)
This would show you the characters in a more readable format and let
you figure out what ^@ is. (It's quite probably \0, but that's
implementation dependent.)
yeah, it is '\0'

BTW, what is "od -c ..."
Michael
thanks Michael

- arnuld
http://arnuld.blogspot.com

Oct 14 '06 #3
arnuld wrote:
BTW, what is "od -c ..."
hey, i just used "man" page & came to know that it dumps files in
different formats, i even used "od -x ..." options. wow, UNIX has lots
of small but useful & important utilities.

thanks Michael

- arnuld
http://arnuld.blogspot.com
Oct 14 '06 #4
arnuld wrote:
[snip]
BTW, what is "od -c ..."
It's a unix utility. Following the long standing tradition of choosing very
short, non-memorizable names for utility programs, this one has been
named "od" because it is Off Dopic in this group. If you do "man od", you
will find a slightly different explanation, namely that od stands
for "octal dump".
Best

Kai-Uwe Bux
Oct 14 '06 #5
arnuld wrote:
i am trying to implement C style strings in C++ (from chapter 4 "C++
Primer 4/e"):

// reading from std::cin for a c-string
// (a null terminated character array)

#include <iostream>
#include <string>

int main() {
// reading c style character array from std::cin
const int arr_sz = 7;
char c;
int i = 0;
char ca[arr_sz];
for(char *pbegin = ca, *pend = ca + arr_sz;
pbegin != pend; ++pbegin)
{
if(i == 6)
*pbegin = '\0';
else
{
std::cout << "Enter a character: ";
std::cin >c;
*pbegin = c;
++i;
}
}

std::cout << "wrote the array, here is the output:\t";

// printing out array
for(char *pbegin = ca, *pend = ca + arr_sz;
pbegin != pend;
++pbegin)
{
std::cout << *pbegin;
}

std::cout << std::endl;

}

is there any better way to insert '\0' into the character array?

-- "arnuld"
http://arnuld.blogspot.com

Oct 14 '06 #6
On 14 Oct 2006 02:42:35 -0700 in comp.lang.c++, "arnuld"
<ar*****@gmail.comwrote,
>arnuld wrote:
>BTW, what is "od -c ..."

hey, i just used "man" page & came to know that it dumps files in
different formats, i even used "od -x ..." options. wow, UNIX has lots
Compare with:
http://groups.google.com/gr*********....earthlink.net

Oct 14 '06 #7
David Harmon wrote:
hey, i just used "man" page & came to know that it dumps files in
different formats, i even used "od -x ..." options. wow, UNIX has lots

Compare with:
http://groups.google.com/gr*********....earthlink.net
well, i did not understand it. all i can understand is 3 things:

1.) this programme takes input from "std::cin".
2.) outputs some vales to "std::cout" in "hexadecimal" form.
3.) uses some "arrays" to do that.

-- arnuld
http://arnuld.blogspot.com

Oct 14 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

43 posts views Thread by Anitha | last post: by
8 posts views Thread by Brand Bogard | last post: by
14 posts views Thread by Shhnwz.a | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.