473,568 Members | 3,014 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7770
/* 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 "hexadecima l" 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
2674
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 -> full copy or virtual array spread in chunks over mem). The reason of my question is the following: // many function calls with smaller char arrays...
43
2676
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 holds strings of any length. I guess what is happening here is that this array initially holds only '\0' and hence is of length 1. But sometimes,...
8
7373
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
14130
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 segmentation faults and core dumps. The problem occurs when the program calls scanf(). I don't know what is wrong with it. Here is my code: ...
8
11858
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
4051
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
3006
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 to implement. I had a special Malloc function that would allocated the number of needed bytes for the objects i needed to store + 1 additional byte...
4
7801
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 given a different colour. if it has a hydrop then it should be assigned a different colour. if the sequence does not have a character that is neither...
3
3525
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 error of "illegal character," from the JavaScript console: Error: illegal character Source Code:
19
2028
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 character strings, test1.c #include <stdio.h> #define LEN 20 struct info {
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.