1
#include <stdio.h>


void getScores(int a, char n[10][15], int s[10]) {
    int score;
    printf("Enter the number of students: ");
    scanf("%d",&a);
    for (int i=0; i < a;i++)
    {
            scanf("%s",n[i]);
            scanf("%d",&score);
            s[i]=score;
    }
}

void printScores(int a, char n[10][15], int s[10] ) {
    for (int i=0; i < a;i++)
    {
            printf("%s", n[a]);
            printf(" ");
            printf("%d\n",s[a]);
    }

}

int main() {
    char names[10][15];
    int scores[10];
    int num;


    getScores(num,names,scores);
    printScores(num,names,scores);
}

What I am trying to accomplish is have the parameter value of int a from the getScores function to be used in the printScores function as an array length as it is being used in getScores.

The arrays are saving its value when used in the print function but the a value is resetting to an unassigned number 896 when I need it to be what the user enters in the get function. Any tips?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Post sample user names. – chux - Reinstate Monica Nov 01 '20 at 14:49
  • Ted 10, Mark 55 – HectorMaleves Nov 01 '20 at 14:50
  • 3
    In main(), neither of those function calls can change the argument 'num':( It remains uninitialized in the printScores() call. – Martin James Nov 01 '20 at 14:50
  • [How do pointer to pointers work in C?](https://stackoverflow.com/q/897366/10147399) – Aykhan Hagverdili Nov 01 '20 at 15:18
  • but why if the names and scores change the num doesnt? – HectorMaleves Nov 01 '20 at 15:53
  • The parameter `a` in your `getScores()` function is local to the function. Changes made in the function do not affect the value or variable named when the function is called. The obvious way to deal with it is to make the function return the number of values: `int getScores(char n[10][15], int s[10]) { int a; …; return a; }`. The alternative is to make the argument into a pointer: `void getScores(int *a, char n[10][15], int s[10])` and then pass `&num` in the `main()` function, and inside `getScores()`, pass `a` (and not `&a`) to `scanf()` and reference `*a` when you need the number. – Jonathan Leffler Nov 03 '20 at 22:24

1 Answers1

0
  1. Print scores will not print anything.
void getScores(int *a, 

And the call

getScores(&num

You need also change accordingly the functions code

0___________
  • 60,014
  • 4
  • 34
  • 74