-1

This code accepts an array of struct Student and then displays it, using a pointer. The code uses not so common approach. The common approach is shown in single-line comments within this code itself.

However, there seems some problem with this approach because the correct value of "percentage" is not displayed. Can anyone tell me what is the cause of this problem?

#include<stdio.h>

struct Student
{
    char grade;
    int rollNumber;
    float percentage;
};

typedef struct Student Student;

void acceptArray(Student*);
void displayArray(Student*);

int main()
{
    Student myFriends[3];
    Student* p = myFriends;

    acceptArray(p);
    displayArray(p);
}

void acceptArray(Student* p)
{
    int i = 0;
    for(i = 0; i < 3; i++)
    {
        printf("\nEnter grade, roll number, and percentage:\n");
        scanf(" %c %d %f",
                (p + i),
                //&(p + i)->grade
                //&p[i].grade
                ( (Student*)((unsigned int)p + sizeof(char)) + i ),
                //&(p + i)->rollNumber
                //&p[i].rollNumber
                ( (Student*)((unsigned int)p + sizeof(char) + sizeof(int)) + i )
                //&(p + i)->percentage
                //&p[i].percentage
                );
     }
}

void displayArray(Student* p)
{
    int i = 0;
    for(i = 0; i < 3; i++)
    {
        printf("\nGrade is      :   %c.", *(p + i));
                //(p + i)->grade
                //p[i].grade
        printf("\nRoll number is:   %d.", *( (Student*)((unsigned int)p + sizeof(char)) + i ));
                //(p + i)->rollNumber
                //p[i].rollNumber
        printf("\nPercentage is : %.1f.", *( (Student*)((unsigned int)p + sizeof(char) + sizeof(int)) + i ));
                //(p + i)->percentage
                //p[i].percentage
     }
}
  • @sg7 - Huh? `p` is initialised to point at the first element of `myFriends`. – Oliver Charlesworth Dec 17 '17 at 19:44
  • @sg7 typedef is used to replace struct Student with Student. – JustStartedCoding Dec 17 '17 at 19:44
  • This is unclear - what is the problem when you use the conventional `&p[i].grade`, etc.? (Can you construct a [minimal test case](https://stackoverflow.com/help/mcve)?) – Oliver Charlesworth Dec 17 '17 at 19:44
  • @OliverCharlesworth This code compiles without any error and runs perfectly except for last printf statement. The correct value is printed if "(p + i)->percentage" or "p[i].percentage" is used instead of "*( (Student*)((unsigned int)p + sizeof(char) + sizeof(int)) + i )". – JustStartedCoding Dec 17 '17 at 19:51
  • 2
    Possible duplicate of [Structure padding and packing](https://stackoverflow.com/questions/4306186/structure-padding-and-packing) – Tom Karzes Dec 17 '17 at 19:54
  • Code compiles, but the way of accessing the data members is wrong. Use the convention recommended by @OliverCharlesworth. – sg7 Dec 17 '17 at 19:59

2 Answers2

0

You are not using the pointers correctly. The best way is to index it by [i] and access struct members via . operator.

That way you will will get a cleaner code. Going inside the structure, the way you do it, is not recommended because of padding holes.

#include<stdio.h>

struct Student
{
    char grade;
    int rollNumber;
    float percentage;
};

typedef struct Student Student;

void acceptArray(Student*);
void displayArray(Student*);

int main()
{
    Student myFriends[3];
    Student* p = myFriends;

    acceptArray(p);
    displayArray(p);
}

void acceptArray(Student* p)
{
    int i = 0;
    char c;
    int d;
    float f;

    for(i = 0; i < 3; i++)
    {
        printf("\nEnter grade, roll number, and percentage:\n");
        scanf(" %c %d %f", &c, &d, &f);

                p[i].grade = c;
                p[i].rollNumber = d;
                p[i].percentage = f;
     }
}

void displayArray(Student* p)
{
    int i = 0;
    for(i = 0; i < 3; i++)
    {

        printf("\nGrade is      :   %c.", p[i].grade );
        printf("\nRoll number is:   %d.", p[i].rollNumber ); 
        printf("\nPercentage is : %.1f.", p[i].percentage );
      }
}

OUTPUT:

Enter grade, roll number, and percentage:                                                                                                                                                                                                         
A 1 66.8                                                                                                                                                                                                                                          

Enter grade, roll number, and percentage:                                                                                                                                                                                                         
B 2 77.8                                                                                                                                                                                                                                          

Enter grade, roll number, and percentage:                                                                                                                                                                                                         
C 3 88.9                                                                                                                                                                                                                                          

Grade is      :   A.                                                                                                                                                                                                                              
Roll number is:   1.                                                                                                                                                                                                                              
Percentage is : 66.8.                                                                                                                                                                                                                             
Grade is      :   B.                                                                                                                                                                                                                              
Roll number is:   2.                                                                                                                                                                                                                              
Percentage is : 77.8.                                                                                                                                                                                                                             
Grade is      :   C.                                                                                                                                                                                                                              
Roll number is:   3.                                                                                                                                                                                                                              
Percentage is : 88.9.                                                                                                                                                                                                                             
sg7
  • 6,108
  • 2
  • 32
  • 40
0

You are trying to pass the individual component address of the struct object to scanf. The way you did it is wrong. It won't be right because (Excerpt from C A reference manual Book).

holes or padding may appear between any two consecutive components or after the last component in the layout of a structure if necessary to allow proper alignment of components in memory. The bit patterns appearing in such holes are unpredictable and may differ from structure to structure or over time within a single structure.

Being said that it is always cleaner, readable and much more easier to read in struct the way we normally do (&p[i].grade..). Keep simple things simple.

user2736738
  • 30,591
  • 5
  • 42
  • 56