J
jason
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 ?
Help - 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.name);
printf(" catagory : %s\n", entries.cat);
printf(" descript : %s\n", entries.desc);
printf(" weekday : %d\n", entries.wday);
printf(" duration : %d\n", entries.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.name);
printf(" catagory : %s\n", data.cat);
printf(" descript : %s\n", data.desc);
printf(" weekday : %d\n", data.wday);
printf(" duration : %d\n", data.dur);
}
return;
}
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 ?
Help - 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.name);
printf(" catagory : %s\n", entries.cat);
printf(" descript : %s\n", entries.desc);
printf(" weekday : %d\n", entries.wday);
printf(" duration : %d\n", entries.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.name);
printf(" catagory : %s\n", data.cat);
printf(" descript : %s\n", data.desc);
printf(" weekday : %d\n", data.wday);
printf(" duration : %d\n", data.dur);
}
return;
}