开发者

working with function prototypes and arrays in C

I'm very new to C, and I have been watching tutorials about arrays and functions and I started a project.

I have a program that I'm working on shown below, for now, I just want the user to enter x and y values terminated by ctlr Z. The problem is I still don't understand how to relate the function EnterValues inside and outside main(). Note the function EnterValues has arrays inside.

This program is not done, as I'm I'm still adding things to it. The output comes up empty I understand this, because inside main() there is nothing but int i,j; int values; what I want for output is whats under void EnterValues(float dataarray[][MAXDATACOL]).

#include "stdafx.h"
#include "stdio.h"

#define MAXDATACOL 5

int main(void) {
    void EnterValues(int dataarray[][MAXDATACOL]);
    int i,j;
    int values;
    while(1);
}

void EnterValues(float dataarray[][MAXDATACOL]) {
    for (;;) {
        int k = 0, g =开发者_开发知识库 0;
        printf("enter the x and y values terminated by ctrl Z\n");
        printf("[%d][%d]:",k++,g++);
        if (scanf("%f%f",&dataarray[k],&dataarray[g]) == EOF)
            break;
    }
}


First, you should declare the function before you use it. So, put the EnterValues function declaration before the main. Second, I guess the dataarray is the value you want to retrieve from the "EnterValues" function.

You should modify the code as

void EnterValues(float **dataarray, int *col_num);

int main(void)
{
    int i,j;
    float dataarray[MAXDATACOL][2];
    int col_num;

    EnterValues((float **)&dataarray, &col_num);
}

I hope you know the concept of pointer. Good luck!


you should write prototype of function before main.

void EnterValues(float dataarray[][MAXDATACOL]);int main(void)

good luck :)


void EnterValues(float dataarray[][MAXDATACOL]); is a function prototype, which means it is used to tell the compiler that there is a function, declared somewhere (in this case, in the same C file), called EnterValues, which returns a float dataarray[][MAXDATACOL] as a parameter, and returns nothing (void) the function prototype is not declared inside any function, but outside, and it has to be declared before you can use that function. otherwise the compiler will not know what do you mean when you call this function.

When you are calling the function, which happens inside some other function (in this case, you want to call EnterValues from main) you don't mention what type it receives / returns. You just obey the declaration of the function (the prototype), by giving it input parameters of proper type, and assigning it's return value to a variable of the proper type.

For example:

/* This is the prototype of our function multiply */
int multiply(int arg1, int arg2);

/* This is the main function which will use multiply */
int main()
{
   int a = 4;
   int b = 3;
   int sum;
   /* here we call the function, we don't write the types it gets, but obeying the prototype */
   sum = multiply(a, b);
   return 0;
}

/* This is the implementation of the function multiply */
int multiply(int arg1, int arg2)
{
   return arg1 * arg2;
}

As I see many mistakes in your code, I suggest you will read the book The C programming language which is not exactly new, but is very very bright. (see this question)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜