I really need help with char arrays , my teacher taught us but i didnt get to understand the whole concept , is there any book where I can easily understand it with some exercises to solve also?
Most books on C will include a section on arrays (char arrays are only special in that they are often used to store strings with a \0 as the terminating character). You can also do a search on C-strings.
Free C/C++ Resources
Recommended C/C++ Books
Free C/C++ Everything
Teach Yourself C++ in 21 Days (look at chapter 11 for arrays)
I can give you a quick tutorial.
Char arrays or any type of arrays it's just like a table with X elements that you define. Imagine that you want reserve a space in memory to hold 30 characters, then you define:
To access to an specific position in the char array and assign a value to it you should do:Code:char string[30];
This will assign an 'A' char to the 6th position of the char array (six because 0 also counts, so it's 5+1).Code:string[5] = 'A';
You should also know that you need to put the terminator character at the end of string, the terminator char is \0 (ASCII 0). So to assign any string to char array, like your name, you should do:
To easly use char arrays for input and output you should use functions like:Code:char name[30] = "ahmed\0';
puts() -> prints char array elements until finds the null char \0.
gets() -> gets the input and assign it to a variable (automatically puts char terminator at the end)
You can also use printf and scanf for strings too.
For char array's manipulation you should use the lib <string.h>, it contains functions for copy a array to another array, to compare char arrays, to get a char array length, and much more:
strcpy() -> copies the origin char array to destination char array.
strcmp() -> compares 2 char arrays.
strlen() -> gets the length of a char array (from first char to terminator char, not the full array lengt).
There is a quick example about using char arrays:
Code:#include <stdio.h> #include <string.h> #include <conio.h> #define NAME_SZ 25 void main() { char name_1[NAME_SZ], name_2[NAME_SZ]; // Declare 2 char arrays with size of 25 printf("Hi, what's yout name pal? "); gets(name_1); // Get input to target char array printf("And what's your best friend's name? "); gets(name_2); if (strlen(name_1) > strlen(name_2)) // Compares 2 char array lengths puts("Wow, your name has more characters than your best friend's name! "); else if(strlen(name_1) < strlen(name_2)) puts("Wow, your best friend's name has more characters than yout name! "); else puts("It seems that your name has the same length than your best friend's name..."); if (strcmp(name_1, name_2) != 0) //returns 0 if both char arrays are equal puts("And are they equal? No!"); else puts("And the names are both equal!!"); getch(); }
Easy isnt it ?![]()
Last edited by outsid3r; 11-13-2008 at 03:12 PM.
You shouldn't refer to it as "position 6", but rather as "the 6th position", just for clarification purposes. But well written and very understandable. I know it's not worth much, but here's a +repThis will assign an 'A' char to position 6 of the char array (six because 0 also counts, so it's 5+1).![]()
Wow, thank you both for rep+ me! i wasn't expecting that really, it was just a quick tutorial to help ahmed to undestand a little better how char arrays work, i always like to help, ty both. And as you requested Aereshaa, i have posted it in tutorial's section.
And Steve you're right of course, sorry, my english isn't perfectthanks for correcting me
![]()
Ok, i explain to you.
void main() -> I know you probably thinking "lol, i have seen on the internet that everyone should use int main, bla bla bla...", well you readed wrong, they are talking about C++ not C. In C it is used void main(). If you don't believe ask Dennis Ritchie.
gets() -> What's the wrong thing about that?! it's a very widely used function in C to get input from the stream to a variable of type char[].
getch() and <conio.h> -> I always use this function in the end of function 'main()' to avoid exit console then i'm running the program. But this time i just opened the Visual C++ and builded this tutorial in there. But in visual c++ we must declare <conio.h> to use getch() and i just declared the lib. In turbo C we don't need to declare <conio.h> to use getch() for example.
Are you satisfied ?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks