Working with function prototypes and arrays in C

I am very new to C, and I looked at tutorials on arrays and functions, and I started the project.

I have a program that I'm working on, as shown below, for now I just want the user to enter the x and y values โ€‹โ€‹completed by ctlr Z. The problem is that I still donโ€™t understand how to bind the EnterValues function Inside and outside main (). Note that the EnterValues function contains arrays inside.

This program has not been completed since I am still adding to it. The result is empty. I understand this because inside main () there is nothing but int i, j; int, what I want to output is what is 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; } } 
+4
source share
3 answers

First, you must declare a function before using it. So put the declaration of the EnterValues โ€‹โ€‹function in front of the main one. Secondly, I think dataarray is the value you want from the "EnterValues" function.

You must change 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 a pointer. Good luck

+1
source

you must write a function prototype before the main one.

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

good luck :)

0
source

void EnterValues(float dataarray[][MAXDATACOL]); is a function prototype, which means that 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 prototype of the function is not declared inside any function, but outside, and it needs to be declared before you can use this function. otherwise, the compiler will not know what you mean when you call this function.

When you call a function that happens inside some other function (in this case you want to call EnterValues from main ), you will not specify what type it receives / returns. You simply obey the function declaration (prototype) by providing it with input parameters of the correct type and assigning it the return value of a variable of the corresponding type.

For instance:

 /* 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 a lot of errors in your code, I suggest you read the book C programming language , which is not entirely new, but very bright. (see this question )

0
source

Source: https://habr.com/ru/post/1345368/


All Articles