structure array pointer function help

This is a discussion on structure array pointer function help within the C Programming forums, part of the General Programming Boards category; I get weird output when I run this program. It seems the printcars function isn't receiving any of the data ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    38

    structure array pointer function help

    I get weird output when I run this program. It seems the printcars function isn't receiving any of the data from the main() function. I have set up a pointer but assume it's not setup properly.
    Code:
     #include <stdio.h>
    #include <string.h>
    
    struct car {
    	char make[16];
    	char model[16];
    	int year;
    	int miles;
    };
    
    void printcars(struct car*);
    
    int main()
    
    {
    
    	struct car myCars[3];
    	struct car *ptrMC;
    
    	ptrMC=&myCars[3];
    
    	strcpy(myCars[0].make, "Toyota");
    	strcpy(myCars[0].model, "Camry V6XL");
    	myCars[0].year=1998;
    	myCars[0].miles=196758;
    
    	strcpy(myCars[1].make, "Honda");
    	strcpy(myCars[1].model, "Sedan");
    	myCars[1].year=2011;
    	myCars[1].miles=20;
    
    	strcpy(myCars[2].make, "Toyota");
    	strcpy(myCars[2].model, "Sienna");
    	myCars[2].year=2006;
    	myCars[2].miles=96000;
    	
    	printcars(ptrMC);
    	
    }
    
    void printcars(struct car *info)
    {
    	int x=0;
    
    	for(x=0;x<3;x++){
    		printf("make:  %s\n", info[x].make);
    		printf("model:  %s\n", info[x].model);
    		printf("year:  %d\n", info[x].year);
    		printf("miles:  %d\n\n", info[x].miles);
    	}
    }

  2. #2
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    Code:
    ptrMC=&myCars[3];
    I think it should be
    Code:
    ptrMC = myCars;
    and
    Code:
    void printcars(struct car *info)
    {
    	int x=0;
    
    	for(x=0;x<3;x++){
    		printf("make:  %s\n", info[x].make);
    		printf("model:  %s\n", info[x].model);
    		printf("year:  %d\n", info[x].year);
    		printf("miles:  %d\n\n", info[x].miles);
    	}
    }
    should be
    Code:
    void printcars(const struct car *info, size_t num_elem)
    {
    	size_t x;
    
    	for(x=0;x<num_elem;x++){
    		printf("make:  %s\n", info[x].make);
    		printf("model:  %s\n", info[x].model);
    		printf("year:  %d\n", info[x].year);
    		printf("miles:  %d\n\n", info[x].miles);
    	}
    }
    Last edited by cph; 03-07-2011 at 08:33 PM.
    Do not stare at my avatar.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    38
    Thanks

    Code:
     ptrMC=&myCars[3];
    I guess I was pointing to the address of an address and the amount of elements is irrelevant to the pointer.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BrandNew View Post
    Thanks

    Code:
     ptrMC=&myCars[3];
    I guess I was pointing to the address of an address and the amount of elements is irrelevant to the pointer.
    Actually you were pointing at 1 position past the end of your array...

    An array[3] has three elements numbered 0, 1 and 2 ... 3 is out of bounds.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    38
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21