Jump to content

Char Arrays

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
65 replies to this topic

#1
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,712 posts
Free C/C++ Resources
Recommended C/C++ Books
Free C/C++ Everything
Teach Yourself C++ in 21 Days (look at chapter 11 for arrays)

#4
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
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

#5
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
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:


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.


#6
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
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

#7
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts

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
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
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 :)

#9
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

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)

#include <conio.h>

void main()

gets(name_1);											getch();

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

#10
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts

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
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

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.
I've probably gone up and down this one enough to know on my own. Sure it's legal. But I doubt you're using it in a freestanding implementation. If not, it's surely not the right thing to do. Bad habit.

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[].
It's known to be the most unsafe function out there; it cannot be made safe. It was included in the standard merely not to break code existing from the 70's and 80's.

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.
That's a long-winded way of saying "it's non-standard". Which is another way to say, "maybe this will work for you, maybe not".

#12
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
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.