0
#include <stdio.h>

int main(void) {
    int list_size;
    printf("size of the array:");
    scanf("%d", &list_size);

    int list[5];
    for (int i = 0; i < list_size; i++) {
        scanf("%d", &list[i]);
    }

    printArray(list, 5);
    return 0;
}
void printArray(int list[], int list_size){
    for (int j = 0; j < list_size; j++) {
        printf("%d ", list[j]);
    }
}

Error C2371 : 'printArray' : Override, the default format is different. How can I change the code? Is the array declaration wrong? I have to make function 'printArray', so I can't put code into main function.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • *Where* do you get this error? Please [edit] your question to include a comment in the code where you have the error. – Some programmer dude Dec 08 '20 at 09:50
  • On another couple of notes, what happens if the user inputs a value larger than `5` for `list_size`? What happens with the remaining values in `list` if `list_size` is less than `5`? Why do you hard-code the size `5` when calling `printArray`? – Some programmer dude Dec 08 '20 at 09:52
  • 1
    You are missing a function prototype for `printArray`. Either declare a prototype above the `main` function, or define your function above `main` instead. The goal is for the compiler to know what the heck `printArray` is by the time parsing reaches the place where you're trying to use it. – paddy Dec 08 '20 at 09:52

5 Answers5

1

For starters you are not using the variable list_size in the array declaration

scanf("%d", &list_size);

int list[5];

It seems you mean

int list[list_size];

Correspondingly the function printArray should be called like

printArray(list, list_size);

You have to declare the function printArray before its call for example before main

void printArray(int list[], int list_size);

int main( void )
{
    //...
}

As the function does not change the passed array then the first parameter should be declared with the qualifier const.

void printArray(const int list[], int list_size);

int main( void )
{
    //...
}

And the function can be defined like

void printArray( const int list[], int list_size ) {
    for (int j = 0; j < list_size; j++) {
        printf("%d ", list[j]);
    }
    putchar( '\n' );
}

Pay attention to that this declaration

int list[list_size];

declares a variable length array. You need to set a corresponding option of the compiler to compile your program according to the C99 Standard.

Otherwise you could define some large enough constant for the size of the array. For example

int main( void )
{
    enum { N = 100 };
    int list[N];
 
    int list_size;
    printf( "size of the array (not greater than %d): ", N );
    if ( scanf( "%d", &list_size ) != 1 || N < list_size ) list_size = N;

    //...
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Like this

/* function declaration or prototype */
void printArray(int list[], int list_size);

int main(void) {
    ...
}

void printArray(int list[], int list_size) {
    ...
}

You have to declare functions before you use them. Function declarations are also called prototypes.

john
  • 85,011
  • 4
  • 57
  • 81
0

You could've declared your function before the main() one. As for the rest, I don't see the problem with your function.

#include <stdio.h>

void printArray(int list[], int list_size)
{
    for (int j = 0; j < list_size; j++)
    {
        printf("%d ", list[j]);
    }
}

int main(void)
{
    int list_size;
    printf("size of the array:");
    scanf("%d", &list_size);

    int list[list_size];
    for (int i = 0; i < list_size; i++)
    {
        scanf("%d", &list[i]);
    }

    printArray(list, list_size);
    return 0;
}
  • I want to declare >>> int list[list_size]; <<< but then, it causes error like C2057(A constant expression is required) and C2133 ('list': Unknown size). How can I do for this error? – 아이스초코바닐라 Dec 08 '20 at 10:07
  • @아이스초코바닐라 What complier are you using, and which version of C are you specifying? I get no errors on my code. You input the size of the array, then that will be the array size, then input multiple values, then the function will print it out for you. As simple as that. –  Dec 08 '20 at 10:14
0

You should declarate the function after the headers files if you make the function after the principal program

#include <stdio.h>

 void printArray(int list[], int list_size);

int main(void) 
{
    int list_size;
    do
    {
          printf("size of the array:");
          scanf("%d", &list_size);         
    }
    int list[list_size];
    for (int i = 0; i < list_size; i++) 
    {
         scanf("%d", &list[i]);
    }
    printf("\nDisplay of array :\n");
    for (int i = 0; i < list_size; i++)
    {
         printf("%d", list[i]);
    }
    printf("\n\n");
    printArray(list, list_size);
    return 0;
}

void printArray(int list[], int list_size)
{
    printf("\nDisplay of array :\n");           
    for (int j = 0; j < list_size; j++) 
    {
        printf("%d ", list[j]);
    }
}

This code print 2 time the output of array,if you want to display the array 1 time you should remove this foor loop :

    printf("\nDisplay of array :\n"); 
    for (int i = 0; i < list_size; i++)
    {
         printf("%d", list[i]);
    }
MED LDN
  • 684
  • 1
  • 5
  • 10
0

C is parsed without looking ahead. You could write a single-pass C compiler (not the most useful thing, but the language was designed with this in mind). When the compiler sees the call to printArray, i.e. the line printArray(list, 5);, it doesn't know yet what printArray's real declaration is - it hasn't read it yet. But it has to do something with that call, and instead it makes a guess: the compiler implicitly declares, at that moment, the function as-if you had the following declaration:

int printArray();

In C, this declaration really means

int printArray(...);

And thus, the call to printArray is generated with such an implicit declaration - the function is called as if it was a variadic function. But it isn't a variadic function. You later attempt to define it in an incompatible way, as:

void printArray(int [], int);

This is not allowed and triggers an error: in C, all the declarations must agree with the definition. C allows multiple declarations - they are not an error, as long as they all agree. In your case they didn't: the implicit one didn't match the explicit one that came later.

There are two ways of fixing this error:

  1. Declare void printArray(int list[], int size); before the point of use, e.g. before main. You can still define it later - after main.

  2. Change the signature of printArray to match what the compiler implied, i.e. printArray(...) { ... }. The only problem is: such a signature would be useless. You can pass whatever arguments you want into that function, but there's no way to actually use them from inside the function. That variadic argument mechanism of C requires at least one named argument. So no luck there. A minimum viable printArray function with variadic arguments could look as follows - and of course then it's not defined as printArray(...) anymore, so the compiler would raise the error you see due to the mismatch of implicit declaration and definition, and you're back to square one.

void printArray(int list[], ...)
{
    va_list args;
    va_start(args, list);
    int count = va_arg(args, int);
    for (int i = 0; i < count; ++i) printf("%d ", list[i]);
    printf("\n");
    va_end(args);
}

The problem is in va_start: you have to pass it the first named argument to the function. With a signature like printArray(...), there are no named arguments, and thus you can't use va_start, and there's no way of accessing any arguments passed to such function from the inside of it - not while depending on Standard (portable) C. On various architectures there are non-portable "hacks" to bypass this issue, but they are not recommended at all.

Note: If you want to declare a function with no arguments in C, it should be declared as:

int myFunction(void);  /* <- like this, not like -> */ int myFunction();

To add to the potential confusion, C++ sees things differently: there, the int foo(void) is redundant, since it interprets int foo() as a function taking no parameters, and not as int foo(...) like C would.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313