Closed Thread
Page 1 of 7 123 ... LastLast
Results 1 to 10 of 66

Thread: Char Arrays

  1. #1
    ahmed is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    300
    Rep Power
    0

    Char Arrays

    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?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Char Arrays

    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.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

  5. #4
    Join Date
    Apr 2008
    Posts
    789
    Blog Entries
    5
    Rep Power
    24

    Re: Char Arrays

    For info on string handling functions:
    stdio.h
    string.h
    Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
    "When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

  6. #5
    outsid3r's Avatar
    outsid3r is offline Programming God
    Join Date
    Jul 2008
    Posts
    621
    Rep Power
    19

    Re: Char 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:

    Code:
    char string[30];
    To access to an specific position in the char array and assign a value to it you should do:
    Code:
    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:

    Code:
    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:

    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.

  7. #6
    Join Date
    Apr 2008
    Posts
    789
    Blog Entries
    5
    Rep Power
    24

    Re: Char Arrays

    Repost that in the tutorials section. I'll +rep you.
    Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
    "When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

  8. #7
    Steve.L's Avatar
    Steve.L is offline Programming Expert
    Join Date
    Sep 2008
    Location
    Ottawa,ON
    Posts
    445
    Rep Power
    15

    Re: Char Arrays

    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

  9. #8
    outsid3r's Avatar
    outsid3r is offline Programming God
    Join Date
    Jul 2008
    Posts
    621
    Rep Power
    19

    Re: Char Arrays

    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 thanks for correcting me

  10. #9
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: Char Arrays

    Quote Originally Posted by outsid3r View Post
    I can give you a quick tutorial.
    ...
    gets() -> gets the input and assign it to a variable (automatically puts char terminator at the end)

    Code:
    #include <conio.h>
    void main()
    gets(name_1);											getch();
    Easy isnt it ?
    IMO, anyone demonstrating the use of gets(), void main(), using <conio.h> and getch() should be reading tutorials, not writing them.

  11. #10
    outsid3r's Avatar
    outsid3r is offline Programming God
    Join Date
    Jul 2008
    Posts
    621
    Rep Power
    19

    Re: Char Arrays

    Quote Originally Posted by dcs View Post
    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 ?

Closed Thread
Page 1 of 7 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Reading from file char by char in array
    By cypro in forum C and C++
    Replies: 7
    Last Post: 08-10-2011, 12:30 AM
  2. Replies: 10
    Last Post: 04-18-2011, 04:35 PM
  3. Replies: 4
    Last Post: 09-08-2010, 12:02 AM
  4. Make a function that returns Char Arrays
    By Dren in forum C and C++
    Replies: 11
    Last Post: 09-01-2008, 01:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts