# Thread: Allocating dynamic memory to a string within a structure

1. ## Allocating dynamic memory to a string within a structure

Hi, I have the below code:

Code:
```struct fact_entry
{
int n;
long long int lli_fact;
char *str_fact;
};

int main (int argc, char *argv[])
{
int n;
int i;
struct fact_entry *fact_table;
n = atoi (argv);
fact_table = (struct fact_entry *)malloc(n*sizeof(fact_table));
for (i=0; i<n; i++) {
if(i==0) {
fact_table[i].n = 0;
fact_table[i].lli_fact = 1;
fact_table[i].str_fact = "1";
}
else {
fact_table[i].n = i;
fact_table[i].lli_fact = i*fact_table[i-1].lli_fact;
/* the next line seems to be wrong*/
fact_table[i].str_fact = (char *)malloc(log10(fact_table[i].lli_fact));
}
}
for (i=0; i<n; i++) {
printf("%d    %lld    %s\n", fact_table[i].n, fact_table[i].lli_fact, fact_table[i].str_fact);
}
return 0;
}```
The program accepts an integer and return 3 columns, the 1st column is the array position, the 2nd column is the factorial and the 3d column is also the factorial but in string format.

The 2 first columns are fine, it's the 3rd column that's failing, so I'm allocating memory the wrong way for the string within the struct. Could someone let me know what I'm doing wrong?

Regards 2. Well you need to create a string of some sort, not just allocate some space.

> fact_table[i].str_fact = "1";
A mixture of assigning string constants and malloc will be a real PITA later on.

Code:
```        if(i==0) {
fact_table[i].n = 0;
fact_table[i].lli_fact = 1;
}
else {
fact_table[i].n = i;
fact_table[i].lli_fact = i*fact_table[i-1].lli_fact;
}
fact_table[i].str_fact = malloc(20);  // max of a long long in decimal is 19 digits
snprintf(fact_table[i].str_fact,20,"%lld",fact_table[i].lli_fact);``` 3. Originally Posted by Salem Well you need to create a string of some sort, not just allocate some space.

> fact_table[i].str_fact = "1";
A mixture of assigning string constants and malloc will be a real PITA later on.

Code:
```        if(i==0) {
fact_table[i].n = 0;
fact_table[i].lli_fact = 1;
}
else {
fact_table[i].n = i;
fact_table[i].lli_fact = i*fact_table[i-1].lli_fact;
}
fact_table[i].str_fact = malloc(20);  // max of a long long in decimal is 19 digits
snprintf(fact_table[i].str_fact,20,"%lld",fact_table[i].lli_fact);```

Thanks Salem. The issue is, I'm already assigning the space with this line in my original code:
Code:
`       fact_table[i].str_fact = (char *)malloc(log10(fact_table[i].lli_fact));`
I've tried your solution as well but I'm still getting the same output, it looks like this:

./factor 19
0 1 1
1 1 P
2 2
3 6 ��
4 24 �
5 120

6 720
7 5040 P
8 40320
9 362880
10 3628800 �
11 39916800 39916800
12 479001600 479001600
13 6227020800 6227020800
14 87178291200 87178291200
15 1307674368000 1307674368000
16 20922789888000 20922789888000
17 355687428096000 355687428096000
18 6402373705728000 6402373705728000

So, the last columns seem to be fine, the problem is the first 10 lines, in which the 3rd column doesn't look fine, I'm still trying to figure out what could be the issue. 4. > fact_table = (struct fact_entry *)malloc(n*sizeof(fact_table));
This is also wrong.

It should be
fact_table = malloc(n*sizeof(*fact_table));
You weren't allocating anywhere near enough memory, leading to corrupt data.

Also, stop casting malloc in your C programs.
See the FAQ. 5. thanks Salem, it works fine now. I've found that nomenclature in many tutorials. Again, thanks for correcting that one. Popular pages Recent additions c progamming, int, long, malloc () function, struct 