error: passing argument 1 of 'count_elements' makes pointer from integer without a cast [-Wint-conversion]

0

I have a program that aims to calculate the number of elements that an array has. For that the arrangement enters a feature count_elements that counts the number of items and returns the total. The issue is that when I try to show the content of the fix, it throws the following error: How can the failure or error be solved?

#include <stdio.h>
#define TAMANIO 12

int contar_elementos(int a[TAMANIO]);

int main()
{
    int a[ TAMANIO ] = { 1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45};
    printf( "El total de los elementos del arreglo es %d\n", contar_elementos(a[TAMANIO]));
    return 0;
}

int contar_elementos(int a[TAMANIO]){
    int i, total = 0;
    for ( i = 0; i < TAMANIO; i++ ) {
        total = total + 1;
    }
    return total;
}
    
asked by Alejandro Caro 07.10.2018 в 16:33
source

2 answers

0

I think this can help you a bit, maybe the code is a little longer but it gives you the solution. I hope I have helped you.

#include <iostream>

using namespace std;

int contar_elementos[100];

int main()
{
///Array con elementos.
int contar_elementos[] = { 1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45};

int tamano_del_tipo_de_dato; ///Ej. Tamaño de un Int (4 bytes), de un Char (1 byte)
int tamano_del_array; ///Aplica sizeof al array
int cantidad_elementos;

///Obtenemos el tamano de un int y del array usando sizeof.
tamano_del_array = sizeof(contar_elementos);
tamano_del_tipo_de_dato = sizeof(int);

///Para calcular la cantidad de elementos
///Dividimos el tamano del array, entre el tamano de su tipo de dato, por ejemplo enteros.
cantidad_elementos = tamano_del_array / tamano_del_tipo_de_dato;

cout << endl;
cout << "Cantidad de elementos: " << cantidad_elementos << endl;

return 0;
}
    
answered by 07.10.2018 в 17:29
0

Two things: 1 You have to dereference the array, with an & amp; 2 You have to stop the console Complete program:

#include <stdio.h>
#include <conio.h>
#define TAMANIO 12

int contar_elementos(int a[TAMANIO]);

int main()
{
    int a[ TAMANIO ] = { 1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45};
    printf( "El total de los elementos del arreglo es %d\n", contar_elementos(&a[TAMANIO]));
    getch();
    return 0;
}

int contar_elementos(int a[TAMANIO]){
    int i, total = 0;
    for ( i = 0; i < TAMANIO; i++ ) {
        total = total + 1;
    }
    return total;
}
    
answered by 07.10.2018 в 20:24