473,405 Members | 2,404 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,405 software developers and data experts.

array's

las
Hello, Im trying to figure out the difference between int[] and char[]

#include <iostream>
#include <stdlib.h>

int main()
{
int i[10] = {1};
char c[10] = {'1'};

cout << "&int \t" << &i << endl;
cout << "int \t " << i << endl;
cout << "int[0]\t" << i[0] << endl;
cout << endl;
cout << "&char \t" << &c << endl;
cout << "char\t " << c << endl;
cout << "char[0]\t" << c[0] << endl;
cout << endl;
system("PAUSE");
return 0;
}

I get this output :

&int 0x254fda8
int 0x254fda8
int[0] 1

&char 0x254fd98
char 1
char[0] 1

I belive long, float, double etc work the same way as int does in my above
example. How is char[] different for these other variables ? Any links to
related tutorials would be great aswell.

Cheers :)

Jul 19 '05 #1
4 4486
las wrote:
Hello, Im trying to figure out the difference between int[] and char[]
int[] is an array of integers.
char[] is an array of characters.
#include <iostream>
#include <stdlib.h>

int main()
{
int i[10] = {1};
char c[10] = {'1'};

cout << "&int \t" << &i << endl; <snip> I get this output :
That's interesting. On mine, it doesn't even compile.
&int 0x254fda8
int 0x254fda8
int[0] 1

&char 0x254fd98
char 1
char[0] 1

I belive long, float, double etc work the same way as int does in my above
example. How is char[] different for these other variables ?

<snip>

It isn't, apart from the syntactic sugar and standard libraries enabling
char arrays to be used as strings.

When you send an int, long, float or double to a stream, the output
defaults to the numerical value in decimal.

OTOH, when a char is sent, the output is the character itself, as
distinct from the numerical value, which is the ASCII (or other platform
character set) code of the character.

When a pointer is sent, the default is the memory address in
hexadecimal. The name of an array by itself denotes a pointer. The
exception is a pointer to char, IWC the char array is output as a
null-terminated string.

As for why &char and char gave different values, &c and c are the same
memory address but are of different types. They are char[10]* (pointer
to array of 10 chars) and char* respectively. This difference in
referent size means that incrementing a char* will increment by one
char, whereas incrementing a char[10]* will increment by a block of 10
chars.

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

Jul 19 '05 #2

"las" <la******@online.no> wrote in message
news:cE*******************@news4.e.nsc.no...
Hello, Im trying to figure out the difference between int[] and char[]

#include <iostream>
#include <stdlib.h>

int main()
{
int i[10] = {1};
char c[10] = {'1'};

cout << "&int \t" << &i << endl;
cout << "int \t " << i << endl;
cout << "int[0]\t" << i[0] << endl;
cout << endl;
cout << "&char \t" << &c << endl;
cout << "char\t " << c << endl;
cout << "char[0]\t" << c[0] << endl;
cout << endl;
system("PAUSE");
return 0;
}

I get this output :

&int 0x254fda8
int 0x254fda8
int[0] 1

&char 0x254fd98
char 1
char[0] 1

I belive long, float, double etc work the same way as int does in my above
example. How is char[] different for these other variables ? Any links to
related tutorials would be great aswell.

Cheers :)


Such a simple question, such a complex answer.

First your code is wrong. The only way to guarantee the behaviour you see is
to do this

char c[10] = {'1', '\0'};

This makes c a 'null terminated string' ('\0' is the null). In C++ arrays of
chars are often used as null terminated strings. operator<< knows this an
given an array of char will attempt (rightly or wrongly) to interpret it as
a null terminated string and print out all of the string. Gvien any other
sort of array operator<< will print out the address of the first element of
the array, that's what you see with your int array.

You might want to experiment with other values for c, e.g.

char c[10] = {'h', 'e', 'l', 'l', 'o', '\0'};

john
Jul 19 '05 #3


John Harrison wrote:
First your code is wrong. The only way to guarantee the behaviour you see is
to do this

char c[10] = {'1', '\0'};

This makes c a 'null terminated string' ('\0' is the null).

Nope nope nope. If you init any values in an array, the remainder of the
elements are initialized to the type-specific 0 value. So the addition
of the null-terminator here is superfluous.


Brian Rodenborn
Jul 19 '05 #4

"Default User" <fi********@company.com> wrote in message
news:3F***************@company.com...


John Harrison wrote:
First your code is wrong. The only way to guarantee the behaviour you see is to do this

char c[10] = {'1', '\0'};

This makes c a 'null terminated string' ('\0' is the null).

Nope nope nope. If you init any values in an array, the remainder of the
elements are initialized to the type-specific 0 value. So the addition
of the null-terminator here is superfluous.


OK, I stand corrected.

john
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
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...

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.