What’s wrong with this array of pointers in C? (incompatibility?)

Asked

Viewed 67 times

1

Hello, recently I was doing a relatively simple test to try to unify some concepts I have been studying, and which you will see below. It is an array of pointers that I used as a parameter of a function to add the values of this after picking them in the main function. See:

   #include <stdio.h>


void sum_all(int *pi[7]){
int i=0, sum;
    while(i  < 7){
      sum = *pi[i];
      printf("%d", sum);
      i+=1;
           }

    }



void main (){
   int vector[7] = {3,7,2,5,6,8,1};
   sum_all( &vector[7]);

}

I’m getting the second error on line 16: "Warning: Passing argument 1 of "sum_all" makes Pointer from integer without a cast"

 sum_all( &vector[7]);

Apparently I am doing something wrong in relation to the parameter but I do not know what for sure, despite the theory that occurred to me that I am violating some rule of passing parameter. who can help me would be grateful.

Update: After solving the problem, the code looks like this:

#include <stdio.h>
void sum_all(int *pi){
int i=0, sum=0;
   while(i  != 6){
    sum += pi[i];

    i+=1;
            }
printf("%d",sum );
    }



void main (){
   int vector[7] = {3,7,2,5,6,8,1};
   sum_all(vector);

}

Thanks to those involved.

  • Good evening, no need to use & to pass an array to a function.

  • When you pass as parameter: &vector[7]está passando o endereço do oitavo elemento do array mas lembre-se que se você declarou um array com 7 elementos e portanto os índices irão variar de 0 a 6. Para o que deseja use:void sum_all(int *pi){e invoque com:sum_all(vector);`.

  • Thank you, your tips helped a lot and the program worked, however here is the curiosity: in the parameter of the function "sum_all" because exactly it does not allow me to use the brackets? I read somewhere that he interprets this as a pointer to a pointer but it’s not clear.

1 answer

1


What is happening in your code is that you declare vector as an int array (int*) and its sum_all() function expects to receive an int array (int**), are different data types.

Another point is that when a matrix is used in an expression or declared as a function parameter it decays into a pointer that points to the first element and the information of how many elements it has is lost.

It means that: void sum_all(int *pi[7]) will be converted to: void sum_all(int **pi).

For its function sum_all() to know how many elements have a matrix it would be necessary to pass this information through another parameter.

I made an example where the function sum_all() expects to receive an int matrix in the first parameter and the matrix length in the second parameter and returns the sum of the matrix elements for the caller.

No need to use operator & to pass an array as argument or access matrix elements because the matrix name is already a pointer to the first element.

#include <stdio.h>

int sum_all(int *array, int length) {
  int accumulator = 0;

  for(int i = 0; i < length; ++i) {
    accumulator += array[i];
  }

  return accumulator;
}

int main() {
  int vector[] = { 3, 7, 2, 5, 6, 8, 1 };

  int length = (sizeof(vector) / sizeof(vector[0]));

  int sum = sum_all(vector, length);

  printf("%d\n", sum);

  return 0;
}
  • What exactly the line: "int length = (sizeof(vector) / sizeof(vector[0]));" does?

  • @This line calculates the length of the matrix automatically by dividing the total size of the matrix that would be sizeof(vector) by the size of the type of matrix that would be sizeof(vector[0]). With this you can add or remove elements from the matrix without worrying about manually calculating the number of elements in the matrix.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.