简体   繁体   English

相同的代码在 main 中有效,但在从 C 中的 function 调用时无效

[英]Same code works in main, but not when calling from a function in C

Okay so I'm a beginner programmer so any tips on any part of the code are greatly appreciated, but the main question is why does the code in function int longestSequence(int n,int array[n]);好的,所以我是初学者程序员,因此非常感谢任何有关代码任何部分的提示,但主要问题是为什么 function int longestSequence(int n,int array[n]);中的代码work when placed in main, but not when called from the function?放在 main 中时工作,但从 function 调用时不工作?

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

int longestSequence(int n,int array[n]);

int main()
{
    int n;
    scanf("%d", &n);

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

    int arraySize = n*n;
    int array[arraySize];
    int arrayIndex = 0;

    for(int i=0; i<n; i++){
        if(i%2 == 0){
            for(int j = 0; j<n; j++){
                array[arrayIndex++] = mat[i][j];
            }
        }else{
            for(int j = n-1; j>=0; j--){
                array[arrayIndex++] = mat[i][j];
            }
        }
    }

/// Here's the same code that works when in main
//    int numOfSequental = 0;
//    int maxNumOfSequental = INT_MIN;
//        for(int i = 0; i<n; i++){
//        if(niz[i] == (niz[i+1]-1)){
//            numOfSequental++;
//            if(numOfSequental>maxNumOfSequental){
//                maxNumOfSequental = numOfSequental;
//            }
//            continue;
//        }
//        numOfSequental = 0;
//    }

//calling the function in printf
    printf("Length of the sequence: %d", longestSequence(arraySize, array[arraySize]));
    return 0;
}

int longestSequence(int n,int array[n])
{
    int numOfSequental = 0;
    int maxNumOfSequental = INT_MIN;
        for(int i = 0; i<n; i++){
        if(array[i] == (array[i+1]-1)){
            numOfSequental++;
            if(numOfSequental>maxNumOfSequental){
                maxNumOfSequental = numOfSequental;
            }
            continue;
        }
        numOfSequental = 0;
    }
    return maxNumOfSequental+1;
}

"the main question is why does the code in function int longestSequence(int n,int array[n]); work when placed in main, but not when called from the function?" “主要问题是为什么 function int longestSequence(int n,int array[n]);中的代码在放在 main 中时有效,但在从 function 调用时无效?”

As called it should not work in either place.正如所谓的,它不应该在任何一个地方工作。

printf("Length of the sequence: %d", longestSequence(arraySize, array[arraySize]));
//                                                                   ^^^^^^^^^^^
return 0;

Note first that the index passed: arraySize is one beyond the legal index for array .首先注意传递的索引: arraySize超出了array的合法索引。 In C, indexing is zero based, and so it goes from 0 - arraySize - 1在 C 中,索引是从零开始的,所以它从0 - arraySize - 1开始
More importantly though the 2nd argument of longestSequence should be a pointer to the array, not an indexed element of the array.更重要的是, longestSequence的第二个参数应该是指向数组的指针,而不是数组的索引元素。

printf("Length of the sequence: %d", longestSequence(arraySize, array));
return 0;

Also, in general, to compare subsequent numbers in an array with size n , the range of comparisons should be limited to:此外,一般来说,要比较大小n的数组中的后续数字,比较范围应限制为:

a[i] == a[i+1] //for i == 0 through i == n-1

Change:改变:

 for(int i = 0; i<n; i++){
    //          ^^^
    if(array[i] == (array[i+1]-1)){//array out of bounds when i == n
    //                    ^^^

To

 for(int i = 0; i<n-1; i++){
    //          ^^^^^  
    if(array[i] == (array[i+1]-1)){//i will never reach n

EDIT:编辑:
One last thing addresses comment about replacing calls to scanf() with using the 2nd argument of main.最后一件事解决了关于使用 main 的第二个参数替换对scanf()的调用的评论。 First to do that the code must include the prototype of main: int main(int argc, char *argv[]);首先,代码必须包含 main 的原型: int main(int argc, char *argv[]); . . With this prototype, the program as called from the command line can now include command line arguments, eg: if running from CMD prompt in Windows:有了这个原型,从命令行调用的程序现在可以包含命令行 arguments,例如:如果从 Windows 中的 CMD 提示符运行:

C:\dev> myProg.exe 3 1 2 3 4 5 6 7 8 9

Inside your program then arguments of argc and argv[]` are populated as follows:在您的程序中, argc和 argv[]` 的 arguments 填充如下:

argc == 11 //total number of arguments
argv[0] == "myProg.exe" //program name is alway in argv[0]
argv[1] == "3"
argv[2] == "1"
...
argv[10] == "9"

Which should translate to creating a 3x3 array populated with the 9 subsequent values.这应该转化为创建一个填充了 9 个后续值的 3x3 数组。

So the first statements in your code could now be: (in psuedo code)因此,您的代码中的第一条语句现在可以是:(在伪代码中)

int n = atoi(argv[1]);//check value of n before using
int array[n][n];
int index = 2;
for(int i = 0; i<n ; i++)
    for(int j = 0; j<n ; j++)
        array[i][j] = atoi(argv[index]);
        index++;

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

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