Originally posted by dharh
Though im still not sure how:
malloc( MAXLIST * sizeof( struct ipList * ) );
differs from:
malloc( MAXLIST * sizeof( struct ipList ) );
exactly.
The first requests memory for MAXLIST pointers to objects of type struct ipList; the second requests memory for MAXLIST objects of type struct ipList. Maybe the following will help clarify.
Code:
#include <stdio.h>
#define MAXLIST 10
struct ipList
{
int id;
char *name;
char *ip;
};
int main(void)
{
printf("MAXLIST = %d\n", MAXLIST);
printf("sizeof(struct ipList) = %lu\n", (unsigned long)sizeof(struct ipList));
printf("sizeof(struct ipList *) = %lu\n", (unsigned long)sizeof(struct ipList *));
printf("MAXLIST * sizeof(struct ipList) = %lu\n", (unsigned long)(MAXLIST * sizeof(struct ipList)));
printf("MAXLIST * sizeof(struct ipList *) = %lu\n", (unsigned long)(MAXLIST * sizeof(struct ipList *)));
return 0;
}
/* my output
MAXLIST = 10
sizeof(struct ipList) = 12
sizeof(struct ipList *) = 4
MAXLIST * sizeof(struct ipList) = 120
MAXLIST * sizeof(struct ipList *) = 40
*/