我是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.我已经工作了一段时间的语言,并通过几个问题的工作,但似乎被卡住在三个问题上:
如果输入ID号没有t已经按顺序输入,则会导致分段错误。如果已经按顺序输入了ID号码,则排序功能不会通过if语句,并且不会出现问题。
将名称/ ID数组传递给print_array函数时,ID会被打印得很好,但名称将被打印为完全空白或作为一系列奇怪字符。
当在程序结束时通过ID进行搜索时,首先输入的ID号码(因此,ID [0]中的号码)显示ID未找到消息,其中索引1或更大的所有数字将会正如第二期中提到的那样,除了应打印的相应名称空白之外,还可以正常工作。
任何意见,我可以得到非常感谢!我发现C语言所需的细节背后的力量非常有趣,但也非常令人困惑,令人生畏,这意味着我能得到的任何帮助都会产生重大影响。
2D数组不是双指针('char **')。 – BLUEPIXY
参数应该是'char nameArray [ARRAY_SIZE] [MAX_NAME_LENGTH]'以匹配您传递的数组。你可以省略'ARRAY_SIZE' –