Using pointers instead of subscripts for arrays

This is a discussion on Using pointers instead of subscripts for arrays within the C Programming forums, part of the General Programming Boards category; For this, I was supposed to build a program that read arrays from pointers instead of subscripts. At the highlighted ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    17

    Using pointers instead of subscripts for arrays

    For this, I was supposed to build a program that read arrays from pointers instead of subscripts. At the highlighted location, I was having an error that said: [Warning] initialization discards qualifiers from pointer target type. How do I get the EOF message to skip for the second array it reads in? Besides that, there is just something wrong with the program that makes it so every array it compares says they are the same, even when they are clearly different. Also, when I print out what the user enters, some of the values are skewed. I included a run at the bottom so you can see what I mean.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX 10
    
    int main()
    {
      int read_eles(int a[], int max, int sent);
      void print_eles(const int a[], int v);
      int table_diff(const int a[], const int b[], int m, int n);
      
      int a[MAX];
      int b[MAX];
      int m;
      int n;
      int index;
      int sent = -999;
      
      m = read_eles(a, MAX, sent);
      n = read_eles(b, MAX, sent);
      print_eles(a, m);
      print_eles(b, n);
      index = table_diff(a, b, m, n);
      if (index = -1)
         printf("The tables are the same.\n");
      else      // index != -1
         printf("The tables are different.\n");
      
      system("PAUSE");	
      return 0;
    }
    
    int read_eles(int a[], int max, int sent)
    {
      int value;
      int r;
      int *ptr = a;
      int *endptr = ptr + max;
      
      while((r = scanf("%i", &value)) != EOF && value != sent && ptr < endptr)
      {
         if (r == 0)            // invalid character
         {
            printf("Nonnumeic data has been entered.\n");
            while (getchar() != '\n')   // flush invalid character
               ;
         }
         else
            *ptr++ = value;
      }
      if (r == 1 && value != sent)
         printf("Too many elements were entered.");
      else if(r == EOF) 
         printf("End of file reached before encountering the"
         " sentinel value %i.\n", sent);
      
      return ptr - a;
    }
    
    void print_eles(const int a[], int v)     // prints the table of values
    {
       int *ptr = a;
       int *endptr = a + v;
       
       while (ptr++ < endptr)
          printf("%i\n", *ptr);
    }
    
    int table_diff(const int a[], const int b[], int m, int n)
    {
       const int *aptr = a;
       const int *bptr = b;
       const int *aendptr = a + m;
       const int *bendptr = b + n;
       
       while (aptr < aendptr && bptr < bendptr && *aptr == *bptr)
       {
          aptr++;
          bptr++;
          
       }
       if (aptr == aendptr && bptr == bendptr)
          return -1;
       else      // if *aptr != *bptr
          return aptr - a;
    }
    Code:
    1 2 3 -999
    1 2 4
    ^Z
    End of file reached before encountering the sentinel value -999.
    2
    3
    2009252579
    2
    4
    4006969
    The tables are the same.
    Press any key to continue . . .

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    Recognize the difference highlighted in your program

    Find the difference in declartion of variables which are highlighted in ur program

    Quote Originally Posted by Sir Andus
    For this, I was supposed to build a program that read arrays from pointers instead of subscripts. At the highlighted location, I was having an error that said: [Warning] initialization discards qualifiers from pointer target type. How do I get the EOF message to skip for the second array it reads in? Besides that, there is just something wrong with the program that makes it so every array it compares says they are the same, even when they are clearly different. Also, when I print out what the user enters, some of the values are skewed. I included a run at the bottom so you can see what I mean.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX 10
    
    int main()
    {
      int read_eles(int a[], int max, int sent);
      void print_eles(const int a[], int v);
      int table_diff(const int a[], const int b[], int m, int n);
      
      int a[MAX];
      int b[MAX];
      int m;
      int n;
      int index;
      int sent = -999;
      
      m = read_eles(a, MAX, sent);
      n = read_eles(b, MAX, sent);
      print_eles(a, m);
      print_eles(b, n);
      index = table_diff(a, b, m, n);
      if (index = -1)
         printf("The tables are the same.\n");
      else      // index != -1
         printf("The tables are different.\n");
      
      system("PAUSE");	
      return 0;
    }
    
    int read_eles(int a[], int max, int sent)
    {
      int value;
      int r;
      int *ptr = a;
      int *endptr = ptr + max;
      
      while((r = scanf("%i", &value)) != EOF && value != sent && ptr < endptr)
      {
         if (r == 0)            // invalid character
         {
            printf("Nonnumeic data has been entered.\n");
            while (getchar() != '\n')   // flush invalid character
               ;
         }
         else
            *ptr++ = value;
      }
      if (r == 1 && value != sent)
         printf("Too many elements were entered.");
      else if(r == EOF) 
         printf("End of file reached before encountering the"
         " sentinel value %i.\n", sent);
      
      return ptr - a;
    }
    
    void print_eles(const int a[], int v)     // prints the table of values
    {
       int *ptr = a;
       int *endptr = a + v;
       
       while (ptr++ < endptr)
          printf("%i\n", *ptr);
    }
    
    int table_diff(const int a[], const int b[], int m, int n)
    {
       const int *aptr = a;
       const int *bptr = b;
       const int *aendptr = a + m;
       const int *bendptr = b + n;
       
       while (aptr < aendptr && bptr < bendptr && *aptr == *bptr)
       {
          aptr++;
          bptr++;
          
       }
       if (aptr == aendptr && bptr == bendptr)
          return -1;
       else      // if *aptr != *bptr
          return aptr - a;
    }
    Code:
    1 2 3 -999
    1 2 4
    ^Z
    End of file reached before encountering the sentinel value -999.
    2
    3
    2009252579
    2
    4
    4006969
    The tables are the same.
    Press any key to continue . . .

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I was having an error that said: [Warning] initialization discards qualifiers from pointer target type.
    That's not an error, it's a warning. It's telling you that you're using an int pointer to point to a const int pointer. You can fix it by doing this:
    Code:
    const int *ptr = a;
    const int *endptr = a + v;
    >How do I get the EOF message to skip for the second array it reads in?
    Can you clarify this?

    >every array it compares says they are the same, even when they are clearly different
    Code:
    if (index = -1)
    No, every array is clearly the same according to your test.

    >when I print out what the user enters, some of the values are skewed.
    That's because your print algorithm is off by one:
    Code:
    while (ptr++ < endptr)
        printf("%i\n", *ptr);
    Should be:
    Code:
    while (ptr < endptr)
        printf("%i\n", *ptr++);
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    Wrong comparison in "if"

    Sorry about previous suggestion i didn't go through ur code clearly. what i observe is u r trying to assign a value to iundex in if statement. Please opbserve the the bolded statement.
    So to avoid such silly mistakes try to code in this way

    if( -1 == index) By doing in this way we can avoid some mistakes

    For ex. if i placed the assign operator instead of equal the compiler will throw an error message.

    if( -1 = index) --> leads to an error

    if( index = -1) --> no error and the value assigned to index


    Quote Originally Posted by Prelude
    >I was having an error that said: [Warning] initialization discards qualifiers from pointer target type.
    That's not an error, it's a warning. It's telling you that you're using an int pointer to point to a const int pointer. You can fix it by doing this:
    Code:
    const int *ptr = a;
    const int *endptr = a + v;
    >How do I get the EOF message to skip for the second array it reads in?
    Can you clarify this?

    >every array it compares says they are the same, even when they are clearly different
    Code:
    if (index = -1)
    No, every array is clearly the same according to your test.

    >when I print out what the user enters, some of the values are skewed.
    That's because your print algorithm is off by one:
    Code:
    while (ptr++ < endptr)
        printf("%i\n", *ptr);
    Should be:
    Code:
    while (ptr < endptr)
        printf("%i\n", *ptr++);

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    17
    Ok, everything is in working order except for the EOF occuring before the sentinel value message. Since I call the read_eles function twice, the second time it calls it, the sentinel value has already been used up, so it prints the message no matter what. The only viable solution I can come up with by myself is to create a whole new function, but that seems very inefficient and I'm sure that has to be another way.

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    You are overloading the read_eles function. Rather than writing a whole new function, you just have to tell the exiting function whether you are interested in hitting EOF before sentinel (1st pass) or not (2nd pass). Perhaps an extra argument to the function, a simple boolean flag indicating whether its important that you've hit EOF before reading the sentinel. It would be "true" for the 1st pass and "false" for the second pass.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > if( -1 == index) By doing in this way we can avoid some mistakes
    http://c-faq.com/style/revtest.html
    It's an old trick from when compilers were rather brain-damaged in the reporting warnings department.

    Use a better compiler, or find out what options exist which warn you when you use = by mistake.

    The real problem is you replaced "remember to use ==" with "remember to swap them over AND remember to use =="
    What happens when the safety net is no longer there, and you have to compare two variables? You're forced to remember the == rule because the swapped "meta-fix" no longer works.

    I also find swapped expressions far more annoying to read.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if( -1 == index) By doing in this way we can avoid some mistakes
    I'm not keen on bug-saving conventions that don't always work. It's better to use a nice, readable convention then both be careful as you're writing and run your code through a semantic checker like lint. Most good compilers will warn about this bug also, if you bother to turn up your warnings.

    >Ok, everything is in working order except for the EOF occuring before the sentinel value message.
    I'm still not sure I understand your problem. If SKeane described it correctly then you should pull that message out of the function entirely and deal with EOF conditionally in the calling function:
    Code:
    m = read_eles(a, MAX, sent);
    
    /* Only process EOF on the first array */
    if ( feof ( stdin ) )
      printf("End of file reached before encountering the sentinel value %i.\n", sent);
    
    n = read_eles(b, MAX, sent);
    If that's not your problem, please give examples of what you want to happen and examples of what's actually happening.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21