0

Hi I wrote this sorting algorithm and I'm not sure why I'm getting the following error: "member reference base type 'int' is not a structure or union"

void sort(float avg_dist, cg[]){

    int i,j,t;

    for(i=1; i<=cg[i]-1; i++)
        for(j=1; j<=cg[i]-i; j++)
            if(cg[j-1].avg_dist >= cg[j].avg_dist){
                t = cg[j-1];
                cg[j-1] = cg[j];
                cg[j] = t;

            }

}
5
  • What line provoke that error ? Commented Jul 28, 2015 at 15:03
  • 1
    What type cg is? It's an invalid function declaration (well, valid for the compiler, but you don't mean it). Commented Jul 28, 2015 at 15:05
  • @EugeneSh.: "valid for the compiler ..."? That is non standard at least. Commented Jul 28, 2015 at 15:08
  • @Olaf See blue112 comment. I am assuming the compiler is not complaining about it, otherwise it won't reach the reported line. Commented Jul 28, 2015 at 15:10
  • @EugeneSh.: Fair enough. I just wonder: if there were only C compiler for C99 upward with all relevant warnings enabled and made errors: would there be more or less questions by beginners, or would just the focus shift? Commented Jul 28, 2015 at 15:14

3 Answers 3

2

cg is an int array.

You can't access a "member" of an int, as in

cg[j-1].avg_dist

I'm not sure what you're trying to do. Maybe multiply ?

 cg[j-1] * avg_dist
Sign up to request clarification or add additional context in comments.

4 Comments

I guess the problem is in the argument list declaration. It lacks type.
"A variable declared without an explicit type name is assumed to be of type int." as of C89.
As I said, I believe the OP doesn't mean it.
Plus one for the insight with the multiplication. Which I stole ;=)
0

It's not the problem, but you're (perhaps intentionally) omitting the type specifier in your function

void sort(float avg_dist, cg[]){

C is defaulting to an int array type, which of course, renders cg[j].avg_dist syntactically invalid. (In reality you probably want to multiply by avg_dist, use * rather than the member selection operator .).

1 Comment

I actually intentionally used "." as a member selection operator
0

This should give you a clear Idea:

#include <stdio.h>

void sortArray(int *array, int length){
  int i,j, k, temp;

  for (i = 0 ; i < length-1; i++){
    for (k = 0 ; k < length-i-1; k++){
      if (array[k] > array[k+1]){
        temp = array[k];
        array[k]   = array[k+1];
        array[k+1] = temp;
      }
    }
  }

    printf("The result:\n");

  for ( j = 0 ; j < length ; j++ ){
    printf("%d  ", array[j]);
  }
}

int main(void){
    int array[] = {1,4,2,-1,2,3,4,1,3,-1};
    int length = sizeof array / sizeof array[0];

    sortArray(array, length);
    printf("\n");

    return 0;
}

Output:

The result:
-1  -1  1  1  2  2  3  3  4  4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.