1

in this program I try to sort customer savings descendingly. And I've tried to compile the code. And I'm still not to understand about pointer. There is an error with message "Assignment to expression with array type error". The target output of this program is tend to be the sorted customers(with their names, and account numbers). I've search in the internet about that error. But I still don't get the solution. Can someone help me to solve the error? Thanks.

#include <stdio.h>
#include <stdlib.h>

struct Data
{
  long long int Savings[100], AccNo[100];
  char Name[100];
};

int main ()
{
  struct Data *ptr;
  int n, i, j, swap = 1, x, y;
  long long int max, min, temp;

  printf ("Enter number of customer(s) : ");
  scanf ("%d", &n);

  ptr = (struct Data *) malloc (n * sizeof (struct Data));

  for (i = 0; i < n; i++)
    {
      printf ("Enter customer %d", i + 1);
      printf ("\nName : ");
      getchar ();
      scanf ("%[^\n]s", &(ptr + i)->Name);
      printf ("Savings : ");
      scanf ("%d", &(ptr + i)->Savings);
      printf ("Account Number : ");
      scanf ("%d", &(ptr + i)->AccNo);
      printf ("\n");
    }

  //Sorting bubblesort
  for (x = 0; x < n; x++)
    {
      for (y = 0; y < (n - x - 1); y++)
    {
      if ((ptr + y)->Savings > (ptr + y + 1)->Savings)
        {
          temp = (ptr + y)->Savings;
          (ptr + y)->Savings = (ptr + y + 1)->Savings;
          (ptr + y + 1)->Savings = temp;
        }
    }
    }

  //Print sorted
  printf ("\n Sorted customers are (:\n");
  for (i = 0; i < n; ++i)
    {
      printf ("%s\n", (ptr + i)->Name);
      printf ("%d\n", (ptr + i)->Savings);
    }
  free (ptr);
  return 0;
}
7
  • 2
    Error message came with a line number, inn'it? Commented Nov 28, 2018 at 15:39
  • 1
    And where does this error occur? Commented Nov 28, 2018 at 15:39
  • Also, I dont think, Savings and AccNo need to be arrays. Commented Nov 28, 2018 at 15:40
  • temp = (ptr + y)->Savings; is wrong: temp is long long but the r-side is struct Data *. Commented Nov 28, 2018 at 15:41
  • scanf("%[^\n]s" will attempt to read a literal factual 's'. The 's' does not belong in the conversion string Commented Nov 28, 2018 at 15:41

1 Answer 1

0
  1. You need to compile your code with warnings enabled, the will guide you through debugging step by step. Try to eliminate one, and compile again. In GCC for example, you would use -Wall flag.
  2. The numerical fields of your struct should be just numbers, not arrays. Moreover, having their type as long long int seems a bit too much, but I leave that on you.
  3. Having (ptr + y)->Savings to access the y-th element of an array of structs and the its field names Savings is technically correct, but it's much more cleaner (thus increases readability and maintainability) to write ptr[y].Savings. That is a general rule, applying to the rest of your code.
  4. I believe the above led you to make two syntactical errors when you were reading the numerical data of the customers with scanf(), since you know that an integer in general needs the & operator. If you had used the clean approach from the start, you wouldn't made those, I believe.
  5. For long long int use the %lld format specifier, not just %d.
  6. In Bubblesort, when you find elements that need to be swapped, then swap the whole elements, not just their Savingss. I recommend creating a function to do that.
  7. scanf("%[^\n]s" doesn't make much sense, I would change it to scanf("%99s", where 99 is the maximum size of your string, minus one. Read more in the 2nd paragraph in scanf(“%[^\n]s”,a) question.

Putting everything together, we get:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Data {
    long long int Savings, AccNo; // these should be numbers, not arrays
    char Name[100];
};

void swap(struct Data* a, struct Data* b) {
    struct Data tmp;
    tmp.Savings = a->Savings;
    tmp.AccNo = a->AccNo;
    strcpy(tmp.Name, a->Name);

    a->Savings = b->Savings;
    a->AccNo = b->AccNo;
    strcpy(a->Name, b->Name);

    b->Savings = tmp.Savings;
    b->AccNo = tmp.AccNo;
    strcpy(b->Name, tmp.Name);
}

int main() {
    struct Data *ptr;
    int n, i, x, y;

    printf("Enter number of customer(s) : ");
    scanf("%d", &n);

    ptr = malloc (n * sizeof(struct Data)); // do not cast malloc

    for(i=0; i<n; i++) {
        printf("Enter customer %d", i+1);
        printf("\nName : ");
        getchar();
        scanf("%99s", ptr[i].Name); // ptr is a pointer, but now you want to actually use it as an array, so use '.'
        printf("Savings : ");
        scanf("%lld", &ptr[i].Savings); // needs &, just like you did for 'n'. USe `%lld' for long lont int as the format specifier.
        printf("Account Number : ");
        scanf("%lld", &ptr[i].AccNo);   // needs &, just like you did for 'n'. USe `%lld' for long lont int as the format specifier.
        printf("\n");
    }

    //Sorting bubblesort
    for (x = 0; x < n; x++)
    {
        for (y = 0; y < n - x - 1; y++) // you don't need paranetheses in the stop condition
        {
            if (ptr[y].Savings > ptr[y + 1].Savings)
            {
                swap(&ptr[y], &ptr[y + 1]); // swap the whole element, not just its savings
            }
        }
    }

    //Print sorted
    printf("\nSorted customers are:\n");
    for(i=0; i<n; ++i)
    {
        printf("%s\n", (ptr+i)->Name);
        printf("%lld\n", (ptr+i)->Savings);
    }
    free(ptr);
    return 0;
}

Output (with relevant input):

Sorted customers are:
George
1
Babis
3
Theodor
20

PS: Do I cast the result of malloc? No!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! I really appreciate it.
@SALOMO welcome to Stackoverflow! You posted a question with an MCVE, which is a very good thing! Next time, indent your code, too, +1! ;)

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.