I get error C2440 in C. cannot convert from int to int[][2]
I get error C2440 in C. cannot convert from int to int[][2]
any help is appreciated.
#include "stdafx.h"
#include "stdio.h"
#define MAXDATACOL 2
void EnterValues(int dataarray[][MAXDATACOL]);
int main(void)
{
开发者_开发问答 int dataarray[][MAXDATACOL]=0;
int i,j;
int values;
}
void EnterValues(int dataarray[][MAXDATACOL])
{
for(;;)
{
int i, j;
printf( "enter the x and y values terminated by ctrl Z\n" );
if( scanf( "%d%d", &dataarray[i], &dataarray[j] ) == EOF )
break;
}
}
int dataarray[][MAXDATACOL]=0;
You can't do this because:
- 0 is a number while dataarray is an array (so a collection of numbers)
- you are trying to modify an out of range value. Indexes go from 0 to MAXDATACOL - 1
- you don't specify one of the dimensions of the array
for(;;)
{
int i,j;
printf("enter the x and y values terminated by ctrl Z\n");
if(scanf("%d%d",&dataarray[i],&dataarray[j])==EOF)
break;
}
Another problem: You need to initialize the values of i, j. Also, dataarray
is a 2D array.
As BlackBear noted C arrays need to be declared with a fixed size. They can not change in run time. Something like int a[] can be used in a function prototype because the pointer to the array is passed to the function.
#define MAXDATAROW 1000
#define MAXDATACOL 2
int main(void)
{
int dataarray[MAXDATAROW][MAXDATACOL]={0};
}
Is one way of dealing with this. Another way is using dynamic memory allocation (malloc, or realloc). Yet another is to use C++ containers which can resize dynamically (they allocate memory undet the hood).
I can see that you are struggling a little with this. If I were you I would declare a point struct to hold x,y pairs. I would also declare a large fixed size buffer to hold the values as input by the user. Something like this:
#include <stdio.h>
#define MAX_COUNT 1000
typedef struct {
int x;
int y;
} point;
void addPoint(point points[], int *count, point item)
{
points[*count] = item;
(*count)++;
}
int main(void)
{
point points[MAX_COUNT];
point item;
int i;
int count = 0;
printf("enter the x and y values terminated by ctrl Z\n");
for(;;)
{
if (scanf("%d%d",&item.x,&item.y)==EOF)
break;
addPoint(points, &count, item);
if (count==MAX_COUNT)
{
printf("Input buffer full.\n");
break;
}
}
for (i=0; i<count; i++)
printf("x=%d, y=%d\n", points[i].x, points[i].y);
return 0;
}
fugy - if you truly thought the code you posted above might actually have worked (once someone here sorted out your compiler error), I'm afraid you have much to learn.
You haven't specified the size of the first dimension of your dataarray. Even if you had, it would be meaningless to assign a value of 0 to the start address of a 2D array (it already has a fixed unchangeable address somewhere on the stack).
Your main() function contains no call to EnterValues(), so that code would never execute.
At your current level of coding ability there is no point in defining an EnterValues() function at all. It's only called once, and you've given the parameter the same name as the variable being passed, so you might as well have put the user input loop within main() to keep the code short while you're learning.
You don't set any values for your index subscripts i and j. Even if you had, they wouldn't do what you want because they would both refer to the first dimension. Obviously you want to read values into two consecutive elements in the second dimension.
You have made no provision for the maximum number of input pairs allowed before the user finally presses ^Z, and I suspect you don't even understand why this is a problem.
I suggest you work through a lot more very simple coding exercises before tackling multi-dimensional arrays and dynamic storage allocation for input of indeterminate size. These are relatively advanced techniques and it's unlikely you will understand any code corrections given here until you have a much firmer grasp of the basics.
精彩评论