non-lvalue in assignment ERROR

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kreda
    New Member
    • Jun 2010
    • 1

    non-lvalue in assignment ERROR

    I'm trying to write a program for sorting students by their names with structures and pointers, but I get messages in lines 34 and 35
    "non-lvalue in assignment".Here is the code and image.
    And I must say,I'm very new in programming.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #define LOOP for(i=0;i<n;i++)
    #include<string.h>
    
    struct date {int day,mo,year;};
    struct fax {char name[20]; int index; date birth;};
    
    main()
    {
       int i,n;
    
        printf("How many students ");
        scanf("%d",&n);
        
          fax *pStudent;
          pStudent=(fax*)malloc(n*sizeof(fax));
          LOOP
          {
          printf("Name:"); scanf("%s",(pStudent+i)->name);
          printf("Index:"); scanf("%d",&(pStudent+i)->index);
          printf("Date of birth:"); scanf("%d %d %d",&((pStudent+i)->birth.day),&((pStudent+i)->birth.mo),&((pStudent+i)->birth.year));
          }
          
          
          void *pom;
          
        LOOP /* in this loop i'm sorting students by their names*/
             for(int j=i+1;j<n;j++)
                      if(  strcmp((pStudent+i)->name,(pStudent+j)->name)>0  )
          {
           pom=pStudent+i;
          [B](pStudent+i)=(pStudent+j);[/B] 
           [B](pStudent+j)=pom;[/B]
          }
         
         puts("Students sorted by name");
         
         LOOP
         printf("%s %d",(pStudent+i)->name,(pStudent+i)->index);
        
          system("pause");
          }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    An lvalueis (crudely) a value or expression that can appear on the left hand side of an assignment.

    So what is (pStudent+i)? Well lets start with what type is (pStudent+i). Well pStudent is a fax* so the type of (pStudent+i) is also fax* since (pStudent+i) is just pointer arithmetic resulting a pointer to the fax i after pStudent.

    If (pStudent+i) is fax* then (pStudent+i)=(p Student+j) must be pointer assignment, but hang on (pStudent+i) is not a pointer with a memory location it is a calculated value with no memory location for you to assign a value to. It is an rvalue not an lvalue hence the compiler error.

    At line 18 you allocate an array of fax objects when you do you sort you there need to work on fax objects when swapping values in the array, not fax* pointers.

    Comment

    Working...