1

I have this coding assignment where I have to use pure pointer notation only. I am pretty much finished with it but I just realized that I used an array. I am not allowed to do so, unless I change it into a pointer somehow. That's where I am slightly stuck.

This is my code.

#include <stdio.h>
#include <stdlib.h>

/* Function Prototypes */
int main();
void s1(char *random);
void s2(char *s2_input, int index);
void strfilter(char *random, char *s2_input, char replacement);

int main()
{
    for(;;)
    {
        int s1_index = 41;
        char s1_random[s1_index];
        s1(s1_random);
        printf("\ns1 = ");
        puts(s1_random);
        printf("s2 = ");
        int s2_index = 21;
        char s2_input[s2_index];
        s2(s2_input, s2_index);

        if(s2_input[1] == '\0')
        {
            printf("Size too small");
            exit(0);
        }

        if(s2_input[21] != '\0' )
        {
            printf("Size too big");
            exit(0);
        }

        printf("ch = ");
        int replacement = getchar();
        if(replacement == EOF)
            break;
        while(getchar() != '\n');
        printf("\n");
        strfilter(s1_random, s2_input, replacement);
        printf("\ns1 filtered = ");
        puts(s1_random);

        printf("Do you wish to run again? Yes(Y), No(N) ");
        int run = getchar();
        // or include ctype.h and do:
        // run == EOF || toupper(run) == 'N'
        if(run == EOF || run == 'N' || run == 'n')
            break;
        while(getchar() != '\n');

    }
}




void s1(char *random)
{
    int limit = 0;
    char characters;
    while((characters = (('A' + (rand() % 26))))) /* random generator */
    {
        if(limit == 41)
        {
            *(random + 41 - 1) = '\0';
            break;
        }
        *(random + limit) = characters;
        limit++;
    }
}



void s2(char *s2_input, int index)
{
    char array[21] = "123456789012345678901"; /* populated array to make sure no random memory is made */
    char input;
    int count = 0;
    int check = 0;

    while((input = getchar() ))
    {
        if(input == '\n')
        {
            *(s2_input + count) = '\0';
            break;
        }

        else if(input < 65 || input > 90)
        {
            printf("invalid input");
            exit(0);
        }

        *(s2_input + count) = input;
        count++;
    }

    index = count;
}

void strfilter(char *random, char *s2_input, char replacement) /* replacement function */
{
    while(*s2_input)
    {
        char *temp = random;

        while(*temp)
        {
            if(*temp == *s2_input)
                *temp = replacement;
            temp++;
        }
        s2_input++;
    }
}

My issue is this part I am not sure how to edit this to not include an array, and still have it output the program in the same way.

        if(s2_input[1] == '\0')
        {
            printf("Size too small");
            exit(0);
        }

        if(s2_input[21] != '\0' )
        {
            printf("Size too big");
            exit(0);
        }

I tried to take the address of the array at a certain point, and then dereference it with a pointer, however that is still using a array. Which is what I am trying to avoid. Any help would be greatly appreciated!

  • 4
    The question you are really asking is "What is the difference between a pointer and an array". Maybe search for that – Tibrogargan Nov 20 '22 at 04:11
  • I recommend using `srand()` for generating better pseudo-random numbers, that change after every run of the program. – L_R Nov 20 '22 at 05:48
  • 1
    @user1234 no `srand()` is only for **seeding** the generator, to get random numbers you need to use `rand()`. But [`rand()` isn't exactly good either](https://stackoverflow.com/q/52869166/995714), the standard says nothing about it so each implementation uses its own algorithm, sometimes [extremely bad](https://stackoverflow.com/q/7866754/995714). No one uses `rand()` for serious problems – phuclv Nov 20 '22 at 10:09

3 Answers3

0

s2_input[i] can be written as *(s2_input+i) where i is some index.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
0
if ((s2_input[1]) == '\0')

is equivalent to:

if (*(s2 + 1) == '\0')

Which means to dereference the value at s2 (which is the location of the zeroth[0] element), and add one to it. The same could be done for any other location.

Harith
  • 4,663
  • 1
  • 5
  • 20
0

Pointer notation and what is often referred to as Indexed notation (which uses the [ ] subscript operator) are entirely equivalent. Either notion provides the pointer address plus an offset from that pointer address. See C11 Standard - 6.5.2.1 Array subscripting That being array[offset] or *(array + offset)1

For example accessing the first element using *array is shorthand for *(array + 0) which is simply array[0] in indexed notation. The 0 being the offset (in elements of that type) from the original pointer address. (type controls pointer arithmetic)

So array[10] is simply *(array + 10). If array is type char, array[10] is 10-bytes after array address. If array is type int (where an int is 4-bytes), then array[10] is 40-bytes after array address (10-int).

For a 2D array, the notation for arr2d[1][2] would simply be *(arr2d[1] + 2) which expanded further is simply *(*(arr2d + 1) + 2).

So generally array[i] is *(array + i) and arr2d[i][j] is *(*(arr2d + i) + j).

footnotes:

  1. It follows that array[offset] is equivalent to *(array + offset) is equivalent to offset[array].
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85