Thread: Issue with pointers and data types (in functions)

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Question Issue with pointers and data types (in functions)



    I cant seem to make this work i have an issue when i call the function into main, the data type of the array is NOT compatible...

    I know I have to change the pointers or data type, but if i do, i don't get the right median number or the combination is incompatible.

    Please help me out, I been trying this little piece of code for more than 3 hours.

    Thank you

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #define maxarr 30
    
    void show_median(double *[], int counter);
    
    
    int main ()
    {
    	double arr[maxarr], value;
    	int counter, i;
            counter=0;
    	scanf("%lf", &value);
    	while(value !=-1)
    	{
    		arr[counter]= value;
    		printf("Please enter '-1' to stop entering values.\n");
    		scanf("%lf", &value);
    		counter=counter+1;
    	}
    	printf("values entered are:\n\n");
    	i=0.0;
    	while(i<counter)
    	{
    		printf("%.1lf  ", arr[i]);
    		i=i+1;
    	}
    
    
           show_median(arr, counter);
    
           return 0;
    }
    void show_median(double *arr[], int counter)
    {
    	int mid;
    	double median2;
    	mid=counter/2.0;
    	if(counter%2)
    		median2= (*arr[mid]+*arr[mid/+1])/2.0;
    	else (median2=*arr[mid]);
    	printf("median is : %.1lf\n", median2);
    
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    double *arr[] -> double * arr
    median2= (*arr[mid]+*arr[mid/+1])/2.0; -> median2= (arr[mid] + arr[mid/+1]) / 2.0;
    else (median2=*arr[mid]); -> else (median2 = arr[mid]);

    Btw, it is convention that macros are all UPPERCASE, to avoid name conflicts.
    Also, you seem to be confusing integers are doubles, interchanging them where they really are not usable. They don't cause harm, but still...
    If you add ".0" to a number, you get a double. If you don't, you get an integer.

    Example:
    >>while(value !=-1)
    Here you are comparing a double to an integer. No problem since the integer is promoted to a double, but you might want to know.

    >>mid=counter/2.0;
    This is pointless (or might give a warning) since the result is truncated to an int afterwards anyway. You may as well divide by only "2".

    And lastly, i = i + 1 is the same as i++. Same for counter.
    Last edited by Elysia; 11-06-2010 at 04:14 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM

Tags for this Thread