Ok where do i start?
Sometimes in programming you encounter an array of pointers to a structure and while i can understand why you might do this as it is more efficient to pass pointers to functions rather than the whole array of structures I cannot see where the actual data is stored when using pointers.
Look at the following source codes to see my problem
the first one passes a table(an array of structures) to the functions and the second one passes an array of pointers.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAXCARS 5
#define MAXSTRING 80 //80 being the number of characters for one whole line
//structure for a car data type
typedef struct
{
char make[MAXSTRING];
char colour[MAXSTRING];
int age;
}CAR;
//function prototypes
void ShowMenu(CAR cars[MAXCARS]);
void AddData(CAR cars[MAXCARS]);
void DisplayCars(CAR cars[MAXCARS]);
void DisplayOne(CAR cars[MAXCARS]);
void main(void)
{
CAR cars[MAXCARS];
clrscr();
printf("size of car data structure is %d bytes\n", sizeof(CAR));
printf("size of *cars is %d bytes\n", sizeof(CAR *));
printf("size of array cars is %d bytes\n ", sizeof(cars));
getch();
ShowMenu(cars);
}
void ShowMenu(CAR cars[MAXCARS])
{
int choice;
clrscr();
do
{
printf("Car Program\n");
printf("1. Add Data\n");
printf("2. Display All\n");
printf("3. Display one\n");
printf("4. Quit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: AddData(cars);
break;
case 2: DisplayCars(cars);
break;
case 3: DisplayOne(cars);
break;
case 4: printf("Quitting Program\n");
break;
default: printf("Enter 1 to 3\n");
}
}
while(choice != 4);
}
void AddData(CAR cars[MAXCARS])
{
int i; //looping variable
for(i=0; i<MAXCARS; i++)
{
printf("Enter make ");
scanf("%s", cars[i].make);
printf("Enter colour ");
scanf("%s", cars[i].colour);
printf("Enter age ");
scanf("%d", &cars[i].age);
}
}
void DisplayCars(CAR cars[MAXCARS])
{
int i; //looping variable
for(i=0; i<MAXCARS; i++)
{
printf("Make: %s\n", cars[i].make);
printf("Colour: %s\n", cars[i].colour);
printf("Age: %d\n", cars[i].age);
}
}
void DisplayOne(CAR cars[MAXCARS])
{
char cartofind[MAXSTRING];
int i;
printf("Enter car to find ");
scanf("%s", cartofind);
for(i=0; i<MAXCARS; i++)
{
if(strcmp(cars[i].make, cartofind) == 0)
{
printf("Make: %s\n", cars[i].make);
printf("Colour: %s\n", cars[i].colour);
printf("Age: %d\n", cars[i].age);
}
}
}
#include <stdio.h>
#include <conio.h>
#define MAXCARS 5
#define MAXSTRING 80 //80 being the number of characters for one whole line
//structure for a car data type
typedef struct
{
char make[MAXSTRING];
char colour[MAXSTRING];
int age;
}CAR;
//function prototypes
void ShowMenu(CAR *cars[MAXCARS]);
void AddData(CAR *cars[MAXCARS]);
void DisplayCars(CAR *cars[MAXCARS]);
void DisplayOne(CAR *cars[MAXCARS]);
void main(void)
{
CAR *cars[MAXCARS];
clrscr();
printf("size of car data structure is %d bytes\n", sizeof(CAR));
printf("size of *cars is %d bytes\n", sizeof(*cars));
printf("size of array cars is %d bytes\n ", sizeof(cars));
getch();
ShowMenu(cars);
}
void ShowMenu(CAR *cars[MAXCARS])
{
int choice;
clrscr();
do
{
printf("Car Program\n");
printf("1. Add Data\n");
printf("2. Display All\n");
printf("3. Display one\n");
printf("4. Quit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: AddData(cars);
break;
case 2: DisplayCars(cars);
break;
case 3: DisplayOne(cars);
break;
case 4: printf("Quitting Program\n");
break;
default: printf("Enter 1 to 3\n");
}
}
while(choice != 4);
}
void AddData(CAR *cars[MAXCARS])
{
int i; //looping variable
for(i=0; i<MAXCARS; i++)
{
printf("Enter make ");
scanf("%s", cars[i]->make);
printf("Enter colour ");
scanf("%s", cars[i]->colour);
printf("Enter age ");
scanf("%d", &cars[i]->age);
}
}
void DisplayCars(CAR *cars[MAXCARS])
{
int i; //looping variable
for(i=0; i<MAXCARS; i++)
{
printf("Make: %s\n", cars[i]->make);
printf("Colour: %s\n", cars[i]->colour);
printf("Age: %d\n", cars[i]->age);
}
}
void DisplayOne(CAR *cars[MAXCARS])
{
char cartofind[MAXSTRING];
int i;
printf("Enter car to find ");
scanf("%s", cartofind);
for(i=0; i<MAXCARS; i++)
{
if(strcmp(cars[i]->make, cartofind) == 0)
{
printf("Make: %s\n", cars[i]->make);
printf("Colour: %s\n", cars[i]->colour);
printf("Age: %d\n", cars[i]->age);
}
}
}
as you can see, the first program passes in a table to each function and from this i can see that when you use the AddData()function each piece of data is added to the array but in the second program you pass an array of pointers so that when you have entered the data you require for the structure you only store the address for the start of this structure in the array. but what confuses me is where is all this data held in the first place.
am i making a lot of sense here?
How about them apples?