Passing Arrays

I'll confess first of all that this is in C, not C++, but I since they're similar and I couldn't find a C forum I figured this was the next best thing. Sorry if I've broken any rules, I just need help with my program.

I've been working on a program for a computer science course where I need to pass a few arrays into a function. One is a structure array and the other is an integer array. The structure array works fine, while the integer array with the values 0-79 loses the first 44 terms to junk numbers and then begins with the number 0 at index 44. The function prototype is:

void sort_lab(struct data student [], int SIZE, int sorted []);

This function then calls another function, bsort, which is:

void bsort(struct data student [], int SIZE, int sorted []);

bsort is supposed to sort the array sorted based on some of the data inside the student array, but reaches some nice segfaults on account of the values of sorted (sorted is actually supposed to be an array of indices for one of the arrays stored inside student). Worse, the junk values of sorted inside bsort are different than they are in sort_lab, although fortunately its still just all the indices < 44.

I'm pretty new to C programming, so I have no idea what's going on, and the project is due Wednesday. Any help?
You may have written to the elements of sorted due to some fence-post error, or possibly added an offset to the pointer. Post some code.
Ehhh...code please? I can't really tell where you are going wrong without actual code ^^;
Sorry. I've left out irrelevant code from these, but the debugging printf statements I left in just in case they're somehow screwing with the rest:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void sorted_data(struct data student [], int SIZE)
{
system("clear");

char choice;
int sorted[SIZE];
int ii;

for (ii = 0; ii < SIZE; ii++)
{
  sorted[ii] = ii;
  printf("Outside sort_lab, sorted[%d] = %d\n", ii, sorted[ii]); 
}

/* Display menu */
printf("Sort based on the following:\n");
printf("(All sorts go in ascending order)\n");
printf("a: Lab average\n");
printf("b: Program average\n");
printf("c: Midterm average\n");
printf("d: Final\n");
printf("e: Weighted total score\n");
printf("Your choice: ");
scanf("%s", &choice);

switch(choice)
{
  case 'a':
    sort_lab(student, SIZE, sorted);
    break;
...}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void sort_lab(struct data student [], int SIZE, int sorted [])
{
  int ii, jj;
  float avg;
  printf("Starting sort_lab\n");

  for (ii = 0; ii < SIZE; ii++)
  {
    printf("Inside sort_lab, sorted[%d] = %d\n", ii, sorted[ii]);
  }

  for (ii = 0; ii < SIZE; ii++)
  {
    avg = 0;
    for (jj = 0; jj < 12; jj++)
    {
      avg += student[ii].lab[jj];
    }
    avg /= 12;
    student[ii].sort = avg;
  }

  printf("Entering bsort.\n");
  bsort(student, SIZE, sorted);
  printf("Completed bsort.\n");
  printf("Completed sort_lab\n");
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void bsort(struct data student [], int SIZE, int sorted [])
{
int ii, temp, flag = 0;

int a,b;

for (ii = 0; ii < SIZE; ii++)
{
  printf("Inside bsort, sorted[%d] = %d\n", ii, sorted[ii]);
}

while (!flag)
{
  flag = 1;
  for (ii = 0; ii < (SIZE - 1); ii++)
  {
    a = sorted[ii];
    b = sorted[ii+1];
    if (student[a].sort < student[b].sort)
    {
      temp = sorted[ii];
      sorted[ii] = sorted[ii+1];
      sorted[ii+1] = temp;
      flag = 0;
    }
  }
}

} 


Hope that helps.
Here's a possible problem:
scanf("%s", &choice);
If you enter just 1 character, the above line will overwrite 1 byte after &choice. You may be writing on the least significant byte of sorted, sending the start of the array a few bytes forward relative to the pointer.
You should have used this:
scanf("%c", &choice);
Amazingly, that fixed it, although I now have two new problems. First, the switch statement I have in sorted_data no longer works––a case where it's probably just that I don't know enough about C to get it working. (In order to get it working I just had it automatically run sort_lab since that's what I was testing with, but I'll need to fix the switch statement before I turn the program in). Any help with that would be great.

Also, after sort_lab returns the program is supposed to write out to a user-specified file. My code for that is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
FILE *fp;
char fout[40];
int a;

printf("Filename: ");
scanf("%s", fout);
fp = fopen(fout, "w");

for (ii = 0; ii < SIZE; ii++)
{
  a = sorted[ii];
  fprintf(fp, "%s - %d\n", student[a].name, student[a].sort);
}

fclose(fp);

Thanks again for the help!
See what the value of choice is before entering the switch. It might not have been correctly set for some reason.

What's the problem with the other code?
Can't believe I forgot to mention that. It writes out the names of the students followed by another junk integer. An example run gives data like:
1
2
3
4
5
6
7
Sawyer - -1610612736
Zolty - 0
Vedder - 1610612736
Green - -1610612736
Anderson - -1610612736
Silver - 1610612736
Vishnevetsky - 0


And actually, it seems that the code doesn't even run the scanf, despite not being in a comment block. It prints out that it would like me to enter something, then immediately skips past and continues running. So this code:
1
2
3
4
5
printf("Your choice: ");
scanf("%c", &choice);
printf("choice = %c\n", choice);

sort_lab(student, SIZE, sorted);

produces this result:
 
Your choice: choice = 

followed by the code continuing onwards into sort_lab.
Last edited on
After reading your sort routine more carefully, I think it would be easier for you to use an array of pointers to structures.

I would like to see your entire code. There appears to be a whole mess of fence-post errors and related problems with pointers.
If it's too large, you can paste it here: http://pastebin.com/
All of the relevant code is found here: http://pastebin.com/m642fbf15 This truly is an intro course and we covered C for probably no more than two weeks, so I really have no idea how to do this any other way than how the professor instructed, with the array of structures. I did write an equivalent program in MATLAB that works fine and uses pretty much exactly the same sorting program, so while it might look weird, it works for what I need it to do. I also have no idea what a fence-post error is, so I don't know how to fix that :)
Fence-post error: If you need to make a wire fence of 100 m, and you want to put a post every 10 m, how many posts do you need?

If your answer is 10, you just made a fence-post error. The answer is 11.

Fence-post errors in programming refer to the instance of writing/reading past the end, or before the start, of an array.

EDIT: No main()?
Last edited on
There is, but this whole area is embedded deep within a bunch of menus. This is just a small section of the program. The rest of it works fine, so I didn't feel the need to post it (especially because it's all spread across a whole bunch of source files).
Topic archived. No new replies allowed.