i have to convert a program from c++ to C language and at one point in my program im stuck where its dealing with dynamic memory of an array. i started it but i dont know how u do dynamic memomory in c language.

if (reply == '1')
   {
      printf("Enter the 1st score (any invalid score to quit): ");
      scanf("%d", score);
      while (score >= 0 && score <= 100)
      {
         ++numOfScores;
         if (numOfScores > arraySize)
         {
            arraySize = int(1.5*arraySize + 1);
            scoresArrayPtrHold = scoresArrayPtr;
            scoresArrayPtr = new int [arraySize];
            for (int i = 0; i < numOfScores - 1; ++i)
               *(scoresArrayPtr + i) = *(scoresArrayPtrHold + i);
            free(scoresArrayPtrHold);
            scoresArrayPtrHold = 0;
         }
         *(scoresArrayPtr + numOfScores - 1) = score;

         printf("Enter the next score (any invalid score to end): ");
         scanf("%d", score);
      }

Dani AI

Generated

A few focused tips to convert that C++ snippet into clean C (and to avoid the common pitfalls seen in the thread).

  • As recommended, use the C allocation family (malloc / realloc / free). Do not mix C++ new with C free — use matching alloc/free or new/delete consistently. was right to point you to the man page, and 's reminder to free memory is important: always free what you allocate. and gave pointers to tutorials worth reading for background.

  • Use realloc to grow the buffer instead of allocating, copying, and freeing manually. Check scanf return values and pass the address (&score). Watch the off-by-one: check count >= capacity before inserting.

Example C pattern (safe, minimal checks):

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *scores = NULL;
    size_t capacity = 0, count = 0;
    int score;

    printf("Enter the 1st score (any invalid score to quit): ");
    while (scanf("%d", &score) == 1 && score >= 0 && score <= 100) {
        if (count >= capacity) {
            size_t new_capacity = capacity ? (capacity * 3) / 2 + 1 : 4;
            int *tmp = realloc(scores, new_capacity * sizeof *scores);
            if (!tmp) { free(scores); fprintf(stderr, "Out of memory\n"); return 1; }
            scores = tmp;
            capacity = new_capacity;
        }
        scores[count++] = score;
        printf("Enter the next score (any invalid score to end): ");
    }

    /* use scores[0..count-1] */

    free(scores);
    return 0;
}

Practical notes: always test allocation results, prefer size_t for sizes, avoid arithmetic that might overflow on very large arrays, and validate user input (check scanf return). Using realloc with a temporary pointer prevents leaks on failure. This approach should map cleanly from the C++ logic while following C conventions.

Recommended Answers

All 7 Replies

search for the malloc, calloc, realloc functions.

you'll find good tutorials on this site itself

whats that

how do i get to the tutoriols

how do i get to the tutoriols

man is your friend...

(*hint man malloc)

And don't forget free() , which I'm sure is mentioned in the tutorial.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.