Trouble printf'ing a string out of a structure.

Hi. I would appreciate it if someone of you guys could help me with this one. after building and running the program enclosed it doesn't say the name of the highest earner. All it writes is "The highest earner is ".

<--------------------------------------->

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 2


typedef struct worker_struct
{
char name[];
long id_number;
int age;
unsigned int salary;
}worker;

void* read_data(worker* w)
{
int i;

for(i=0;i<SIZE;i++)
{
printf("Please enter worker number %d's name\n",i+1);
scanf("%s",w[i].name);
printf("Please enter worker number %d's ID number\n",i+1);
scanf("%li",&w[i].id_number);
printf("Please enter worker number %d's age\n",i+1);
scanf("%d",&w[i].age);
printf("Please enter worker number %d's salary\n",i+1);
scanf("%ui",&w[i].salary);

}

return 0;
}

int highest_earner(worker *w)
{
unsigned int highest_salary=0;
int i,n;
for(i=0;i<SIZE;++i)
if((w[i].salary)>(highest_salary))
{
(highest_salary)=((w[i]).salary);
n=i;
}


return n;
}

int main()
{

worker* wrk;
wrk=(worker*)calloc(SIZE,sizeof(worker));
int n;

read_data(wrk);
n=highest_earner(wrk);
printf("%s", wrk[n].name); <---------

printf("The highest earner is %s", wrk[n].name);

return 0;
}

<--------------------------------------->


It works, once you give the name[] a size. It's illegal to declare an empty array, it's an incomplete definition.

This is a C program. There are some benefits to using C++ features where appropriate.

That aside, you allocate wrk in main, but never free it. We call that a memory leak and it's generally considered an error.
It works, once you give the name[] a size. It's illegal to declare an empty array, it's an incomplete definition.

This is a C program. There are some benefits to using C++ features where appropriate.

That aside, you allocate wrk in main, but never free it. We call that a memory leak and it's generally considered an error.


Thank's kbw. I appreciate it :)
Topic archived. No new replies allowed.