Thread: pointers to arrays of structures

  1. #1
    Registered User terryrmcgowan's Avatar
    Join Date
    Jun 2003
    Posts
    8

    Smile pointers to arrays of structures

    Hi all,
    my code as shown below is the beginning of a lottery program, 20 players each with a tag number are assigned 3 numbers between 1 and 10. this is just the first portion of the program. First question I've defined a pointer to the first struct in the array (I hope thats what Ive done) and then passed this pointer to the functions getname and showname, in each function to enable me to cycle through the structures in the array Ive used ptr++. What I'd rather do is

    ptr[i]->name or

    (*ptr[i]).name

    my compiler tells this that this is silly nonsense and although I can't find anything to the contrary in FAQ's, Tutorials, books, searchs etc.I would be suprised if its not possible. I can find plenty on stuctures, plenty on arrays of structures, and also pointers to structures but very little on pointers to arrays of structures.

    Thank you in advance for any help, links to info etc.


    PS is it possible to create a for instance 3 element array and copy that directly into another 3 element array within a structure. yes or no will do because Ive not attempted this yet







    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define NUMOFPLAYERS 3
    
            typedef struct{
                           char name[15];
    
                           int tag;           /*Structure definition*/
    
                           int numbers[3];
    
                           }PLAYER;
    
    void getname(PLAYER *ptr);
    
    void showname(PLAYER *ptr);     /* Function Prototypes  */
    
              int main(void)
              {
              	int s,i;
    
              	PLAYER *ptr;   /*Declare pointer to structure*/
    
              	PLAYER bob[NUMOFPLAYERS]; /*Declare array of structures*/
    
              	ptr = bob;     /*Make pointer point to first struct in array*/
    
              	getname(ptr); /*Call function to input names*/
    
              	showname(ptr);
    
              	printf("player 1 = %s\n\n",bob[0].name);  /*These three lines check to*/
    
              	printf("player 2 = %s\n\n",bob[1].name);  /*ensure the functions did  */
    
              	printf("player 3 = %s\n\n",bob[2].name);  /*their job*/
    
              	scanf("%d",&s);
    
              }
    void getname(PLAYER *ptr)
    {
         int i;
    
         for(i=0;i<NUMOFPLAYERS;i++)
       	  {
         		printf("please enter name for player no %d \n\n",i);
    
         		scanf("%s",ptr->name);
    
         		ptr->tag = i+1;
    
         		ptr++;
    
    	  }
    
    }
    
     void showname(PLAYER *ptr)
    {
         int i;
    
         for(i=0;i<NUMOFPLAYERS;i++)
         {
         		printf("the name for player no %d is %s the tag no is %d\n\n",i,ptr->name,ptr->tag);
    
         		ptr++;
    
    
         }
    }

  2. #2
    Registered User terryrmcgowan's Avatar
    Join Date
    Jun 2003
    Posts
    8

    Thumbs up

    Salem,
    Ta very much for your assistance, I had tried that and made it work but mistakenly thought that this was passing the structure itself rather than a pointer to it, and abandoned this method as bad programming. All of which confirms your assumption that I am very confused by these "pointers to structures". Once again Cheers
    Terry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to structures - Beginner question
    By RobJ in forum C Programming
    Replies: 6
    Last Post: 04-10-2006, 05:57 PM
  2. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. using pointers to pass multi-d arrays
    By aaronc in forum C Programming
    Replies: 1
    Last Post: 04-27-2004, 03:07 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM