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?
Char Arrays
Started by ahmed, Nov 13 2008 05:12 AM
65 replies to this topic
#1
Posted 13 November 2008 - 05:12 AM
|
|
|
#2
Posted 13 November 2008 - 05:40 AM
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.
#3
Posted 13 November 2008 - 10:36 AM
Free C/C++ Resources
Recommended C/C++ Books
Free C/C++ Everything
Teach Yourself C++ in 21 Days (look at chapter 11 for arrays)
Recommended C/C++ Books
Free C/C++ Everything
Teach Yourself C++ in 21 Days (look at chapter 11 for arrays)
#5
Posted 13 November 2008 - 02:27 PM
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:
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:
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:
Easy isnt it ? :P
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:
char string[30];
To access to an specific position in the char array and assign a value to it you should do:
string[5] = 'A';This will assign an 'A' char to the 6th position of the char array (six because 0 also counts, so it's 5+1).
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:
char name[30] = "ahmed\0';
To easly use char arrays for input and output you should use functions like:
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:
#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 ? :P
Edited by outsid3r, 13 November 2008 - 03:12 PM.
#7
Posted 13 November 2008 - 02:40 PM
Quote
This will assign an 'A' char to position 6 of the char array (six because 0 also counts, so it's 5+1).
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 +rep :)
#8
Posted 13 November 2008 - 02:50 PM
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 perfect :P thanks for correcting me :)
And Steve you're right of course, sorry, my english isn't perfect :P thanks for correcting me :)
#9
Posted 13 November 2008 - 03:00 PM
outsid3r said:
I can give you a quick tutorial.
...
gets() -> gets the input and assign it to a variable (automatically puts char terminator at the end)
Easy isnt it ? :P
...
gets() -> gets the input and assign it to a variable (automatically puts char terminator at the end)
#include <conio.h> void main() gets(name_1); getch();
Easy isnt it ? :P
#10
Posted 13 November 2008 - 03:20 PM
dcs said:
IMO, anyone demonstrating the use of gets(), void main(), using <conio.h> and getch() should be reading tutorials, not writing them.
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 ?
#11
Posted 13 November 2008 - 03:24 PM
outsid3r said:
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.
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.
Quote
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[].
Quote
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 declare the lib. In turbo C we don't need to declare <conio.h> to use getch() for example.
#12
Posted 13 November 2008 - 03:31 PM
There is nothing unsafe with gets, i have used it many times and i have never seen any kind of error. About void main, like i have said, it's used in C but not in C++. About the rest you are wrong in my opinion, it's not a winded way of saying "it's non-standard", it's just C.


Sign In
Create Account


Back to top









