Home > Mobile >  Implicit declaration C
Implicit declaration C

Time:08-28

I wish to get my code cleaner, the code can compile, but unfortunately there are still some stuff showing minor problem about this following "error message"

how can I solve this ?

#include <stdlib.h>
#include <stdio.h>

int main(){

  int arr0[] = {1,2,3,4,5};
  int arr1[] = {2,2,2,2,2};
  int arr2[] = {1,4,2,4,4};


  int sizeArr0 = sizeof(arr0);
  int sizeArr1 = sizeof(arr1);
  int sizeArr2 = sizeof(arr2);

  parseArray(arr0[0], sizeArr0);
  parseArray(arr1[0], sizeArr1);
  parseArray(arr2[0], sizeArr2);

}


  int parseArray(int ch[], int sizeValue){

  int sum;
    for(int x = 0; x < ch; x  ){
      int ch[x];

        if(x == 5){
        sum  = 5;
        }

if (sum == 15){
      return sum;
        }
    }
  }
warning: implicit declaration of function ‘parseArray’ [-Wimplicit-function-declaration]
   17 |   parseArray(arr0[0], sizeArr0);
      |   ^~~~~~~~~~
test.c: In function ‘parseArray’:
test.c:30:22: warning: comparison between pointer and integer
   30 |     for(int x = 0; x < ch; x  ){

CodePudding user response:

  1. You need to have a function definition or prototype before the function which calls it
int parseArray(int ch[], size_t sizeValue);

int main()
{
    /* ... */
  1. for(int x = 0; x < ch; x ){ makes no sense and I believe that an typo. for(size_t x = 0; x < sizeValue; x ){

  2. int sizeArr0 = sizeof(arr0); is giving you the size of the array in char not in element types. You need to divide it by the size of the elements. It should also have different type (size_t) size_t sizeArr0 = sizeof(arr0) / sizeof(arr0[0]);

  3. All local function variables have to be initialized as they are not zeroed as global variables. int sum = 0;

  4. You pass the first element to the array not the reference to the array parseArray(arr0, sizeArr0); or parseArray(&arr0[0], sizeArr0);

  •  Tags:  
  • c
  • Related