-
foo
i am trying to run the following program and i keep getting a compiling error that says that foo is not a function. What am I doing wrong?
Code:
#include <stdio.h>
#include <stdlib.h>
double* makeAnArray(double size);
int main (void)
{
double *anArray;
int size=0;
int i;
double *foo;
printf("Please enter the size of the array ");
scanf("%d", &size);
anArray=makeAnArray(size);
for (i = 0; i < size; i++)
{
printf("%.1f ", anArray[i]);
}
printf("\n");
foo(anArray,size);
free(anArray);
getchar();
return 0;
}
double* makeAnArray(double size){
double* array;
array =calloc(size,sizeof(double));
return array;
}
void foo (double* a, double size){
int i;
for (i=0; i<size; i++){
*(a+i)=132;
}
}
-
You need to prototype foo so you can use it.
A few other things,
* Don't use double for everything
* calloc() expects size_t as arg1 not a double
* Check the return value of calloc()
* Learn how to indent.
-
i got the prototype down and i indented, and I am trying to figure out why it still tells me that foo is not a function?
Code:
#include <stdio.h>
#include <stdlib.h>
double* makeAnArray(double size);
void foo(double*a, double size);
int main (void)
{
double *anArray;
int size=0;
int i;
double *foo;
printf("Please enter the size of the array ");
scanf("%d", &size);
anArray=makeAnArray(size);
for (i = 0; i < size; i++)
{
printf("%.1f ", anArray[i]);
}
printf("\n");
foo(anArray,size);
free(anArray);
getchar();
return 0;
}
double* makeAnArray(double size){
double* array;
array =calloc(size,sizeof(double));
return array;
}
void foo (double* a, double size){
int i;
for (i=0; i<size; i++){
*(a+i)=132;
}
}
-
You're defining a local variable "double *foo"
-
i am sorry to keep bothering you but i am very new to this whole programing thing.
do i use void instead of double*
-
delete this line. u prototyped foo already. change the prototype to double instead of void if u want a return type double.
-
The red bits are the areas of interest:
Code:
#include <stdio.h>
#include <stdlib.h>
double* makeAnArray(double size);
void foo(double*a, double size);
int main (void)
{
double *anArray;
int size=0;
int i;
double * foo;
printf("Please enter the size of the array ");
scanf("%d", &size);
anArray=makeAnArray(size);
for (i = 0; i < size; i++)
{
printf("%.1f ", anArray[i]);
}
printf("\n");
foo(anArray,size);
free(anArray);
getchar();
return 0;
}
double* makeAnArray(double size){
double* array;
array =calloc(size,sizeof(double));
return array;
}
void foo (double* a, double size){
int i;
for (i=0; i<size; i++){
*(a+i)=132;
}
}