pointer to array of structs

B

brownqued

I have the following code.

#define NUMBER 100

#include <stdlib.h>

struct sData
{ char test[NUMBER];
int length;
};

int main(int argc, char *argv[])
{ int a, b;
int NumberOfRows=4, NumberOfColumns=2;
char ctest[]="test";
struct sData **StructArray=NULL;
int len=strlen(ctest);

printf("%d %d\n", sizeof(*StructArray), sizeof(**StructArray));

StructArray=malloc(sizeof(*StructArray) * NumberOfRows);
if (StructArray==NULL) return 1;
for (a=0; a<NumberOfRows; a++)
{ StructArray[a]=malloc(sizeof(**StructArray) * NumberOfColumns);
if (StructArray[a]==NULL) return 1;
}

for (a=0; a<NumberOfRows; a++)
{ for (b=0; b<NumberOfColumns; b++)
{ memset(StructArray[a].test, '\0', NUMBER);
strncpy(StructArray[a].test, ctest, len);
StructArray[a].length=len;
}
}

for (a=0; a<NumberOfRows; a++)
{ for (b=0; b<NumberOfColumns; b++)
{ printf("{%d,%d} %s %d\n", a, b, StructArray[a].test,
StructArray[a].length);
}
}

return 0;
}

Output:
4 104
{0,0} test 4
{0,1} test 4
{1,0} test 4
{1,1} test 4
{2,0} test 4
{2,1} test 4
{3,0} test 4
{3,1} test 4

Is the code above correct?

Is there an alternative way of allocating the required space for the
array? This has a lot of malloc calls, and I wonder if it can be done
more efficiently.

Thanks

Qued
 
B

Ben Bacarisse

I have the following code.

#define NUMBER 100

#include <stdlib.h>

struct sData
{ char test[NUMBER];
int length;
};

int main(int argc, char *argv[])
{ int a, b;
int NumberOfRows=4, NumberOfColumns=2;
char ctest[]="test";
struct sData **StructArray=NULL;
int len=strlen(ctest);

I would welcome more spaces. I find this hard to read.
printf("%d %d\n", sizeof(*StructArray), sizeof(**StructArray));

sizeof produces a size_t. You should either use C99's %zu format for
cast the result of sizeof to right type for the format you are using.
StructArray=malloc(sizeof(*StructArray) * NumberOfRows);
if (StructArray==NULL) return 1;

Better to use "return EXIT_FAILURE;"
for (a=0; a<NumberOfRows; a++)
{ StructArray[a]=malloc(sizeof(**StructArray) * NumberOfColumns);
if (StructArray[a]==NULL) return 1;
}

for (a=0; a<NumberOfRows; a++)
{ for (b=0; b<NumberOfColumns; b++)
{ memset(StructArray[a].test, '\0', NUMBER);
strncpy(StructArray[a].test, ctest, len);


Two things. (1) you may not need to set the whole array to zeros. It
depends how you use it. Simply putting a properly terminated string
in there may well be enough. (2) strncpy is almost always the wrong
function. It is usually better just to put all the string bytes
(including the null) into the destination:

memcpy(StructArray[a].test, ctest, len + 1);
StructArray[a].length=len;
}
}

for (a=0; a<NumberOfRows; a++)
{ for (b=0; b<NumberOfColumns; b++)
{ printf("{%d,%d} %s %d\n", a, b, StructArray[a].test,
StructArray[a].length);
}
}


Is there an alternative way of allocating the required space for the
array? This has a lot of malloc calls, and I wonder if it can be done
more efficiently.

There is a FAQ about this: http://c-faq.com/aryptr/dynmuldimary.html
It even has pictures which are very useful in these situations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top