# Please help! Beginner stuck on table and array problem!

• 03-10-2005
robsmith
Hi I am new to C programming and I have to produce a C program which holds data for competitors in a competition. The structure must have 5 fields, one to hold the competitor's name, one to hold a competitor number (which you allocate automatically as you read in the data for each competitor, use 1 for the first competitor, 2 for the second and so on), one to hold the river fish weight converted to ounces, one to hold the sea fish weight converted to ounces and a final field to hold the weight of fish caught in the fly fishing competition also converted to ounces.

I must also define an array with 8 elements, each of which is a structure as defined above.

The input should be exactly like this:
Competitor's name: Fred_Bloggs
Weight of Fish from the River Competition (Stones Pounds Ounces): 1 2 9
Weight of Fish from the Sea Competition (Stones Pounds Ounces): 4 10 7
Weight of Fish from the Fly Competition (Stones Pounds Ounces): 0 8 5

and the output should be outputted into a table.

Here is my code so far:

Code:

```#include <stdio.h> #include <stdlib.h> /* Field Definitions */   typedef struct competitor {     char name;     int comp_number;     int river_fish;     int sea_fish;     int fly_fish;   } data        ; main(){   int n, count, x = 0; data *record;   printf("Please enter number of competitors:\n");   scanf("%d", &n);   /* Creates dynamic array*/   record = (data *)malloc(n * sizeof(data));   for(count = 0; count <= (n-1); count++){     printf("Please enter name competitor # %d\n", count+1);     scanf("%s", &record[count].name);     printf("Please enter weight of Fish from the River Competition (Stones Pounds Ounces) for competitor # %d\n", count+1);     scanf("%d", &record[count].river_fish);     printf("Please enter weight of Fish from the Sea Competition (Stones Pounds Ounces) for competitor # %d\n", count+1);     scanf("%d", &record[count].sea_fish);     printf("Please enter weight of Fish from the Fly Competition (Stones Pounds Ounces) for competitor # %d\n", count+1);     scanf("%d", &record[count].fly_fish);   }   for(count = 0; count <= (n-1); count++){ { int i,j ;   const i_limit = 8, j_limit = 5;         do {                 printf("row %d:", i);                 do {                         printf(" %3d", j);                         j = j+1;                         } while (j < j_limit);                                 printf("\n");                                 i++;                         } while  ( i <=i_limit) ; }         printf("Name: %s\n", (&record[count].name));         printf("River Fishing: %d\n", (&record[count].river_fish));         printf("Sea Fishing: %d\n", (&record[count].sea_fish));         printf("Fly Fishing: %d\n", (&record[count].fly_fish));     } }```
• 03-10-2005
k4z1nh0
hi.
man this error is in pointer that you are accessing by means of operator unary "&".
In this part of code, pay attention and write again:
Code:

```for(count = 0; count <= (n-1); count++){     printf("Please enter name competitor # %d\n", count+1);     scanf("%s", record[count]->name);     printf("Please enter weight of Fish from the River Competition (Stones Pounds Ounces) for competitor # %d\n", count+1);     scanf("%d", record[count]->river_fish);     printf("Please enter weight of Fish from the Sea Competition (Stones Pounds Ounces) for competitor # %d\n", count+1);     scanf("%d", record[count]->sea_fish);     printf("Please enter weight of Fish from the Fly Competition (Stones Pounds Ounces) for competitor # %d\n", count+1);     scanf("%d", record[count]->fly_fish);   }```
when work with pointers to structures use every operator "->" that means pointer for one field of struct.

Wait to have help u!
• 03-10-2005
Salem
> typedef struct competitor {
> char name;
If you want a string (as you do), then you need a char array
char name[50];

> record = (data *)malloc(n * sizeof(data));
Don't cast malloc in C.
If you remove the cast and you're still getting errors, then you're probably compiling C++.

> for(count = 0; count <= (n-1); count++)
The common idiom for accessing all the elements of an array is
for(count = 0; count < n; count++)

> when work with pointers to structures use every operator "->" that means pointer for one field of struct.
Actually, the OP had it right, and you've got it wrong.
&foo->bar
is identical to
&foo[0].bar