Pointer Notation in C

How I can Write following code with poiner notation (char *s) instead of char s[ ]?

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
#include <stdio.h>

#define LENGTH 80

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

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

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

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

    return 0;
}
Last edited on
Yes I know but nobody answered.I thought if I create a separate thread it will help.
Please could someone help me??PLEASE..
Hi,
Can you explain what is a "pointer notation"?
*(array+index) is the pointer notation of array[index]
I hope this is what you requested.
Trank you very much.But how I can describe char s[LENGTH] with *.?
This might be what you need :
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
#include <stdio.h>
#define LENGTH 80
int main(void)
{
    int c, i, j;
    char *s; s = new char[LENGTH];
    i = 0;
    while ((c = getchar()) != EOF)
    {
        if (c != '\n')
        {
            if (i == LENGTH)
                for (j = 0; j < LENGTH; j++)
                    putchar(*(s + j));
            if (i >= LENGTH)
                putchar(c);
            else             *(s + i) = c;
            i++;
        }
        else        {
            if (i > LENGTH)
                putchar('\n');
            i = 0;
        }
    }
    delete [] s;
    return 0;
}
In C there is no way reserving memory at program space other then via:

my_type var_name = my_type[LENGTH]

That line instructs the compiler to reserve a whole array. Instead, a pointer holds just an address, pointing to a part of memory. Therefore, array[index] and *(array+index) are just pointers references to the same part of memory.
Last edited on
Thank you very much.I will try and let you know.
What about this one:

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

  i = 0;
  while (s[i] != '\0')
    ++i;
  --i;

  if (s[i] == '\n')
    --i;

  j = 0;
  while (j < i)
  {
    c = s[j];
    s[j] = s[i];
    s[i] = c;
    --i;
    ++j;
  }
}
Last edited on
> What about this one?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void reverse(char *s)
{
  int i, j;
  char c;
  i = 0;
  while (*(s + i) != '\0')
    ++i;
  --i;
  if (*(s + i) == '\n')
    --i;
  j = 0;
  while (j < i)
  {
    c = *(s + j);
    *(s + j) = *(s + i);
    *(s + i) = c;
    --i;
    ++j;
  }
}
The one with LENGTH is not working.What can be the cause?
Topic archived. No new replies allowed.