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.
– teste-90
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);`.– anonimo
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.
– Victor Mariano