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 . . .
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 . . .