473,499 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Character Arrays in C

5 New Member
I understand simple character arrays; however I do not understand this code example in the K & R book:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define MAXLINE 1000    /* maximum input line length */
  3.  
  4. int getline(char line[], int maxline);
  5. void copy(char to[], char from[]);
  6.  
  7. /* print the longest input  line */
  8. main()
  9. {
  10.     int len;             /* current line length */
  11.     int max;             /* maximum length seen so far */
  12.     char line[MAXLINE];     /* current input line */
  13.     char longest[MAXLINE];  /* longest line saved here */
  14.     max = 0;
  15.     while ((len = getline(line, MAXLINE)) > 0)
  16.         if (len > max) {
  17.              max = len;
  18.              copy(longest, line);
  19.         }
  20.     if (max > 0) /* there was a line */
  21.         printf("%s", longest);
  22.     return 0;
  23. }
  24.  
  25. /* getline: read a line into s, return length   */
  26. int getline(char s[],int lim)
  27. {
  28.     int c, i;
  29.     for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
  30.         s[i] = c;
  31.     if (c == '\n') {
  32.         s[i] = c;
  33.         ++i;
  34.     }
  35.     s[i] = '\0';
  36.     return i;
  37. }
  38.  
  39. /* copy: copy 'from' into 'to'; assume to is big enough */
  40. void copy(char to[], char from[])
  41. {
  42.     int i;
  43.     i = 0;
  44.     while ((to[i] = from[i]) != '\0')
  45.         ++i;
  46. }
  47.  
As the comment before main says, the program outputs the longest string. However, a few things I don't understand.

When I declare an array, I cannot leave the array subscript empty (int blah[]) I have to specify the subscript. However in this program, the parameters for getline and copy have arrays with an empty subscript. I don't understand this and I don't understand using the array line and longest. They are being used as arguments without specifying which subscript is being used.

I may not be clear on what I do not understand, but I would like someone to explain how line and longest are being passed to the functions.

Thank you in advance.
Mar 29 '08 #1
2 2635
Natasha26
12 New Member
I understand simple character arrays; however I do not understand this code example in the K & R book:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define MAXLINE 1000    /* maximum input line length */
  3.  
  4. int getline(char line[], int maxline);
  5. void copy(char to[], char from[]);
  6.  
  7. /* print the longest input  line */
  8. main()
  9. {
  10.     int len;             /* current line length */
  11.     int max;             /* maximum length seen so far */
  12.     char line[MAXLINE];     /* current input line */
  13.     char longest[MAXLINE];  /* longest line saved here */
  14.     max = 0;
  15.     while ((len = getline(line, MAXLINE)) > 0)
  16.         if (len > max) {
  17.              max = len;
  18.              copy(longest, line);
  19.         }
  20.     if (max > 0) /* there was a line */
  21.         printf("%s", longest);
  22.     return 0;
  23. }
  24.  
When I declare an array, I cannot leave the array subscript empty (int blah[]) I have to specify the subscript. However in this program, the parameters for getline and copy have arrays with an empty subscript. I don't understand this and I don't understand using the array line and longest. They are being used as arguments without specifying which subscript is being used.

I may not be clear on what I do not understand, but I would like someone to explain how line and longest are being passed to the functions.

Thank you in advance.

When you declare a static array, you can do either of the following:
Expand|Select|Wrap|Line Numbers
  1.  int arr1[3] = {1,2,3}; // or int arr1[3]; arr1[0]=1; arr1[1]=2; arr1[2]=3;
  2. int arr2[] = {1,2,3}; //compiler creates array of 3 and initialises them with the given values
  3. char arr3[] = "hello world!"; //compiler creates char array of length 12+1. the last character being the '\0' to indicate end of char array (this is a special case), and initialises the array with the given characters
  4.  
When you pass an array to a function (in the 1-dimensional case), the following declarations are equivalent:

Expand|Select|Wrap|Line Numbers
  1. void f(char arr[], int size);
  2. void f(char* arr, int size);
Since the function cannot know the size of the array (actually i don't think the function even knows if it's an array), you have to pass the array size as second argument, and write your code to deal with both arguments.
Mar 29 '08 #2
whodgson
542 Contributor
Another variation is demonstrated with this example.
Assume you have an int array with say 20 test scores and you want to cycle through them and assign each an A or B or C etc.grade if each is > 90 or >80 or >70 etc.and accumulate the number of A`s,B`s etc.
Expand|Select|Wrap|Line Numbers
  1. int score [20] = (88,47,67, etc.};      //the data array
  2. enum {A,B,C,D,F};                         //assign an integer to each grade 1-5
  3. int freq [5] = {0};                            //declare a freq array (to hold grades A - F)
  4. if(score[i] >=90)++freq[A];             //will accumulate no of A`s in score[]
  5. if(score[i]>=80)++freq[B};              // will accumulate no of B`s in score[] etc.
  6. cout<<"No of A`s"<<freq[A];           
  7.  
I found this in a book i`m using to learn about C++ namely: Fundamentals of Computing with C++ by John R Hubbard.
Mar 30 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2667
by: rl | last post by:
Hi out there, I'd like to know sth about the costs of a function call in php and the handling of character arrays (init size, enlargement steps of allocated memory, technique on enlargement ->...
43
2655
by: Anitha | last post by:
Hi I observed something while coding the other day: if I declare a character array as char s, and try to use it as any other character array..it works perfectly fine most of the times. It...
8
7367
by: lasek | last post by:
Hi all, a simple question, look at this code below: char acName="Claudio"; unsigned int uiLen; uiLen=strlen(acName); printf("Length of acName variable %u",uiLen); //uiLen >>>> 7
3
14122
by: linguae | last post by:
Hello. In my C program, I have an array of character pointers. I'm trying to input character strings to each index of the character pointer array using scanf(), but when I run the program, I get...
8
11846
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
14
4043
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
14
3000
by: mast2as | last post by:
Hi everyone, I am trying to implement some specs which specify that an array of parameter is passed to a function as a pointer to an array terminated by a NULL chatacter. That seemed fairly easy...
4
7798
by: reva | last post by:
hi all!! can any one please help me in checking the two character arrays. in my code i need to compare a character array(seq) to that of hydrob and hydrop . if the seq has hydrob then it should be...
3
3517
by: Tarik Monem | last post by:
Hi Everyone, Still a newbie with FLEX, and I've passed arrays using AJAX to FLEX before, but I've never passed links to FLEX. Basically, this is the OUTPUT, which I wanted, but I'm given an...
19
2010
by: bowlderyu | last post by:
Hello, all. If a struct contains a character strings, there are two methods to define the struct, one by character array, another by character pointer. E.g, //Program for struct includeing...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.