Afficher un message
Vieux 21/10/2007, 18h13   #1
jason
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut sizeof and passing pointer

Hello,

I'm a beginning C programmer and I have a question regarding arrays and
finding the number of entries present within an array.

If I pass an array of structures to a function, then suddenly I can't use
sizeof(array) / sizeof(array[0]) anymore within that function ?

- What point am I missing ?

To show an example below. The commented out code that works where as the
exact same code, only wrapped in a function does not work ?

Thank you...

#include <stdio.h>

typedef struct {
char *name;
char *cat;
char *desc;
int wday;
int dur;
} entry_t;

entry_t entries[] = {
{ "dummy", "todo", "explanation", 1, 20 },
{ "not", "bla", "foobar", 1, 10 },
{ "check", "nowhere", "for something", 1, 20 }
};

void task_dump(entry_t *);

int main(int argc, char *argv[]) {
/*
int i = 0;
int max = 0;

max = sizeof(entries) / sizeof(entries[0]);
for(i = 0; i < max; i++) {
printf(" -- %02d\n", i);
printf(" name : %s\n", entries[i].name);
printf(" catagory : %s\n", entries[i].cat);
printf(" descript : %s\n", entries[i].desc);
printf(" weekday : %d\n", entries[i].wday);
printf(" duration : %d\n", entries[i].dur);
}
*/

task_dump(entries);

return 0;
}

void task_dump(entry_t *data) {
int i = 0;
int max = 0;

/* this doesn't work ? */
max = sizeof(data) / sizeof(data[0]);
for(i = 0; i < max; i++) {
printf(" -- %02d\n", i);
printf(" name : %s\n", data[i].name);
printf(" catagory : %s\n", data[i].cat);
printf(" descript : %s\n", data[i].desc);
printf(" weekday : %d\n", data[i].wday);
printf(" duration : %d\n", data[i].dur);
}

return;
}
  Réponse avec citation
 
Page generated in 0,05635 seconds with 9 queries