c - 在 C 程序中出现两个错误

标签 c arrays switch-statement

我将在下面发布我的代码,然后在下面指定我的问题。

#include <stdio.h>
#include <string.h>

//initializing the array
int arrayElements(array)
{
    for (int i = 0; i <= strlen(array); i++)
    {
        printf("Enter array element:\n");
        scanf("%d", &array[i]);
    }
    printf("Elements: ");
    for (int i = 0; i <= strlen(array); i++)
    {
        printf("%d, ", array[i]);
    }
}

//sorting the array
int arraySort(int array)
{
    char sortType;
    printf("Sort in Ascending or Descending order? [A\D]\n");
    scanf("%c", &sortType);
    while ((sortType != "A") || (sortType != "D"))
    {
        printf("Invalid selection.\n");
        printf("Sort in Ascending or Descending order? [A\D]\n");
        scanf("%c", &sortType);
    }
    if (sortType == "A")
    {
        ascending(array);
    }
    else
    {
        descending(array);
    }
}

//searching the array
int arrayFind(int * array)
{
    int searchElement;
    int *d;
    int i;
    int **string;
    printf("What element do you wish to search for?");
    scanf("%d", &searchElement);
    d = strint (array, searchElement);

    if (d != NULL)
    {
        for (i = 0; i <= strlen(array); i++)
        {
            if (string[i] == searchElement)
            {
                printf("Element found at array[%d]\n", i);
            }
        }
    }
    else
    {
        printf("Element '%d' not found\n", searchElement);
    }
}

//printing the array
int arrayPrint(array)
{
    printf("%d", array);
}

//printing array in reverse order
int arrayRevPrint(array)
{
    int **size;
    for (size = strlen(array); size >= 0 ; size--)
    {
        printf("%d",array[size]);
    }
}

int main()
{
    int userSelection;
    int size;
    int elements;
    int searchElement;
    while (userSelection != 7)
    {
        printf("Please enter your selection\n>");
        printf("1 - Enter the size of the array:\n");
        printf("2 - Enter the array elements:\n");
        printf("3 - Sort the array\n");
        printf("4 - Find a number within the array\n");
        printf("5 - Print the array\n");
        printf("6 - Reverse print the array\n");
        printf("7 - Quit\n");
        scanf("%d", &userSelection);

        switch (userSelection)
        {
        case 1 :
        size=0;

        while ((size > 20) || (size < 1))
        {
            printf("What is the size of your array? (1 - 20)\n");
            scanf("%d", &size);
            if ((size > 20) || (size < 1))
            {
                printf("Invalid selection.\n");
            }
        int *array[size];
        printf("\nSize of array: array[%d]\nReturning...", size);
        break;
        }

        case 2 :
            if (size == 0)
            {
                printf("You should first set the size of the array.\n");
            }
            else
            {
                arrayElements(array);
            }
        case 3 :
            if (elements == NULL)
            {
                printf("You should first intitialize the array.\n");
            }
            else
            {
                arraySort(array);
            }
        case 4 :
            if (elements == NULL)
            {
                printf("You should first intitialize the array.\n");
            }
            else
            {
                int arrayFind(array);
            }
        case 5 :
            if (elements == NULL)
            {
                printf("You should first intitialize the array.\n");
            }
            else
            {
                arrayPrint(array);
            }
        case 6 :
            if (elements == NULL)
            {
                printf("You should first intitialize the array.\n");
            }
            else
            {
                arrayRevPrint(array);
            }
        case 7 :
            printf("Exiting...");
                return 0;
        default :
            printf("That's not a valid choice\n");
        }
    }

}

现在,我遇到了两个主要问题。我将只发布一个,因为我为不同的行得到了一堆。

第一个是,

error at line 137: switch jumps into scope of identifier with variably modified type.

第二个是

error at line 10: subscripted value is neither an array nor pointer nor vector

另外,我试图理解为什么我必须在某些函数中重新声明 int size;,如果我已经在主函数中声明了它的话。

如有任何建议,我们将不胜感激。谢谢 PS,这是 C 赋值的介绍,我知道它很糟糕。但如果我能让它工作,我就可以忽略警告。

最佳答案

逻辑和句法上有很多错误。我删除了所有的语法错误和几乎所有的逻辑错误。我已经评论了您代码中的错误以及我如何编辑它。请查看代码。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Its a good practice to mention your definitions of functions on the top.
void arrayElements(int arr[], int size);
void arraySort(int arr[], int size);
void arrayPrint(int arr[], int size);
void arrayRevPrint(int arr[], int size); 
void arrayFind(int arr[], int size);

//initializing the array
//Function not returning anything use void instead of int
void arrayElements(int arr[], int size) // Modified signature
{
    int i;
    for (i = 0; i <= size; i++)
    {
        printf("Enter array element:\n");
        scanf("%d", &arr[i]);
    }
    //Since you just want to print the array you can directly call arrayPrint() function.
    // printf("Elements: ");
    // for (i = 0; i <= size; i++)
    // {
    //     printf("%d, ", arr[i]);
    // }
    arrayPrint(arr,size);
}

//sorting the array
//Function not returning anything use void instead of int
void arraySort(int arr[], int size) // Modified signature
{
    char sortType;
    printf("Sort in Ascending or Descending order? [A\\D]\n"); // to use an escape charachter
                                                               // use \ before them. 
                            //To know more about escape characters please surf the internet.
    scanf("%c", &sortType);
    // Character literal are always bound between single quotes.
    while ((sortType != 'A') || (sortType != 'D'))
    {
        printf("Invalid selection.\n");
        printf("Sort in Ascending or Descending order? [A\\D]\n");
        scanf("%c", &sortType);
    }
    // Where are the functions ascending and descending declared??
    // Define the functions.
    if (sortType == 'A')
    {
        //ascending(arr);  
    }
    else
    {
        //descending(arr);
    }
}

//searching the array
//Function not returning anything use void instead of int
void arrayFind(int arr[], int size)// Modified signature 
{
    int searchElement;
    //int *d; No need of this
    bool found = false; // A flag which we will turn true if element found.
    int i;
    //int **string; No need of this
    printf("What element do you wish to search for?");
    scanf("%d", &searchElement);
    //d = strint (array, searchElement); No need of this

    //if (d != NULL) 
    //{
        for (i = 0; i <= size; i++)
        {
            if (arr[i] == searchElement)
            {
                printf("Element found at array[%d]\n", i);
                found = true;
                break; // Element has been found so for coming out of the loop
            }
        }
        if (found == false)
        {
            printf("Element %d not found \n", searchElement);
        }
    // No need of the extra code below
    //}
    // else
    // {
    //     printf("Element '%d' not found\n", searchElement);
    // }
}

//printing the array
//Function not returning anything use void instead of int
void arrayPrint(int arr[], int size) // Modified signature 
{
    // You can not print array like this in c.
    //printf("%d", array);

    // To print an array in c you have to loop through it.
    int i;
    for(i=0; i<size; i++){
        printf("%d ", arr[i]);
    }
}

//printing array in reverse order
//Function not returning anything use void instead of int
void arrayRevPrint(int arr[], int size) // Modified signature
{
    int i;
    for (i=size-1; i >= 0 ; i--)
    {
        printf("%d ",arr[i]);
    }
}

int main()
{
    int userSelection;
    int size;
    bool elements = false; // I guess you wanted it to use as a flag for whether there
                  // there are elements in the array or not.
                  // In order to do that better use a boolean and set its default value
                  // to false.
    int searchElement;
    int *arr;  // Your Dynamic array
    while (userSelection != 7)
    {
        printf("Please enter your selection\n>");
        printf("1 - Enter the size of the array:\n");
        printf("2 - Enter the array elements:\n");
        printf("3 - Sort the array\n");
        printf("4 - Find a number within the array\n");
        printf("5 - Print the array\n");
        printf("6 - Reverse print the array\n");
        printf("7 - Quit\n");
        scanf("%d", &userSelection);

        switch (userSelection)
        {
        case 1 :
            size=0;

            while ((size > 20) || (size < 1))
            {
                printf("What is the size of your array? (1 - 20)\n");
                scanf("%d", &size);
                if ((size > 20) || (size < 1))
                {
                    printf("Invalid selection.\n");
                    continue; // to skip the next statements and continue the loop
                }
                arr = (int*)malloc(sizeof(int)*size); // Allocating given size to the dynamic array
                printf("\nSize of array: array[%d]\nReturning...", size);
            }
            break; // Use break between two cases of a switch if two are not meant
                   // execute in a sequence.
        case 2 :
            if (size == 0)
            {
                printf("You should first set the size of the array.\n");
            }
            else
            {
                arrayElements(arr, size); // Always send the size as when you pass array
                                          // it goes to the function as a reference to the array
                                          // Hence, you can't calculate the size in the called function.

                elements = true; // We have populated the array now so have to change the flag.
            }
            break;
        case 3 :
            if (elements == false)
            {
                printf("You should first intitialize the array.\n");
            }
            else
            {
                arraySort(arr, size); // Always send the size as when you pass array
                                      // it goes to the function as a reference to the array
                                      // Hence, you can't calculate the size in the called function.
            }
            break;
        case 4 :
            if (elements == false)
            {
                printf("You should first intitialize the array.\n");
            }
            else
            {
                arrayFind(arr, size);// Always send the size as when you pass array
                                      // it goes to the function as a reference to the array
                                      // Hence, you can't calculate the size in the called function.
            }
            break;
        case 5 :
            if (elements == false)
            {
                printf("You should first intitialize the array.\n");
            }
            else
            {
                arrayPrint(arr, size);// Always send the size as when you pass array
                                      // it goes to the function as a reference to the array
                                      // Hence, you can't calculate the size in the called function.
            }
            break;
        case 6 :
            if (elements == false)
            {
                printf("You should first intitialize the array.\n");
            }
            else
            {
                arrayRevPrint(arr, size);// Always send the size as when you pass array
                                      // it goes to the function as a reference to the array
                                      // Hence, you can't calculate the size in the called function.
            }
            break;
        case 7 :
            printf("Exiting...");
                return 0;
        default :
            printf("That's not a valid choice\n");
        }
    }
}

Note: You have not define the functions ascending() and descending() and that is why I have commented those out. Please write the necessary code for those functions.

关于c - 在 C 程序中出现两个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44727366/

相关文章:

c - 终止字符引用

c - 从3个坐标点求出三角形的面积,返回0

javascript - 是否修改对象 : to use a variable, 中的数组的性能?

javascript - 我如何从这个起始数组创建这个数组?

javascript - 是 switch(true) {... 有效的 javascript 吗?

c - C 中枚举与 switch-case 的使用

c - c 高级编程

C - 转到位于外部文件中的标签

php - 缓存 PHP 数组

c# - "switch"语句评估线程安全吗?