简体   繁体   中英

Implicit declaration of function is invalid in C99

I am new to C language and I am having a problem that I really don't understand. I am trying to get an array from another function but when I try to extract the information, it gives me the following warning:

Implicit declaration of function 'getk_vector_calculation' is invalid in C99 Array initializer must be an initializer list or string literal

Here is the code:

int k_vector_calculation(int krec[3])

{
    ...

    krec [0] = l*u[0]+m*v[0]+o*[0] ;

    krec [1] = l*u[1]+m*v[1]+o*[1] ;

    krec [2] = l*u[2]+m*v[2]+o*[2] ;

    return k_vector_calculation( &krec[3] ) 

}

int main ()

{

    char krec[3] = getk_vector_calculation(&krec[3]); 

    ...

}

in your main() the function you called is getk_vector_calculation() [which is not k_vector_calculation() ] and which is not declared or defined before the usage.

To resolve this,

  1. either #include the header file containg the declaration of getk_vector_calculation() in your sorce file. [Considering getk_vector_calculation() is in some other file]
  2. or, add a forward declaration of getk_vector_calculation() before main() and define getk_vector_calculation() somewhere.

To know more about implicit declaration, you can check this question .


EDIT:

As others have pointed out, there are many more problems in your code snippet. But since the question title is limited to implicit declaration error, IMO, this answer should do the needful. The remaining error(s) is(are) altogether a different aspect.

In older versions of C, functions that had not been declared yet were still able to be called, and it was assumed that they returned int and took an unspecified number of arguments. Strictly speaking, in C99, it is not permitted to call a function without declaring it first.

In your case however, you are trying to call a function called getk_vector_calculation but you have defined a function called k_vector_calculation (no get at the beginning).

You are also trying to initialise an array using a function, which is not permitted (in C, functions cannot return arrays). Simply declare the array and call k_vector_calculation as a separate statement, eg:

int krec[3] = {0};
k_vector_calculation(krec);

Don't use &krec[3] as this points to an invalid location. Use &krec[0] to provide the address of the first element in the array, or equivalently just krec will do. Nb also that you declare an array of type char , but your function accepts a pointer to int , and these types are not compatible. Your function also calls itself unconditionally so there is a guaranteed infinite recursion if the snipped out code does not conditionally return. If your function doesn't need to call itself, and it doesn't return a value of any importance, change the return type to void to indicate it has no return value.

Since you are using C99, you can take advantage of using the static keyword in your function's parameter declaration:

void k_vector_calculation(int krec[static 3])
{
    // ... other code here ...
    krec[0] = l*u[0]+m*v[0]+o*[0];
    krec[1] = l*u[1]+m*v[1]+o*[1];
    krec[2] = l*u[2]+m*v[2]+o*[2];
}

The above code declares a function that takes as an argument an array of at least 3 int .

Several issues, here:

  1. As Sourav Ghosh pointed out, you define k_vector_calculation() , but then try to call getk_vector_calculation() . You have to use the right names.

  2. You say you want to "get an array from another function" - you just can't do this in C.

  3. You don't show all your code for k_vector_calculation() , but as shown, this function will call itself forever, because the last thing it does is to unconditionally call itself again. If you have a recursive function, you need to give it a way to terminate.

  4. &krec[3] is the address of the fourth element of the array k , which is not want you want to be doing, here, especially since your array only contains 3 elements. To refer to the array itself, just use krec .

  5. char krec[3] = getk_vector_calculation(&krec[3]); is invalid for numerous reasons. One, you can't initialize arrays in this way in C. Two, see point (4) for your argument. Three, even if you could initialize arrays this way in C, you'd be trying to pass an uninitialized array to a function, initialize it in there, and then try to initialize your original array with the result. It just makes no sense.

  6. You also write your functions to work with an array of int , but declare krec in main() as an array of char .

It's not clear what you want k_vector_calculation() to do, but you just can't return arrays in C like that. Probably what you want to do is just pass the array to the function, have the function work on in, and return nothing. For instance:

#include <stdio.h>

void k_vector_calculation(int kvec[])
{
    kvec[0] = 1;
    kvec[1] = 2;
    kvec[2] = 3;
}

int main(void)
{
    int kvec[3];
    k_vector_calculation(kvec);

    for ( int i = 0; i < 3; ++i ) {
        printf("kvec[%d] is %d.\n", i, kvec[i]);
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM