473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 7756
/* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: rl | last post by:
Hi out there, I'd like to know sth about the costs of a function call in php and the handling of character arrays (init size, enlargement steps of allocated memory, technique on enlargement ->...
43
by: Anitha | last post by:
Hi I observed something while coding the other day: if I declare a character array as char s, and try to use it as any other character array..it works perfectly fine most of the times. It...
8
by: lasek | last post by:
Hi all, a simple question, look at this code below: char acName="Claudio"; unsigned int uiLen; uiLen=strlen(acName); printf("Length of acName variable %u",uiLen); //uiLen >>>> 7
3
by: linguae | last post by:
Hello. In my C program, I have an array of character pointers. I'm trying to input character strings to each index of the character pointer array using scanf(), but when I run the program, I get...
8
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
14
by: mast2as | last post by:
Hi everyone, I am trying to implement some specs which specify that an array of parameter is passed to a function as a pointer to an array terminated by a NULL chatacter. That seemed fairly easy...
4
by: reva | last post by:
hi all!! can any one please help me in checking the two character arrays. in my code i need to compare a character array(seq) to that of hydrob and hydrop . if the seq has hydrob then it should be...
3
by: Tarik Monem | last post by:
Hi Everyone, Still a newbie with FLEX, and I've passed arrays using AJAX to FLEX before, but I've never passed links to FLEX. Basically, this is the OUTPUT, which I wanted, but I'm given an...
19
by: bowlderyu | last post by:
Hello, all. If a struct contains a character strings, there are two methods to define the struct, one by character array, another by character pointer. E.g, //Program for struct includeing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.