Thread: incompatible pointer type error?

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    2

    incompatible pointer type error?

    Howdy!

    I'm learning C and ran into the error above with the note:
    "expected 'int *' but argument is of type 'int **' "
    What exactly did I mess up and how do I fix it? Here is my full code:

    #include <stdio.h>


    void sum(int a[], int n, int *even_elt_sum_ptr, int *odd_elt_sum_ptr);


    Code:
    int main (void)
    {
    	int n=10, *even_elt_sum_ptr , *odd_elt_sum_ptr , odd, even;
    	int a[10] = {1,2,3,4,5,6,7,8,9,10};
    	
    	even_elt_sum_ptr = 0;
    	odd_elt_sum_ptr = 0;
    	
    	sum(a, n, &even_elt_sum_ptr, &odd_elt_sum_ptr);
    	
    	even = *even_elt_sum_ptr;
    	odd = *odd_elt_sum_ptr;
    	
    	printf("the even sum is %d\n", even);
    	printf("the odd sum is %d\n", odd);
    	
    	return 0;
    }
    
    
    void sum(int a[], int n, int *even_elt_sum_ptr, int *odd_elt_sum_ptr)
    {
    	int x;
    	
    	for (x=0;x<n;x++){
    		if (a[x] / 2 == 0){
    			*even_elt_sum_ptr = *even_elt_sum_ptr + a[x];
    		}
    		else
    			*odd_elt_sum_ptr = *odd_elt_sum_ptr + a[x];
    	}
    }
    The code is intended to take an array of 10 integers and return the sum of all of the odd elements and a separate sum for all of the even elements. As I am still learning I am open to any comments/suggestions on improvements that you see. Thanks in advance!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Inside main, remove even_elt_sum_ptr and odd_elt_sum_ptr and pass the addresses of even and odd directly to the function.

    Also, if you want to find out if a number is even or odd, you need modulo, not divide.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    2

    Thanks!

    Quote Originally Posted by GReaper View Post
    Inside main, remove even_elt_sum_ptr and odd_elt_sum_ptr and pass the addresses of even and odd directly to the function.

    Also, if you want to find out if a number is even or odd, you need modulo, not divide.
    Thank you so much that fixed it, I did not realize that I was trying to pass the address of a pointer which made no sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'strcmp' incompatible pointer type Error
    By Anitha Uday in forum C Programming
    Replies: 3
    Last Post: 08-01-2013, 11:02 AM
  2. Incompatible pointer type
    By krieghart in forum C Programming
    Replies: 2
    Last Post: 12-04-2010, 05:04 PM
  3. incompatible pointer type?
    By kezman in forum C Programming
    Replies: 3
    Last Post: 04-22-2009, 04:42 PM
  4. Incompatible pointer type !?
    By nilathinesh in forum C Programming
    Replies: 14
    Last Post: 11-13-2006, 06:22 AM
  5. incompatible pointer type
    By krappa in forum C Programming
    Replies: 9
    Last Post: 04-21-2005, 11:13 AM

Tags for this Thread