2016-04-22 46 views
5

我是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号没有t已经按顺序输入,则会导致分段错误。如果已经按顺序输入了ID号码,则排序功能不会通过if语句,并且不会出现问题。

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

  3. 当在程序结束时通过ID进行搜索时,首先输入的ID号码(因此,ID [0]中的号码)显示ID未找到消息,其中索引1或更大的所有数字将会正如第二期中提到的那样,除了应打印的相应名称空白之外,还可以正常工作。

任何意见,我可以得到非常感谢!我发现C语言所需的细节背后的力量非常有趣,但也非常令人困惑,令人生畏,这意味着我能得到的任何帮助都会产生重大影响。

+0

2D数组不是双指针('char **')。 – BLUEPIXY

+0

参数应该是'char nameArray [ARRAY_SIZE] [MAX_NAME_LENGTH]'以匹配您传递的数组。你可以省略'ARRAY_SIZE' –

回答

5

的问题是,你是假设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_LENGTHchar s,同样适用于您的search_id功能。

看看到question 6.13 of C-FAQ

+1

这很快就解决了我所有的问题。感谢您的及时回复。 – Geoiv04

2

我会建议你调整你的计划。而不是存储两个独立阵列的名称和ID,可以存储包含所有必要的数据结构的一个数组:

typedef struct student 
{ 
    int id; 
    char name[MAX_NAME_LENGTH]; 
} student_t; 

student_t students[ARRAY_SIZE]; 

现在你有一个单一的阵列,它永远不能成为“不匹配”通过排序的ID不名称等

您可以使用标准库函数qsort()排序C中的数组:

qsort(students, ARRAY_SIZE, sizeof(student_t), comparator); 

这需要你定义一个比较,这是相当简单的。一个例子是:

int comparator(const void *lhs, const void *rhs) 
{ 
    const student_t *s1 = lhs, *s2 = rhs; 
    return s1->id - s2->id; 
} 

您可以使用相同的比较与其他标准库函数bsearch()寻找学生的阵列将垃圾分类后:

student_t key = { 42 }; // name doesn't matter, search by ID 
student_t* result = bsearch(&key, students, ARRAY_SIZE, sizeof(student_t), comparator); 

这些标准功能比更有效的是什么你有,并且要求你写更少的代码,更少出错的机会。

+0

这似乎是编写程序的一种更有效的方式,所以我将来可能会使用类似的东西。优秀的答案任何一种方式 – Geoiv04