Home > Back-end >  Changing Array Notation to pure pointer notations
Changing Array Notation to pure pointer notations

Time:11-21

I had this school project that I'm working on. I am done, I just need to edit it a bit and am running into a issue. I sent my code to my professor so he could check it over and there's only one thing wrong with it.

All your array notations need to change to pure pointer notations such as *s1 or s1 , etc. nothing like *(random 41 - 1) or *(s2_input count) - you need to update your pointer and use dereference (exactly what you are doing in strfilter function.

He wouldn't explain further, so I am just confused on how exactly I would change my code. I have figured out my code is still in a array notation in a couple spots but any help would be appreciated. Such as *(random 41 - 1) = '\0'; , *(s2_input count) = '\0'; , and *(s2_input count) = input;. What can I do to fix this?

#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 generatro */
    {
        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  ;
    }
}

I tried a making a temporary pointer and replacing it with the array notation but I still would need to have the array notation somewhere. I can have the array defined somewhere but that's it. Any help would be greatly appreciated!

I just got clarification from the professor on what he wants. You don't have to initialize s2 as an array you can initialize it as pointer to integer (int s2;) then you can do a pointer to an array (int (s1)[41];) then point to the single element of the array by doing this: s2 = s1; and incriminating to the next element by doing this: s2

Does this make sense to anyone? I understand that he wants me to make a int pointer, and then use that to point to a certain element in the array however I am not sure on how to implement that.

CodePudding user response:

I interpreted this as that you need to replace

*(random limit) = characters;

with

*random  =characters;

and

*(s2_input   count) = input;

becomes

*s2_input   = input;

i.e. he is asking you to move the pointer to the next point as you use it rather than recalculate each time.

Below are 3 ways of doing the same thing.

void s1(char *random)
{

    for(int limit=0;limit<40;  limit)
    {
       char characters = 'A'   (rand() % 26);
       // random[limit]=characters;
       // *(random   limit) = characters;
       *random  =characters;
    }
    // random[limit]='\0';
    // *(random   limit) = '\0';
    *random='\0';

}

CodePudding user response:

A few issues ...

  1. Hardwiring the limit in s1 is bad. Better to pass the max length as an arg.
  2. The "Size too big" check in main must be done in s2 to prevent overflow/UB.

Here is the refactored code. I just fixed the pointer usage, but didn't test otherwise.

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

/*Function Prototypes*/
int main();
#if 0
void s1(char *random);
#else
void s1(char *random,int maxlen);
#endif
#if 0
void s2(char *s2_input, int index);
#else
int s2(char *s2_input, int index);
#endif
void strfilter(char *random, char *s2_input, char replacement);

int
main()
{
    for (;;) {
        int s1_index = 41;
        char s1_random[s1_index];

        s1(s1_random,s1_index);
        printf("\ns1 = ");
        puts(s1_random);

        printf("s2 = ");
        int s2_index = 21;
        char s2_input[s2_index];

#if 0
        s2(s2_input, s2_index);
        if (s2_input[1] == '\0') {
            printf("Size too small");
            exit(0);
        }
#else
        int len = s2(s2_input, s2_index);
        if (len < 2) {
            printf("Size too small\n");
            exit(0);
        }
#endif

// NOTE/BUG: the s2 function has to check this -- if too much data entered,
// s2 will overflow the buffer before we get here (causing UB)
#if 0
        if (s2_input[21] != '\0') {
            printf("Size too big");
            exit(0);
        }
#endif

        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 maxlen)
{
#if 0
    int limit = 0;
    char characters;

    /* random generatro */
    while ((characters = (('A'   (rand() % 26))))) {
        if (limit == 41) {
            *(random   41 - 1) = '\0';
            break;
        }
        *(random   limit) = characters;
        limit  ;
    }
#else
    char characters;

    /* random generatro */
    for (--maxlen;  maxlen > 0;  --maxlen) {
        characters = 'A'   (rand() % 26);
        *random   = characters;
    }

    *random = 0;
#endif
}

#if 0
void
s2(char *s2_input, int maxlen)
{
    /* populated array to make sure no random memory is made */
    char array[21] = "123456789012345678901";
    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(1);
        }

        *(s2_input   count) = input;
        count  ;
    }

    index = count;
}
#else
int
s2(char *s2_input, int maxlen)
{
    int input;
    int count = 0;

    --maxlen;

    while ((input = getchar()) != EOF) {
        if (input == '\n')
            break;

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

        if (maxlen <= 0) {
            printf("input too long\n");
            exit(1);
        }

        *s2_input   = input;
        --maxlen;
          count;
    }

    *s2_input = 0;

    return count;
}
#endif

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

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

In the above code, I've used cpp conditionals to denote old vs. new code:

#if 0
// old code
#else
// new code
#endif

#if 1
// new code
#endif

Note: this can be cleaned up by running the file through unifdef -k

  •  Tags:  
  • c
  • Related