Home > Software engineering >  Pure Pointer notation in C
Pure Pointer notation in C

Time:11-20

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!

CodePudding user response:

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

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related