Need help with pointer in C

How I can change the Code below?I want to use pointers (char *s) instead of arrays (char s []).But it should work the same way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  int mgetline(char s[],int lim)
  {
    int c,i,j;
    j=0;

    for (i = 0; c=getchar() != EOF && c != '\n'; i++)
    {
      if (j < lim-1)
      {
        s[j] = c;
        j++;
      }
    }
    if (c == '\n')
    {
      if (j < lim-1)
      {
        s[j] = c;
        j++;
      }
      i++;
    }
    s[j] = '\0';
    return i;
} 
arrays are passed as pointers
in both of these statements s is a pointer
int mgetline(char s[],int lim)

int mgetline(char *s,int lim)
Last edited on
Yes but I need it with pointer-notation (with *).Could someone please help.
I want to see how it would look like.
For example:

void copy(char s[ ], char t[ ])
{
int i = 0;
while (s[i] == t[i])
i++;
}


void copy(char *s, char *t)
{
while (*s++ == *t++)
;
}
s[j]
The pointer equivalent
*(s + j)
Is that correct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int mgetline(char *s,int lim) 
{ 
  int c,i,j; 
  j=0; 
  for (i = 0; c=getchar() != EOF && c != '\n'; i++) { 
    if (j < lim-1) { 
      *(s+j)= c;
       j++; }
   } 
  if (c == '\n') { 
    if (j < lim-1) { 
      *(s+j)= c; 
      j++; }
       i++; } 
  *(s+j) = '\0'; 
  return i; 
} 

Last edited on
How can I do it for that one?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#include <stdio.h>

#define LENGTH 80

int main(void)
{
    int c, i, j;
    char s[LENGTH-1];

    i = 0;
    while ((c = getchar()) != EOF)
    {
        if (c != '\n')
        {
            if (i == LENGTH-1)
                for (j = 0; j < LENGTH-1; j++)
                    putchar(s[j]);

            if (i >= LENGTH-1)
                putchar(c);
            else
                s[i] = c;

            i++;
        }
        else
        {
            if (i > LENGTH-1)
                putchar('\n');
            i = 0;
        }
    }

    return 0;
}
Could someone help me with int mgetline?My version is not working.
Topic archived. No new replies allowed.