Problems with passing arrays as parameters

I am a novice programmer in C, and I run into a problem that is almost painfully simple. I am writing a basic program that creates two arrays, one of the student names and one of the student ID numbers, then sorts them and prints them in various ways and finally allows the user to search for arrays by ID number. Here is the code:

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

And here are the function definitions:

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

I know that this program is quite simple, and most of all this exercise is for me to better understand a language like C. I worked on it for a while and worked through several problems, but it seems to be stuck in three problems:

  • If the input identification numbers are not already entered in order, a segmentation error occurs. If the identification numbers are entered in order, the sort function never passes through the if statement, and there are no problems.

  • When passing arrays of names / identifiers to the print_array function, identifiers print very well, but the names will be printed either completely empty or a series of strange characters.

  • When searching by identifier at the end of the program, the identifier number that was entered first (for example, the number in ID [0]) displays the message ID not found, where all numbers with an index of 1 or more will work fine - except for the corresponding names that should be printed as printed as blank as indicated in the second issue.

Any advice I can get would be greatly appreciated! I find that the power behind the fine details needed in C is really interesting, but also very confusing, intimidating, and that means that any help I can get is of great importance.

+5
source share
2 answers

The problem is that you think char [ARRAY_SIZE][MAX_NAME_LENGTH] and char ** are interchangeable

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

it should be

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

or

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

to use an array pointer from MAX_NAME_LENGTH char s, the same goes for your search_id function.

Take a look at question 6.13 from the C-FAQ

+5
source

I would advise you to rebuild your program. Instead of storing two independent arrays for names and identifiers, you can save one array of structures that contain all the necessary data:

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

Now you have one array that can never become "inappropriate", sorting identifiers without names, etc.

You can sort the array in C using the standard qsort() library function:

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

This requires a comparator definition, which is quite simple. One example would be:

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

You can use the same comparator with another standard library function bsearch() to search for an array of students after sorting it:

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

These standard features are more efficient than what you had and require you to write much less code with less chance of errors.

+2
source

Source: https://habr.com/ru/post/1247676/


All Articles