0

If there is a structure:

struct Student // Student structure to store student's records
{
int rollno; // student rollno
string name; // student name
string address; // address
int pno; // phone number
};

and int main() contains

int main()
{
Student *s;
s= new Student [10];
}

Then how can we assign a struct to a different struct of same types?

void arrange()
{
Student *p= new Student;
//    int temp;
    for (int i=0; i<10; i++)
    {
        for (int j=0; j<10; j++)
        {
            if (i != j)
            {
                if (s[i].rollno > s[j].rollno)
                {
                    p = s[i];
                    s[i] = s[j];
                    s[j] = p;
                }
             }
        }
    }
9
  • You can't use bold format in markup formatted code. Use *p = s[i];and s[j] = *p; to make assignments to/from p. Commented Feb 8, 2014 at 9:15
  • There is no need in writing everything in bold. Commented Feb 8, 2014 at 9:16
  • thanks .... it's the first time i am using ... thats why ... Commented Feb 8, 2014 at 9:18
  • Please provide a SSCCE. Your code has a variable in the scope of main() and you try to use it in arrange(). Commented Feb 8, 2014 at 9:18
  • it doesn't gives an error but void arrange() also doesnt executes :-/ *p = s[i]; s[i] = s[j]; s[j] = *p; Commented Feb 8, 2014 at 9:21

2 Answers 2

1

Than how can we assign a struct to a different struct of same types

Just copy construct it:

int main()
{
    Student s[10]; // array of 10 students
    Student student = s[5]; // copy of 6th element of array
}

You are over-complicating things with all those pointers.

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

Comments

0

If you declare p as a pointer to a student then the struct is accessed by *p. So you should write:

*p = s[i];
s[i] = s[j];
s[j] = *p;

However, I don't think having a pointer is of any use here. Just write

Student p;

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.