Thread: pointers to structures

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    45

    pointers to structures

    Code:
    typedef struct {
    	int nr;
    	char name[21];
    	char afd;
    }
    EMPLOYEE;
    
    
    void print (EMPLOYEE);
    void get(EMPLOYEE*);
    
    int main (void)
    {    
            EMPLOYEE a1= {1111,"Bob The Builder",'B'},a2={0};
    
    	
            get(&a2);
    
    
            print(a1);
            print(a2);
    }
    void print(EMPLOYEE a){                                                      // copy and paste									
    	printf("%d %s %c",a.nr,a.name,a.afd);
    }
    void get(EMPLOYEE *a){                                               //pointer naar structure
    	printf("arbeider(stamnr,name,afd)");
    	scanf("%d %s %c",a.nr,a.name,a.afd);
    }
    in the first function print, i just copy the structure, in second on i try to pass the structures adres,
    then what are the arguments of scanf;
    Last edited by breaka; 08-15-2006 at 04:50 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    scanf("%d %s %c",&a->nr,&a->name,&a->afd);
    You could also try
    Code:
    scanf("%d %s %c",&(*a).nr,&(*a).name,&(*a).afd);
    if you wish.

    Edit: Corrected. Sorry, missed something.
    Last edited by SlyMaelstrom; 08-15-2006 at 05:01 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by SlyMaelstrom
    Code:
    scanf("%d %s %c",a->nr,a->name,a->afd);
    You could also try
    Code:
    scanf("%d %s %c",(*a).nr,(*a).name,(*a).afd);
    if you wish.
    Not exactly better would be
    Code:
    scanf("%d %s %c",&(*a).nr,(*a).name,&(*a).afd);
    Kurt

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, that's correct. Little error on my part. /*mod edit: non pg-13 language removed(kenfitlike)*/
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I get corrected all the time. It makes me feel better to correct others from time to time.
    Kurt

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    this is very helpfull so far, thanx.
    who can add/correct statements that can be usefull with structures

    EMPLOYEE e1; //record
    EMPLOYEE *p; //pointer to record


    p=&e1; //adding adres to pointer

    e1.name //value of name within record

    &e1.name //adres of name

    p->name //value of name within record

    p.name //adres of name
    Last edited by breaka; 08-15-2006 at 06:23 AM.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Did the teacher that assigned you this homework used to teach first grade? I'm suprised they didn't have to walk around the house and look for different shapes. Anyway read the homework policy because that's clearly homework.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    i was looking for some generalisation because the faq dont seem to cover this. and sorry if my newb questions aren't interesting to you. Perhaps a link to tutorials on this matter?
    Last edited by breaka; 08-15-2006 at 06:30 AM.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The cprogramming tutorial on structures should pretty much cover all of this.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by breaka
    p.name //adres of name
    run it through a compiler. it will tell you exactly ( sometimes a litlle cryptic ) what's wrong with it.
    Kurt

  11. #11
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    its pointer arithmetics that im looking for, notting there realy
    http://faq.cprogramming.com/cgi-bin/...&id=1073086407

    thanx for helping me out anyway
    Last edited by breaka; 08-15-2006 at 06:41 AM.

  12. #12
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    whats the difference between:

    &(r.x) and &r.x with r identifier of structure cause they print out different adresses

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > whats the difference between:
    > &(r.x) and &r.x with r identifier of structure cause they print out different adresses
    Really? There is no difference. You will need to post some real code to get to the bottom of it.

    I suspect that you read about Sly's example and started using &(*r).x in scanf() calls. Use r->x instead. It's easier.

  14. #14
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    Code:
    #include<stdio.h>
    
    int main (void){
    
    	typedef struct {
    	  int nr;
    	  char naam[20];
    	}RECORD;
    
    	RECORD r1={1,"jones"},*p;
    
    	p=&r1;
    
    	printf("%d %d",&r1.naam,&(r1.naam));
    
    return 0;
    }

    i'm just tring to sort out all possible indirectives by tring to print them out, &r1.naam prints out other adres than &(r1.naam)
    Last edited by breaka; 08-15-2006 at 12:14 PM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It would help if you used the right conversion specifier. How did this compile? It really shouldn't have.
    Code:
    #include<stdio.h>
    
    int main (void){
    
    	typedef struct {
    	  int nr;
    	  char naam[20];
    	}RECORD;
    
    	RECORD r1={1,"jones"},*p;
    
    	p=&r1;
    
    	printf("%p %p", &r1.naam, &(r1.naam));
    
    return 0;
    }
    You will notice the addresses are the same now. If you wanted to print the structure you should use the right conversion specifiers in printf() and access those members.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding linked lists, structures, and pointers
    By yougene in forum C Programming
    Replies: 5
    Last Post: 07-13-2011, 08:13 PM
  2. Copying pointers in structures instead of the structure data?
    By Sparrowhawk in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 06:04 PM
  3. returning pointers to structures
    By Giant in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2005, 08:40 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Help with pointers and members of structures
    By klawton in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:34 PM