خرید بک لینک

Vote count: 0

For the following program, fill in the blank with the appropriate malloc() statement, and eliminate the memory leak in the program:

int main(void) { char *name = NULL;

// length of the dynamically allocated string int array_size = -1;

// length of the user's name int length = -1;

while (1) { printf("How many letters are in your name? (0 to quit) ");

  // Note: scanf() always wants the address
  // where it should store the input it's reading.
  scanf("%d", &length);

  if (length == 0)
     break;

  if (length + 1 > array_size)
  {
     // Finish this line
     name = malloc(...);
     array_size = length + 1;
  }

  // Check your understanding: Why do we
  // use name instead of &name in this
  // scanf() statement?
  scanf("%s", name);
  printf("You entered: %s", name);

}

retu 0; }

Where it says // Finish line I thought of putting: name = malloc(sizeof(char)*(length+1)); array_size = length + 1;

However, I am not exactly sure. Can someone help?

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 2 خرداد 1395 ساعت: 7:43

صفحه بندی