1

I'm trying to get to allocate a specific number of memory with malloc, however when I run my program it gives some random letter output, and I can't see whats the matter with it. This is the first time I experiment with malloc, so hope you can understand.

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

#define ACE 1
#define CardSize 52

void count();

int main()
{
    count();
    return 0;

}
void count() {
    int *cards;
    int i;
    cards = (int *)malloc(CardSize*sizeof(int));
    for (i = 0; i < 4; i++) {
        for (i = 1; i < 14; i++) {
            printf("%d\n", cards[i]);
        }

    }
}

Basically it should print out 1-13 in my array of cards[52] the cards, 4 different times. I don't understand why it won't do this, hope someone could help me out.

eddie
  • 1,252
  • 3
  • 15
  • 20
William hart
  • 123
  • 5
  • 2
    C doesn't do magic. Where's your code that initializes that array you've allocated? – Mat Dec 20 '15 at 10:35
  • 1
    You have two nested loops with the same control variable, which is a recipe for disaster. And your indices in the inner loop starts at 1 (it should start at 0) and it iterates once too many (14 instead of 13 passes). – M Oehm Dec 20 '15 at 10:38
  • when calling `malloc()` (and calloc() and realloc()) in C, do not cast the returned value. The returned value is already a `void*` so can be assigned to any other variable. Casting just clutters the code, makes debug and/or maintenance harder. When calling `malloc()`, always check (!=NULL) the returned value to assure the operation was successful – user3629249 Dec 21 '15 at 12:03

3 Answers3

4

Basically it should print out 1-13 in my array of cards[52] the cards, 4 different times. I don't understand why it won't do this

In for loop you have only used one control variable i. And for printing 1-13 you have to assign values , you haven't assign anywhere in code. Try this code:

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

#define ACE 1
#define CardSize 52

void count();

int main()
{
    count();
    return 0;

}
void count() {
    int *cards;
    int i,j;
    cards = malloc(CardSize*sizeof(int));
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 13; j++) {
             cards[i*13+j]=j+1;
            printf("%d ", cards[i*13+j]);
        }
    printf("\n");
    }
free(cards);
}
for (i = 0; i < 4; i++) {
            for (j = 0; j < 14; j++) {
                  cards[i*13+j]=j+1;
                printf("%d ", cards[i*13+j]);
            }

step 1: Here first for loop initialize i=0

step 2: second for loop initialize j=0

step 3: cards[i*13+j]=j+1; will execute.here j=0 and i=0 so cards[0]=1 will be assigned

step 4: printf will print value of cards[i*13+j] here j=0 so value of cards[0] will be printed. again loop will go to step 2 and increment j. upto 1-13 will be printed.

step 5: Again first for loop will be started and again 1-13 will be printed and so on until first for loop get finished.

Note: Do not cast malloc and I suggest use free() after dynamic memory allocation to free memory.

You can also use single loop instead of double loop

for (i = 0; i < CardSize; i++) {
             cards[i]=(i%13)+1;
            printf("%d ", cards[i]);
    }
Community
  • 1
  • 1
Punit Vara
  • 3,744
  • 1
  • 16
  • 30
2

You allocate memory to your pointer , but you never initialize it . And you print it , therefore , doesn't give you intended output .

You can modify loop as follows -

    while(i<52){                      // first initialize i=0
        j=1;                          // set j to 1 in each iteration
        while(j<14){                  // iterate for 14 times
             cards[i]=j;              // initialize chards
             printf("%d", cards[i]);
             j++;
             i++;
        }
       printf("\n");
    }

Output

Note-

1. You should check the return of malloc ,also free the allocated memory.

2. Cast in malloc statement is redundant and unnecessary .

ameyCU
  • 16,489
  • 2
  • 26
  • 41
1

You have two loops, so you need two control variables, and you have to calculate the index for the linearized array.

void count() {
    int *cards;
    cards = (int *)malloc(CardSize*sizeof(int));
    for ( int i = 0; i < 4; i++) { // first loop with control varible i
        for (int j = 0; j < 13; j++) { // second loop with control varible j
            int arrI = i*13+j; // linearized index 
             cards[arrI]= /* what ever */;
            printf("%d\n", cards[arrI]);
        }

    }
    free(cards);
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174