在C程序中获得两个错误

时间:2017-06-23 17:55:28

标签: 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;,如果我已经在main函数中声明了它。

非常感谢任何建议。谢谢 PS,这是C分配介绍,我知道这很糟糕。但如果我能让它发挥作用,我可以忽略这些警告。

2 个答案:

答案 0 :(得分:0)

您的代码可以编译许多错误。在使用指针方面,代码中似乎存在许多基本错误。我将尝试在您的代码中指出其中的一些,您可以先从纠正它们开始。我假设您正在寻找使用'size'元素初始化数组。

int *array[size];

在switch:case 1中,上面的声明意味着你正在初始化一个'size'数组的整数指针。您可能希望将其初始化为int *array = (int *)malloc(sizeof(int) * size);

接下来,对于所有功能,您需要将传递给函数的参数作为整数指针。我将纠正你的arrayelement()函数,你也可以对其他函数进行相同的更改。同样在for循环中,您正在计算错误的数组长度。这是arrayElement()函数的更正版本:

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

同样将传递给所有函数的参数更改为funcName(int *array)。并且还将数组的大小计算为:((strlen(array))/sizeof(int))

'size'是main()函数的局部变量。如果您不想在所有函数中初始化它,您可以将其作为参数传递给所有函数,或者您可以将'size'作为全局变量。这些是代码中一些非常明显的错误。

答案 1 :(得分:0)

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

#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");
        }
    }
}
  

注意:你还没有定义ascending()和descending()函数,这就是我对它们进行了评论的原因。请为这些功能编写必要的代码。