I understand simple character arrays; however I do not understand this code example in the K & R book: -
#include <stdio.h>
-
#define MAXLINE 1000 /* maximum input line length */
-
-
int getline(char line[], int maxline);
-
void copy(char to[], char from[]);
-
-
/* print the longest input line */
-
main()
-
{
-
int len; /* current line length */
-
int max; /* maximum length seen so far */
-
char line[MAXLINE]; /* current input line */
-
char longest[MAXLINE]; /* longest line saved here */
-
max = 0;
-
while ((len = getline(line, MAXLINE)) > 0)
-
if (len > max) {
-
max = len;
-
copy(longest, line);
-
}
-
if (max > 0) /* there was a line */
-
printf("%s", longest);
-
return 0;
-
}
-
-
/* getline: read a line into s, return length */
-
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;
-
}
-
-
/* copy: copy 'from' into 'to'; assume to is big enough */
-
void copy(char to[], char from[])
-
{
-
int i;
-
i = 0;
-
while ((to[i] = from[i]) != '\0')
-
++i;
-
}
-
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.
2 2635
I understand simple character arrays; however I do not understand this code example in the K & R book: -
#include <stdio.h>
-
#define MAXLINE 1000 /* maximum input line length */
-
-
int getline(char line[], int maxline);
-
void copy(char to[], char from[]);
-
-
/* print the longest input line */
-
main()
-
{
-
int len; /* current line length */
-
int max; /* maximum length seen so far */
-
char line[MAXLINE]; /* current input line */
-
char longest[MAXLINE]; /* longest line saved here */
-
max = 0;
-
while ((len = getline(line, MAXLINE)) > 0)
-
if (len > max) {
-
max = len;
-
copy(longest, line);
-
}
-
if (max > 0) /* there was a line */
-
printf("%s", longest);
-
return 0;
-
}
-
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: - int arr1[3] = {1,2,3}; // or int arr1[3]; arr1[0]=1; arr1[1]=2; arr1[2]=3;
-
int arr2[] = {1,2,3}; //compiler creates array of 3 and initialises them with the given values
-
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
-
When you pass an array to a function (in the 1-dimensional case), the following declarations are equivalent: - void f(char arr[], int size);
-
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.
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. -
int score [20] = (88,47,67, etc.}; //the data array
-
enum {A,B,C,D,F}; //assign an integer to each grade 1-5
-
int freq [5] = {0}; //declare a freq array (to hold grades A - F)
-
if(score[i] >=90)++freq[A]; //will accumulate no of A`s in score[]
-
if(score[i]>=80)++freq[B}; // will accumulate no of B`s in score[] etc.
-
cout<<"No of A`s"<<freq[A];
-
I found this in a book i`m using to learn about C++ namely: Fundamentals of Computing with C++ by John R Hubbard.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 ->...
|
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...
|
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
|
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...
|
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?
| |
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.
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
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...
| |