简体   繁体   English

如何在C中定义数组

[英]How to define an array in C

I'm reading the book "Cracking the Coding Interview" which contains several examples of algorithms in C. I'd like to make programs which implement these algorithms and run them as I go along. 我正在阅读“破解编码面试”一书,其中包含C语言中的几个算法示例。我想编写实现这些算法并在运行过程中运行它们的程序。

One such algorithm is "Min and Max 1" (from the "Big O" chapter): 一种这样的算法是“ Min and Max 1”(来自“ Big O”一章):

int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int x : array) {
  if (x < min) min = x;
  if (x > max) max = x;
}

I've attempted to 'write a program around this' as follows: 我试图如下编写“围绕此程序”:

#include<stdio.h>

int array[5] = [1, 3, 2, 5, 4];

int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;

int main(void):
{
  for (int x : array) {
    if (x < min) min = x;
    if (x > max) max = x;
  }
  printf("The minimum is %i", min)
  printf("The maximum is %i", max)
}

However, if I try to compile and run this I get the error: expected identifier before numeric constant int array[5] = [1, 3, 2, 5, 4]; 但是,如果我尝试编译并运行它,则会error: expected identifier before numeric constant int array[5] = [1, 3, 2, 5, 4]; . How would I correctly implement this algorithm for this example input array? 如何为示例输入数组正确实现此算法?

What you mean is the following 你的意思是以下

#include <stdio.h>
#include <limits.h>

#define N   5

int main( void ) 
{
    int array[N] = { 1, 3, 2, 5, 4 };

    int min = INT_MAX;
    int max = INT_MIN;

    for ( size_t i = 0; i < N; i++ )
    {
        if ( array[i] < min ) min = array[i];
        if ( max < array[i] ) max = array[i];
    }

    printf( "The minimum is %i\n", min );
    printf( "The maximum is %i\n", max );

    return 0;
}

The program output is 程序输出为

The minimum is 1
The maximum is 5

As for your program then it contains invalid constructions according to the C grammar. 对于您的程序,根据C语法,它包含无效的构造。

In C++ the loop can look the same way as you showed. 在C ++中,循环的外观与您所显示的相同。

#include <iostream>
#include <limits>

int main() 
{
    const size_t N = 5;
    int array[N] = { 1, 3, 2, 5, 4 };

    int min = std::numeric_limits<int>::max();
    int max = std::numeric_limits<int>::min();

    for ( int x : array )
    {
        if ( x < min ) min = x;
        if ( max < x ) max = x;
    }

    std::cout << "The minimum is " << min << std::endl;
    std::cout << "The maximum is " << max << std::endl; 

    return 0;
}

Take into account that there is no sense to declare the array like global. 考虑到没有必要将数组声明为全局数组。

As for the array definition in C then you can define it either like 至于C中的数组定义,则可以像

int array[5] = { 1, 3, 2, 5, 4 };

(or using some named constant instead of the number 5) (或使用一些命名常量而不是数字5)

or like 或喜欢

int array[] = { 1, 3, 2, 5, 4 };

In the last case the number of elements is equal to the number of the initializers. 在最后一种情况下,元素的数量等于初始化程序的数量。 Or even you can use the following initialization 或者甚至可以使用以下初始化

int array[] = { [0] = 1, [1] = 3, [2] = 2, [3] = 5, [4] = 4 };

A few issues: 一些问题:

  1. int array[5] = [1, 3, 2, 5, 4]; needs to be int array[] = {1, 3, 2, 5, 4}; 需要为int array[] = {1, 3, 2, 5, 4};

  2. The short form for loop doesn't exist in C. (Are you using a C++11 compiler to compile C code? If so then saving your source file with a .c extension might be a simple way of putting it into C mode.) Use for (size_t i = 0; etc. instead and access the array elements by the index i . 缩写形式for循环不C.(您使用的是C ++ 11编译器来编译C代码吗?如果这样的话保存源文件与存在.c扩展可能是把它变成C模式的简单方法。)改为使用for (size_t i = 0;等),并通过索引i访问数组元素。

  3. Using min and max as variable names is to be discouraged as they frequently appear as macro definitions. 不建议使用minmax作为变量名,因为它们经常作为宏定义出现。

Use C syntax to implement it in C. 使用C语法在C中实现它。

  • Use {} , not [] , to define the initial values of arrays. 使用{}而不是[]来定义数组的初始值。
  • Use INT_MIN and INT_MAX from limits.h to use minimum and maximum values of int . 使用limits.h INT_MININT_MAX可以使用int最小值和最大值。
  • Don't place a colon after int main(void) . 不要在int main(void)之后放置冒号。
  • for (int x : array) is not supported in C. Use one of the supported forms of loops. C中不支持for (int x : array) 。使用一种受支持的循环形式。
  • Semicolons are required after each statements. 每个语句后都需要分号。

Here is an implementation in C99: 这是C99中的实现:

#include<stdio.h>
#include<limits.h>

int array[5] = {1, 3, 2, 5, 4};

int min = INT_MAX;
int max = INT_MIN;

int main(void)
{
  for (size_t i = 0; i < sizeof(array) / sizeof(*array); i++) {
    int x = array[i];
    if (x < min) min = x;
    if (x > max) max = x;
  }
  printf("The minimum is %i", min);
  printf("The maximum is %i", max);
}

Array is initialized inside {} not [] 数组在{}而不是[]内部初始化

Looks like you copied it from wrong place or you are a java Programmer. 看起来您是从错误的地方复制了它,或者您是Java程序员。

#include<stdio.h>

int a[5] = {1, 3, 2, 5, 4};

int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;


int main(void):
{

  for (int x : array) {
    if (x < min) min = x;
    if (x > max) max = x;
  }
  printf("The minimum is %i", min)
  printf("The maximum is %i", max)
}

This will still give you error if you want i can fix it. 如果您希望我可以解决它,这仍然会给您错误。 I left it so that you can learn this by your own. 我离开了它,以便您可以自己学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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