1

I am writing a code to take input from the user, and print it out exactly as it was input, through arrays.

void getArrays(char a[10][10], int b[10], int n ){
        printf("Enter number of students and then those students names and scores separated by a space.\n");
        scanf("%d", &n);
        int i = 0;
        while(i < n){
                scanf("%s%d", a[i],&b[i]);
                i++;
        }
}
void printArrays(char a[10][10], int b[10], int n ){
        int j;                                                                                                          
        for(j=0; j<n-1; j++){
               printf("%s %d\n", a[j],b[j] );
        }
}

In this scenario a is a character array that is 10 rows by 10 columns, while b is an array of ints, sized 10 rows as well. n is a number entered by the user before the arrays are created, specifying the amount of strings to be in the arrays. The input would look something like this:

5
Jimmy 90
Tony 80
Troy 67
Dona 78
Dylan 97

At this point it cuts off, and prints the arrays, which worked properly. However, after the names were done printing, the terminal spit a multitude of numbers and random strings before giving me this error:

Segmentation Fault (core dumped)

I ran it through the gbd debugger, and was given the following message:

Program received signal SIGSEGV, Segmentation fault.
__strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:65
65      ../sysdeps/x86_64/multiarch/strlen-avx2.S: No such file or directory.

I've searched this error on the site, but the questions already asked didn't relate to my issue; Weird Segmentation Fault after printing Segmentation fault core dump These are just two of the ones I've seen, and they were not the answer I was looking for.

I saw from further searches that it could be a pointer error, or a size discrepancy in the code when referring to an array. I narrowed the problem down to only occurring after it finished printing out the names. So I tried to change for(j=0; j<n; j++) to for(j=0; j<n-1; j++), and once again it gave me the same issues.

I think perhaps the for loop is reading past the number of elements I want it to, ie- instead of stopping at 3, (if n = 3), and it has nothing else to print so it gives me the segmentation fault error?

Do I need to use a pointer with n in this case, such as &n, or is the issue going to be with the size of my array? The code that calls these functions is as follows:

int main(){

        char names[10][15]; // can handle up to 10 students

        int scores[10];

        int num;

        int average;

        getScores(names, scores, num);

        printScores(names, scores, num);
}
  • 1
    Show your code that calls `getArrays()` and `printArrays()`. Add a print statement for `n` after you've scanned its user input and let me know what value you get. – meaning-matters Nov 03 '20 at 17:54
  • @meaning-matters Now that you mention it, its printing out 832. I entered 3. That could very well be an issue. – NameNotFound Nov 03 '20 at 17:59
  • Didn't you post the same question earlier? – Barmar Nov 03 '20 at 17:59
  • @Barmar I did, and it got closed because it wasn't clear enough I guess, so i deleted it and better explained my issue. – NameNotFound Nov 03 '20 at 18:03
  • You should have edited it and requested reopening instead of writing a new question. – Barmar Nov 03 '20 at 18:08
  • @Barmar My apologies. I didn't know how to request reopening, or that I could. I will certainly keep that in mind for the future, so thank you! I did delete the other question though. – NameNotFound Nov 03 '20 at 18:10
  • Another similar question: https://stackoverflow.com/questions/64633129/c-programming-parameters – Barmar Nov 03 '20 at 18:11

2 Answers2

0

Take into account that the function getArrays is modifying variable n locally (since the function definition is like this:

void getArrays(char a[10][10], int b[10], int n )

and shoudl be like this:

void getArrays(char a[10][10], int b[10], int* n )

So possibly the value of n you are providing to printArrays is not initialized.

On the other hand,in printArrays the loop looks to be wrong defined:

 for(j=0; j<n-1; j++)

should be

 for(j=0; j<n; j++)
0

You need to write:

int num;

getScores(... &num);
printScores(... num);

And:

getScores(... int* n)
{
    scanf("%d", n)
    ...
    while (i < *n) {   // Dereference the pointer `n` to get the value again.

With getScores(... int n) you just pass the current value of num and getScores() only modifies it's local copy of num (which is n); num itself it not changed.

Instead you must pass the "address of" (or "pointer to") num by using int* n.

The garbage you get is because num in main(), which you don't update at all, contains some undefined value (that could come from the processor stack, depending on the implementation of your platform and C compiler).

In your case this garbage/undefined value of num is coincidently higher than 4. This causes your scanned values to be printed plus a lot of again undefined values that are further up in names[] and scores[].

In C it's common to prefix pointers with p, so you may want to write pN instead above.

The above is to get things working. But there's more to say: Currently your arrays have a fixed size. If you'd enter 20 for n and/or enter very long names, your program will behave in undefined ways because your arrays are too small. If you want to stick to fixed sized arrays, you should at least check the value of num and the length of the entered names.

meaning-matters
  • 21,929
  • 10
  • 82
  • 142
  • 1
    Oh! That actually makes a lot of sense. I guess that didn't make sense before you answered that. However, it is now complaining with this, which continues the issua a little. ```HW08.c: In function ‘getScores’: HW08.c:8:10: warning: comparison between pointer and integer 8 | while(i < &c){ | ^``` – NameNotFound Nov 03 '20 at 18:08
  • 1
    @NameNotFound Ah indeed, forgot that. I've added a corrected line above. – meaning-matters Nov 03 '20 at 18:12
  • 2
    So that was 100 percent it! Thank you, as this has been a stress for 2 days now. Also concise explanation. I quite understand it now, at least more than before. If you don't mind me asking, how does one know when to use "*" vs "&" and so on for pointers? – NameNotFound Nov 03 '20 at 18:21
  • 1
    @NameNotFound Glad it's working now. I advise you to read or at least scan the [C FAQ](http://c-faq.com) it's a bit of work, but you'll much better understand how things work and will save time. I think there's still something fishy with your `char a[10][10]` function parameter type, I let that for you to sort out. – meaning-matters Nov 03 '20 at 18:37