Thread: foo

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    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;
    	}
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    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("&#37;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;
    	}
    }

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You're defining a local variable "double *foo"

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i am sorry to keep bothering you but i am very new to this whole programing thing.
    do i use void instead of double*

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    delete this line. u prototyped foo already. change the prototype to double instead of void if u want a return type double.
    Code:
    	double *foo;

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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("&#37;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;
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Function
    By Codedecode in forum C++ Programming
    Replies: 5
    Last Post: 06-07-2008, 02:14 AM
  2. array -- foo, &foo and &foo[0]
    By George2 in forum C Programming
    Replies: 9
    Last Post: 08-14-2007, 01:22 PM
  3. anyone here hate when foo bar baz is used in example code?
    By shintaro in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 09-26-2006, 07:07 AM
  4. Couple of Q's.....
    By oobootsy1 in forum C++ Programming
    Replies: 18
    Last Post: 02-23-2004, 02:03 PM
  5. x = foo();
    By HoundEYE in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2003, 09:41 PM