c - 将数组作为参数传递的问题

标签 c arrays

我是 C 语言的新手程序员,遇到了一个简单到令人痛苦的问题。我正在编写一个创建两个数组的基本程序,一个是学生姓名,一个是学生 ID 号,然后对它们进行排序并以各种方式打印它们,最后允许用户按 ID 号搜索数组。这是代码:

#include <stdio.h>
#include <string.h>
#define ARRAY_SIZE 3
#define MAX_NAME_LENGTH 32

int main()
{   
    // Student info arrays
    char NAME[ARRAY_SIZE][MAX_NAME_LENGTH];
    int ID[ARRAY_SIZE];

    // Array for student IDs, shifted twice to the right
    int shiftedID[ARRAY_SIZE];

    // Boolean value to keep while loop running and
    // the ID search prompt repeating
    int loop = 1;

    // Counter variable for the for loop   
    int counter;
    // Gets input values for the student info arrays
    for (counter = 0; counter < ARRAY_SIZE; counter++)
    {   
        printf("Input student name: ");
        scanf("%s", NAME[counter]);

        printf("Input student ID: ");
        scanf("%d", &ID[counter]);
    }

    // Sorts the arrays
    sort(NAME, ID);

    // Prints the arrays
    print_array(&NAME, ID);

    // Shifts the ID value two bits to the right
    shiftright(ID, shiftedID);

    print_array(NAME, shiftedID);

    // Repeatedely prompts the user for an ID to
    // search for
    while(loop == 1)
    {
        search_id(NAME, ID);
    }
}

下面是函数定义:

#define ARRAY_SIZE 3
#define MAX_NAME_LENGTH 32
// Sorts the two arrays by student ID. (Bubble sort)
void sort(char **nameArray, int idArray[])
{   

    // Counter variables for the for loop
    int firstCounter = 0;
    int secondCounter = 0;
    for(firstCounter = 0; firstCounter < ARRAY_SIZE; firstCounter++)
    {
        for(secondCounter = 0; secondCounter < ARRAY_SIZE - 1;   
                secondCounter++)
        {
            if(idArray[secondCounter] > idArray[secondCounter + 1])
            {

                // Temporary variables for the sort algorithm
                int tempInt = 0;
                char tempName[32];

                tempInt = idArray[secondCounter + 1];
                idArray[secondCounter + 1] = idArray[secondCounter];
                idArray[secondCounter] = tempInt;

                strcpy(tempName, nameArray[secondCounter + 1]);
                strcpy(nameArray[secondCounter + 1],   
                      nameArray[secondCounter]);
                strcpy(nameArray[secondCounter], tempName);
            }
        }
    }
}
// Searches the ID array for a user input student
// ID and prints the corresponding student's info.
void search_id(char **nameArray, int idArray[])
{
    // A boolean value representing whether or not
    // the input ID value was found
    int isFound = 0;

    // The input ID the user is searching for
    int searchID = 0;

    printf("Input student ID to search for: ");
    scanf("%d", &searchID);

    // Counter variable for the for loop
    int counter = 0;
    while (counter < ARRAY_SIZE && isFound == 0)
    {
        counter++;
        if (idArray[counter] == searchID)
        {
            // Prints the name associated with the input ID
            isFound = 1; 
            printf("%s", nameArray[counter]);
        }
    }

    // If the input ID is not found, prints a failure message.
    if (isFound == 0)
    {
        printf("ID not found.\n");
    }
}

// Prints the name and ID of each student.
void print_array(char **nameArray, int idArray[])
{   
    // Counter variable for the for loop
    int counter = 0;

    printf("Student Name & Student ID: \n");
    for (counter = 0; counter < ARRAY_SIZE; counter++)
    {   
        printf("%s --- %d\n", nameArray[counter], idArray[counter]);
    }
} 

// Shifts the ID value to the right by two bits
void shiftright(int idArray[], int shiftedID[])
{
    // Counter variable for the for loop
    int counter = 0;
    for (counter = 0; counter < ARRAY_SIZE; counter++)
    {
        shiftedID[counter] = idArray[counter] >> 2;
    }
}

我知道这个程序在本质上是相当基础的,最重要的是,它是一种练习,可以让我更精通 C 等语言。我已经为此工作了一段时间,并且一直在努力通过几个问题,但似乎停留在三个问题上:

  1. 如果输入的 ID 号没有按顺序输入,则会导致段错误。如果身份证号码已经按顺序输入,则排序功能不会通过 if 语句,不会出现问题。

  2. 将名称/ID 数组传递给 print_array 函数时,可以很好地打印 ID,但名称将完全空白或打印为一系列奇怪的字符。

  3. 在程序结束时按 ID 搜索时,首先输入的 ID 编号(即 ID[0] 中的编号)显示未找到 ID 消息,其中索引为 1 或更大的所有编号将正常工作 - 除了应打印的相应名称被打印为空白,如第二期中所述。

如果我能得到任何建议,我将不胜感激!我发现 C 所需的精细细节背后的力量既非常有趣,但也非常令人困惑,令人生畏,这意味着我可以获得的任何帮助都会产生很大的不同。

最佳答案

问题是您假设 char [ARRAY_SIZE][MAX_NAME_LENGTH]char ** 可以互换

void sort(char **nameArray, int idArray[])

应该是

void sort(char nameArray[][MAX_NAME_LENGTH], int idArray[])

void sort(char (*nameArray)[MAX_NAME_LENGTH], int idArray[])

为了使用指向 MAX_NAME_LENGTH char 数组的指针,同样适用于您的 search_id 函数。

查看question 6.13 of C-FAQ

关于c - 将数组作为参数传递的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36784324/

相关文章:

c - 将字符插入cstring

c++ - 在 Mac OS X Lion 上使用 OpenMP 编译失败(memcpy 和 SSE 内在函数)

c++ - 打印 vector 的 vector

java - android/java - 在条件内创建对象时无法提供类

java - 从另一个类调用 String[] 的空指针异常

有人可以解释一下我如何使用来自 grub 的 C 数据结构吗?我不明白 hi mem 和 lo mem

c - 看看并说顺控程序未输出正确的序列

c - 在 C 中释放内存

java - 类文件中变量的初始值存储在哪里

c - 在 C 中搜索二维数组中的特定值