I am stuck on a programming assignment in C. The user has to enter a number specifying the size of an array no bigger than 10 elements which will store the names of students. This must be done in a separate function from the main function called getNames. Each name can be up to 14 characters and getNames must be invoked from within the main function. Here is my code:
#include <stdio.h>
void getNames(char *a, int n){
int i;
for(i=0; i<n; i++){
printf("Enter a name: ");
scanf("%s", &a[i]);
}
}
void printNames(char *a, int n){
int i;
for(i=0; i<n; i++){
printf("%s\n", &a[i]);
}
}
void main(){
int num; //number of names in array 'names'
printf("Num of students: ");
scanf("%d", &num);
char names[num][15]; //each name is 14 characters plus
//a null character
getNames(names[num], num);
printNames(names[num], num);
}
The code compiles and runs without syntax errors but the array is not filled correctly. For example:
Num of students: 5
Enter a name: Jeb
Enter a name: Bob
Enter a name: Bill
Enter a name: Val
Enter a name: Matt
returns:
JBBVMatt
BBVMatt
BVMatt
VMatt
Matt
Clearly there is an issue writing to the array but I am not sure what it is.
For this assignment our professor was adamant that we cannot use any global variables. His wording was rather vague about how we should implement this. My first thought would be to move the for loop in getNames into the main function and just calling getNames repeatedly but I would like a solution that incorporates that into getNames. I'm new to C, having mostly dealt with Java so please bear with me. Any help would be appreciated.
If the code you have posted is exactly the code you are using, then it is invoking undefined behavior by accessing a memory location that your program does not own: getNames(names[num], num);
.
Array indexing in C goes from 0
to n-1
. (or from names[0]
- names[num-1]
) Same in other similar calls.
next issue is the function prototypes to accommodate;
char names[num][15];
The function needs to send the address of an array of arrays. See edits on code to show the difference.
Next issue are the following statements, see comments after each
getNames(names[num], num);//names[n] points to memory beyond what is owned.
//should point to address of beginning of memory &names[0]
printNames(names[num], num);//ditto
Working code example, see edits to explain:
//void getNames(char *a, int n){//change *a to either a[][15] or (*a)
void getNames(char a[][15], int n){
int i;
for(i=0; i<n; i++){
printf("Enter a name: ");
scanf("%s", a[i]);
}
}
//void printNames(char *a, int n){ //change *a to either a[][15] or (*a)
void printNames(char a[][15], int n){
int i;
for(i=0; i<n; i++){
printf("%s\n", a[i]);
}
}
void main(){
int num; //number of names in array 'names'
printf("Num of students: ");
scanf("%d", &num);
char names[num][15]; //each name is 14 characters plus
//a null character
//getNames(names[num], num);//names[n] points to memory beyond what is owned.
//printNames(names[num], num);//ditto
getNames(&names[0], num);
printNames(&names[0], num);
}
you can do following
in main
function call as below
getNames(names, num);
printNames(names, num);
and change the functions as below
void getNames(char (*a)[15], int n)
{
...
}
void printNames(char (*a)[15], int n)
{
...
}
void getNames(char a[5][15], int n)
and
void printNames(char a[5][15], int n)
But instead of using hard values better to use #define
row and col sizes
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.