1

I had to write a program which has a function that sums all positive number in an array, but using function parameter array as an pointer. There in main function a problem occurs, when i try to call sum function. here is my source code:

#include <stdio.h>
#include <conio.h>
int posit(int *x[], int n){
  int s=0;
  for(int i=0; i<n; i++){
    if(*x[i]>0){
      s=s+*x[i];
    }
  }
  return s;
}
int main(){
  int k;
  scanf("%d",&k);
  int a[k];
  for(int i=0; i<k; i++){
    scanf("%d",&a[i]);
  }
  int b=posit(&a,k);
  printf("%d\n", b);
  getch();
  return 0;
}
0

3 Answers 3

5

When an array is passed to a function it decays to a pointer to its first element. Change the signature of the function to:

int posit(int* x, int n){ /* Equivalent to 'int posit(int x[], int n)' */

and change the element access syntax of the array in the function from *x[i] to just x[i]. The invoke the function:

int b=posit(a,k);

Another point worth mentioning is to check the return value of scanf(), which returns the number of assignments made, to ensure &a[i] is actually being assigned and the program is subsequently not using uninitialized variables.

You may find the Arrays and Pointers section of the C FAQ useful.

Sign up to request clarification or add additional context in comments.

3 Comments

You saying if I write *x[] , it will be [here is my "a" array][][][][][] after calling posit //int b=posit(&a,k) //function? I wrote it as you said at first but rejected by my tutor. he making me write function like this // int posit(int *x[], int n)//
@filemonster, int* x[] is an array of int*, it is not an array of int.
@filemonster: also, &a is a pointer to an array and has type of int (*)[k]
1

Actually in your example you are passing to function pointer to pointer (please note that similar notation is very often used with main argument char *argv[]).

As mentioned in hmjd post while passing array to function this is converted to address of its first element. Instead of explicitly using pointer (int *x) you can also use array notation (int[] or even int[k]) but those are the same.

Please also note that in second method (int[k]) size provided doesn't matter and inside function it will be not known. This is only important to include size when you are dealing with multiple dimension arrays, but this is other story...

Comments

0

From K&R2

When an array name is passed to a function, what is passed is the 
location of the initial element. Within the called function, this 
argument is a local variable, and so an array name parameter is a 
pointer, that is, a variable containing an address.

So you will be passing only the first element to the function.

Once this is understood your source can be modified as follows

int posit(int *x, int n)

and to call this function from main you can use

int b=posit(a,k); //Note only the location of the first element is being passed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.