Thread: Swapping structure

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    Swapping structure

    I try to swap name in a structure. But it never work. All it gave me is multiple error : request for member 'name' in something not a structure or union. What's this?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    	
    struct record{
        char* name;
    	char* programme;
    	int id;
    	float cgpa;
    };
    
    typedef struct record record;
    
    int main(){
    	
    	record data[1];
    	int i = 0;
    	int j = 1;
    	
    	data[i].name = "Hello World";
        data[j].name = "HAHAHA";
        
        swap(data,i,j);
    }
    
    
    int swap(int data[], int i , int j){
    
        record temp;
    
        temp.name = data[i].name;
        data[i].name = data[j].name;
        data[j].name = temp.name;
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Compile your program with compiler warnings set at a suitably high level. What warnings do you get, and what do you make of them?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Is record a struct?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Because this:

    Code:
    int swap(int data[], int i , int j);
    should be:

    Code:
    int swap(record data[], int i , int j);
    BTW, this is only one struct:

    Code:
        record data[1];
    Not two, so you have gone out of bounds with j.
    Last edited by MK27; 02-29-2012 at 08:20 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    Quote Originally Posted by laserlight View Post
    Compile your program with compiler warnings set at a suitably high level. What warnings do you get, and what do you make of them?
    What do u mean? I used CFree and I got those errorSwapping structure-screenshot141-jpg message.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ncode
    What do u mean? I used CFree and I got those error message.
    That's what I mean, except that next time, copy and paste the error messages.

    Basically, what MK27 is getting at is correct, but you need to understand what the compiler is telling you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    Thank you MK27 and everyone else. SOlved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping 2 numbers
    By Rakesh Kp in forum C Programming
    Replies: 8
    Last Post: 09-12-2011, 12:07 PM
  2. Swapping the indices of a structure
    By porstart in forum C Programming
    Replies: 4
    Last Post: 11-24-2010, 06:18 PM
  3. swapping of two numbers
    By baffleddreams in forum C Programming
    Replies: 5
    Last Post: 08-25-2010, 10:57 AM
  4. swapping help
    By mouse666666 in forum C Programming
    Replies: 3
    Last Post: 03-24-2010, 10:38 AM
  5. swapping
    By metallicaau in forum C Programming
    Replies: 2
    Last Post: 10-08-2002, 08:17 AM