LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C: array expressions? (https://www.linuxquestions.org/questions/programming-9/c-array-expressions-70508/)

 centr0 07-06-2003 12:12 PM

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; }```

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

 Proud 07-06-2003 12:16 PM

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.

 centr0 07-06-2003 12:26 PM

why mus s[i] = c;?

why is this expression needed if it is just incrementing i?

 kev82 07-06-2003 12:30 PM

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='A' puts 'A' in s. the input is read a character at a time in the test part of the for loop with getchar()

 coolman0stress 07-06-2003 01:38 PM

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).

 centr0 07-06-2003 04:13 PM

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! =)

 All times are GMT -5. The time now is 04:41 PM.