Kyle Orton
Kyle Orton

Reputation: 71

max value in array C

I'm getting a compile errors, that I can't really fix. I need to create a program that initializes an in array, then write a biggest function that takes 2 parameters, an array and it's length and returns the index of the largest element in the array. I will then call this function from main. Can anyone tell me what is the problem?

errors:part1.c: part1.c: In function 'main':
part1.c:6:3: warning: implicit declaration of function 'largest'
part1.c:7:23: error: expected expression before ']' token
part1.c: In function 'largest':
part1.c:17:4: warning: statement with no effect

Thanks!

#include <stdio.h>

int main()
{
  int myArray[]={1,2,3,4,5,6};
  largest(myArray,6);
  printf("%d",myArray[]);
  return 0;
} 

int largest(int array[], int length)
{
  length = sizeof(array)/sizeof(array[0]);
  int i = 1;
  int max = array[0];

  for(i; i<length; i++)
  {
    if(max < array[i])
    {
      max = array[i];
    }
  }
  return max;
 }  

Upvotes: 1

Views: 21793

Answers (6)

Shahbaz
Shahbaz

Reputation: 47583

C compiles your code in one pass. This means that everything should be defined before it is used. Your function largest is defined after its use, therefore once the compiler sees

largest(myArray,6);

it still doesn't know that largest exists!

The solution would be to either move the definition of largest above main, or better, forward declare the function:

#include <stdio.h>

int largest(int array[], int length);

int main()
{
    int myArray[]={1,2,3,4,5,6};
    largest(myArray,6);
    printf("%d",myArray[]);
    return 0;
} 

int largest(int array[], int length)
{
    /* implementation of largest */
}

Also, the sizeof(array) will not give you the number of elements in largest because that information is lost upon function call. You could move that expression up in the function call to compute and pass the length parameter:

largest(myArray,sizeof(myArray)/sizeof(myArray[0]));

This may also be a typo, but you probably meant to store and print the maximum value:

int max = largest(myArray,sizeof(myArray)/sizeof(myArray[0]));
printf("%d\n",max);

Upvotes: 3

Levon
Levon

Reputation: 143152

You have an error and warnings .. the error is clearly more important.

  printf("%d",myArray[]);

the %d format specification implies you want to write an int value, this is not the case, and the likely cause of your error.

There are other warnings that deserved your attention, such as not providing a function prototype for your 'largest` function etc, but those are secondary to fixing the error that prevents compilation.

Of course the warnings should also be eliminated, or a conscious decision should be made to ignore them after examining them.

Upvotes: 0

hmjd
hmjd

Reputation: 122011

Put a declaration of largest() before main() to resolve the implicit declaration warning:

int largest(int array*, int length);

int main()

The error error: expected expression before ']' token is caused by:

printf("%d",myArray[]);

To print the largest value, you need to store the result of largest() or use it as an argument to printf():

printf("%d", largest(myArray, 6));

This is not what you expect:

length = sizeof(array)/sizeof(array[0]);

as arrays decays to pointers when passed as arguments. It is equivalent to:

length = sizeof(int*)/sizeof(int);

Just use the length argument to control the iteration. Recommend make the arguments to largest() const as the function does not modify them.


The warning: statement with no effect is caused by the i; in the for:

for(i; i<length; i++)

change to:

for(; i<length; i++)

or:

for(i = 0; i<length; i++)

or if C99:

for(int i = 0; i<length; i++)

Upvotes: 1

asveikau
asveikau

Reputation: 40264

Many people have pointed out many issues but surprised no one has mentioned this:

int largest(int array[], int length)
{
  length = sizeof(array)/sizeof(array[0]);

Nope, sizeof does not do what you appear to think it does here. It does not magically know the size of your allocations, only takes the size of the underlying type. What you have done is equivalent to sizeof(int*)/sizeof(int).

You should trust the length parameter the caller gave you. There's no way to get the actual size of the array using sizeof, only let the caller tell you how big it is.

Upvotes: 0

vilja
vilja

Reputation: 11

You could also just move the definition of largest() above the definition of main() and it would work.

Upvotes: 0

Eric J.
Eric J.

Reputation: 150208

ISSUE 1

You use largest() in main() before you define it. Use a prototype, or move the definition above main().

ISSUE 2

In:

length = sizeof(array)/sizeof(array[0]);

You declare length as int length, but assign to it something of type size_t. That caused the error error: 'length' redeclared as different kind of symbol in the original version of your question.

ISSUE 3

In

for(i; i<length; i++)

you do not assign a value to i. Did you mean

for(i=0; i<length; i++)

? Although you did previously assign a value to i, I believe this is causing warning: statement with no effect (though hard to be sure without line numbers in the provided code).

Also, arrays in C are 0-based. You probably want to initialize i to 0 rather than 1.

ISSUE 4

In the line

printf("%d",myArray[]);

you use %d as a formatting specifier, which means that the supplied argument is expected to be an integer value. You supply an array instead.

Upvotes: 6

Related Questions