Archived

This topic is now archived and is closed to further replies.

Fractal

char[???]...

Recommended Posts

"int"s I can understand, but what''s with the "char[particular number]"? Without the "[number]", "char" just doesn''t seem to work. Could anyone explain why, and how I could figure out the correct numbers to go inside the "[]" for a particular "char"?

Share this post


Link to post
Share on other sites
the numbers between [] in a "char" means the quantity of chars you want to have. With [] you define a vector of char, that you usually can use to work with char strings

Example:
I want to put my name in a vector of char, then i define:

char name[ 6 ] = "Marcos";

or i can say:

char site[ 7 ] = "Gamedev";

when you define a string the way i did above, the C++ compiler automatically puts a null character in the last position of the array to indicate the end of the string.

also, you could define an string in the following way:

char name[] = "Marcos";

if you don`t put any number inside the [] then the C++ compiler calculates automatically the number of positions needed in the array "name" to contain the string "Marcos" and puts in the final position a null character to indicate the end of the string

Bye
Good Luck
Marcos

Share this post


Link to post
Share on other sites
A "char" is just a basic integer type, consisting of only 1 byte and having a range from -127 to 128 for the signed type, and 0 to 256 for the unsigned type. A char[] is an array of these basic integer types. You can declare either:

  
char somevariable = 45;
char other = ''a'';
char yadayada[8] = {''y'', ''a'', ''d'', ''a'', ''y'', ''a'', ''d'', ''a''};


Recall that basic ASCII characters are converted to their respective ASCII codes in the source code, so a ''a'' is actually 65.

Now, a string is a special case of char array that is terminated with a 0. Strings aren''t just chars. Strings are arrays, and a char is an integer.

  
char somecharacter = ''a'';
char somestring[5] = {''s'', ''o'', ''m'', ''e'', ''/0''};


A string must always have the number of characters + 1 for the null terminating character as the size of the array. You can let the compiler count the number of character for you by doing this:

  
char * filenamestring = "foo.dat";


Then, filenamestring will point to the address where the first member of the string is stored, I.E. the address of ''f'' in this case.

I hope that answered your question

Paradigm Shift 2000

Share this post


Link to post
Share on other sites
quote:
Original post by marcos
the numbers between [] in a "char" means the quantity of chars you want to have. With [] you define a vector of char, that you usually can use to work with char strings

Example:
I want to put my name in a vector of char, then i define:

char name[ 6 ] = "Marcos";

or i can say:

char site[ 7 ] = "Gamedev";



Just a clarification... these are wrong. To store the string "Marcos", you need an array of seven chars; six to store the letters, and a seventh to store a null. Likewise, to store "Gamedev", you will need an array of eight characters.

It sounds like you aren''t quite clear on the concept of arrays, which C-style strings are based around. You should read up on those before attempting to do much stuff with strings.

Share this post


Link to post
Share on other sites