C++ character arrays

A

arnuld

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
 
M

Michael

/* 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
 
A

arnuld

Michael said:
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 ..."

thanks Michael

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

Kai-Uwe Bux

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
 
A

arnuld

arnuld said:
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
 
A

arnuld

arnuld said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top