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.