0

I have this struct:

typedef struct
{
    char name[3];
    int month_num;
    int day_num;
    int day_size; // amount of days that are field in with tasks.
    char *task[40];
}

month; // each month contains a name, its num in the calendar and days.

and I need to assign memory allocation for it. I was able to assign the memory for the struct itself:

mon = (month*)malloc(12 * sizeof(month));

But I am having trouble to do it the same for the char *task[40].

I tried a lot of possibilities, but non of them worked...

char temptask[40];
mon->task=malloc(strlen(temptask)+1);
11
  • 3
    Standard Warning : Please do not cast the return value of malloc(). Commented Jan 28, 2015 at 8:23
  • 1
    You should malloc() for mon->task[0] and/or other elements not mon->task. Commented Jan 28, 2015 at 8:23
  • 1
    @lili No, not "what type is it inside?" but "what type is it?" Commented Jan 28, 2015 at 8:43
  • 1
    int i; <- now i is an integer. month m; <- now m.task is what? (I'm testing your understanding; this is not part of an answer) Commented Jan 28, 2015 at 8:46
  • 3
    @lil A value in C can't be "a string" and "an array of pointers" at the same time, they are not the same. Commented Jan 28, 2015 at 8:56

3 Answers 3

1
for(i=0;i<40;i++)
{ 
  /* Allocating memory to each pointers and later you write to this location */
  mon->task[i] = malloc(size);/* If you have a initialized string then size = strlen(tempTask) + 1 */
}

What you have is array of pointers and just access them and allocate memory to each of the pointers individually as shown above.

Sign up to request clarification or add additional context in comments.

6 Comments

i have tried that option but for some reason the compailer screms: "="- cannot convert from 'void *' to 'char *'
What compiler are you using that can't convert from void* to char*? See stackoverflow.com/questions/605845/…
@JohnZwinck Sounds like a C++ compiler to me. :(
@unwind You should be right. The most acclaimed answer of yours not to cast malloc() answers the OP's question :)
@lili: If you're using a C++ compiler you need to cast the result of malloc, like (char*)malloc(...).
|
0

char *task[40]; is an array of 40 pointers. You probably want a single array of 40 characters, in which case no need to malloc it separately, or else a single pointer in which case you can malloc(40). You can't call strlen() on an uninitialized C string, that's undefined behavior (buffer overrun).

Comments

0

I think, what you need is

char *task;

and then allocate memory as

mon[j].task=malloc(sizeof(temptask));

Next, as you've allocated memory to mon as

 mon = malloc(12 * sizeof(month));

the access should be made as

 mon[j].task  // j being the index, running from 0 to 11

Otherwise, if you really have a char *task[40];, that means, an array of 40 char *s, then you have to allocate memory for each of them, one by one.

int p = strlen(temptask);
for(i=0;i<40;i++)
{
      mon[j].task[i] = malloc(p+1);
}

7 Comments

What you're proposing is undefined behavior!
@JohnZwinck I really appreciate leaving the comment along with the downvote. Can you please elaborate a bit, as i'm not clear on the reason behind undefined behaviour here.
It's undefined behavior to call strlen(temptask) when temptask is an uninitialized C string. This UB was in the OP as well. There's no need for the temptask variable at all.
why do i need to write mon[i] if month are going to max 12? why is going from 0 to 39?
@JohnZwinck Is it better now?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.