LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-06-2003, 12:12 PM   #1
centr0
Member
 
Registered: Feb 2003
Location: Earth
Distribution: Slackware 9.1
Posts: 134

Rep: Reputation: 15
C: array expressions?


im having trouble understanding the concept of arrays. I cant understand a piece of code in a book im reading.

this code is suppose to read a line of characters and return its length:
Code:
int getline(char s[], int lim)
{
     int c, i;
     for(i = 0; i <= lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
          s[i] = c;
     if (c =='\n') {
          s[i] = c;
          ++i;
     }

s[i] = '\0';
return i;
}
basically the lines that read:

s[i] = c;

i do not understand. if its suppose to read a line of characters why am i working with int? these maybe stupid questions so bear with me. im a newbie to C. =\

if someone can explain what is happening in this code i would greatly appreciate it. thanks
 
Old 07-06-2003, 12:16 PM   #2
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
I think you'll find it tests each character in the input array, and if they aren't the end \n, then it increments i. So when the s[i] == '\n' it's the end of the line, the count will be finished, and the function returns.

Edit: Basically an array is a long list of individual characters, so you can use int for each single one. I haven't really tried using C yet.

Last edited by Proud; 07-06-2003 at 12:19 PM.
 
Old 07-06-2003, 12:26 PM   #3
centr0
Member
 
Registered: Feb 2003
Location: Earth
Distribution: Slackware 9.1
Posts: 134

Original Poster
Rep: Reputation: 15
why mus s[i] = c;?

why is this expression needed if it is just incrementing i?
 
Old 07-06-2003, 12:30 PM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
thats quite a badly wriiten piece of code for a beginner to understand, but anyway imagine s to be a row of numbered post boxes, like you get in flats eg:

|0|1|2|3|4|5|

now in each of these boxes you can put a char. s[i] = c puts c in the box numbered i. so s[3]='A' puts 'A' in s[3]. the input is read a character at a time in the test part of the for loop with getchar()
 
Old 07-06-2003, 01:38 PM   #5
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Code:
for(i = 0; i <= lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
Like the above users have mentioned, what this function is read "characters" (using getchar()) and store them temporarly in var "c". The loop will keep going untill a) the limit (as passed to the function) is reached or b) EOF is reached (Ctrl-D in windows, etc) or finally c) a new line (user pressed 'Enter').

Basically, it reads a number of characters (dictated by lim) untill either EOF or newline is reached. The reason c is an int and not a char, is to be able to trap EOF, which doesn't "fit" into a char.

As you go through the loop you save the characters into an array
Code:
 s[i] = c;
Like kev82 said, an array is like a series of numbered boxes where you can put stuff into. So what s[i] means is find the box i in the array s and put the value of c into it. Now you might be wonder how you can put an int into an array of chars and the answer is that the int gets casted (converted) into a char (Actually, char is not a "character" variable, it's just the smallest form of an integer, usually an int is 4 bytes, while char is just a 1 byte, both store numbers, only a char can store less than an int).

Finally after the loop finishes you see:
Code:
 if (c =='\n') {
          s[i] = c;
          ++i;
Remember how the loop would end if you encounter a newliine? Well this piece of code, makes sure that the newline actually DOES get stored in the array.

Code:
s[i] = '\0';
return i;
Once the end of the function is reached (ie, a certain number of chars has been read and if a newline was encountered it was added to the array), you add the null byte ('\0') to the array. You must always add the null byte at the end, since that's how string/char array functions (strlen, strcpy, etc) determine the end of the array. Without the null byte you wouldn't be able to print the array either (unless you created a loop, but then you would also need to know how many characters to print from the array to start with). Finally in the last line you return the number of characters in the array(the number of characters that were read).
 
Old 07-06-2003, 04:13 PM   #6
centr0
Member
 
Registered: Feb 2003
Location: Earth
Distribution: Slackware 9.1
Posts: 134

Original Poster
Rep: Reputation: 15
thank you kev82 and coolman0stress for your replies. it has helped a lot. i hope this logic stuff gets easier as i learn more C. your explanations are great! =)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions markjuggles Programming 2 05-05-2005 11:39 AM
Regular Expressions overbored Linux - Software 3 06-24-2004 02:34 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM
Regular expressions aromes Linux - General 1 10-15-2003 12:29 PM
regular expressions? alaios Linux - General 2 06-11-2003 03:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration