c - 相同的代码在 main 中有效,但在从 C 中的函数调用时无效

标签 c function function-call

好吧,我是一名初学者程序员,因此非常感谢有关代码任何部分的任何提示, 但主要问题是为什么函数 int longsequence(int n,int array[n]); 中的代码在放置在 main 中时可以工作,但在从函数调用时却不能工作?

#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?"

正如所谓的,它不应该在任何一个地方工作。

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

首先请注意,传递的索引:arraySize 超出了array 的合法索引。在 C 中,索引是从零开始的,因此它从 0 - arraySize - 1
更重要的是,longestSequence 的第二个参数应该是指向数组的指针,而不是数组的索引元素。

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

此外,一般来说,要比较大小为 n 的数组中的后续数字,比较范围应限制为:

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

更改:

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

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

编辑:
最后一件事解决了有关使用 main 的第二个参数替换对 scanf() 的调用的评论。首先,代码必须包含 main 的原型(prototype):int main(int argc, char *argv[]);。有了这个原型(prototype),从命令行调用的程序现在可以包含命令行参数,例如:如果在 Windows 中从 CMD 提示符运行:

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

在程序内部,argc 和 argv[]` 的参数填充如下:

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"

这应该转化为创建一个 3x3 数组,并填充 9 个后续值。

因此,代码中的第一条语句现在可以是:(在伪代码中)

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++;

关于c - 相同的代码在 main 中有效,但在从 C 中的函数调用时无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70787853/

相关文章:

c - 几何序列打印……吓坏了

c - linux 驱动程序间通信

c - 如何将结构从一个正在执行的程序传递到另一个程序的可执行文件并返回一些字符串

c# - 当我不能使函数本身成为静态时如何调用非静态函数

javascript - 我怎样才能把这个匿名函数变成一个命名函数,这样我就可以把它移到一个外部 JS 文件中以便重用

c - 如何更改 C 函数中的结构变量?

c - LuaJIT FFI加载dll错误

function - F#:如何定义一个函数来将 2 个 float 相加?在函数签名中指定 "Float"?

javascript - 在 JavaScript 的 if 语句中使用不带括号的函数名有什么作用?

c - 我如何使字符串数组与交换函数交换它的组件?