473,691 Members | 3,091 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 7774
/* 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
2682
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 and less concats echo('wkjksdbjvsdnklvsDVL'.$a.'vfaadf');
43
2708
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, when I tried to write some functions and do some
8
7388
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
14140
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: #include "stdio.h" #define SIZE 5
8
11872
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
4073
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
3017
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 to save the NULL character /*!
4
7805
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 in hydrob and hydrop then they shuld be given a separate colour. for (int...
3
3526
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
2040
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
8951
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8791
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8794
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6457
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5813
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2965
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
3
1952
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.