T
T
Tan Chatn2015-06-24 16:23:47
C++ / C#
Tan Chatn, 2015-06-24 16:23:47

Changes function arguments! What is it like?

Here's an example from the book:

#include <stdio.h>
#define MAXL 1000

int getline(char s[], int lim)
{
  int c, i;
  i=0;

  while(--lim>0 && (c=getchar()) != EOF && c != '\n') s[i++]=c;
  if(c == '\n')
    s[i++]=c;
  s[i]='\0';
  return i;
}

int strindex(char s[], char t[])
{
  int i, j, k;

  for(i = 0; s[i] != '\0'; i++){
    for(j=i, k=0; t[k] != '\0' && s[j] == t[k]; j++, k++)
      ;
    if(k > 0 && t[k] == '\0')
      return i;
  }
  return -1;
}

int main(int argc, char const *argv[])
{
  char pattern[]="ould";
  char line[MAXL];
  char found=0;

  while(getline(line, MAXL)>0)
    if(strindex(line, pattern)>=0){
      printf("Нашли: %s", line);
      found++;
    }
  return found;
}

This example is from a textbook by C Ritchie.
How can getline() modify the line array without passing a pointer?
But it compiles and everything works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2015-06-24
@DUKAEV

And from what you took, what the pointer is not transferred? For C, the char s[] and char *s variants are syntactically equivalent.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question