0

I'm writing a code for my C programming class and stumbled upon a problem. I'm supposed to write a program which will show as an output Pascal's triangle. I'm to use 1d arrays and in each iteration make the array bigger by using realloc. The trouble is that even though the code compiles and runs when I type eg '7' (as the height of the tringle) in the 7th column there will be trash number. I have no idea why it happens. I'm a beginner in dynamic memory allocation, so please by gentle.

Here's my code:

int i,n;
printf("Give the height:\n");
scanf("%d", &n);
int *tab = (int*)malloc(sizeof(int));
int *tab2, liczba=2;
for(i=0;i<n;i++)
{
    tab2=(int *)realloc(tab,i+1);
    tab2[i]=&liczba;
    print(tab2, i+1);
    printf("\n");
}

void print(int *tab, int size)
{
    int i;
    for(i=0;i<size;i++) printf("%d\t", tab[i]);
}
ninigi
  • 143
  • 1
  • 3
  • 14
  • 1
    tab2[i]=&liczba; gives an error. as tab2[i] is of int type so it doesnt compile – Sourav Kanta Jun 26 '15 at 16:05
  • 2
    This code has numerous issues. The sizing for the `realloc` is wrong, the original value of `tab` is repeatedly used even though it has undergone `realloc` resizing in prior iterations, and `tab2[i]=&liczba` looks like a flat-out guess at .. something. I'm shocked that even compiles, and if it does, I'm more shoked that it doesn't spew a phat warning. "...even though the code compiles " - means little in C. You can write code that compiles and runs, yet still invokes undefined behavior, which is exactly what this does. – WhozCraig Jun 26 '15 at 16:07
  • Please note that when you use realloc your memory block may be moved, invalidating the tab pointer so you should not use it again. – Milack27 Jun 26 '15 at 16:18
  • I'm aware of that but I have torubles finding correct solution... Because how can I create n tabs? If I create them in for loop, they will disappear once they leave it. I'm toying with the idea suggested by BLUEPIX but I'm also not certain how to do it... something like: 'code'tab2=(int *)realloc(tab,i+1); tab2[i]=&liczba; print(tab2, i+1); printf("\n"); free(tab2);'/code' ? – ninigi Jun 26 '15 at 16:33
  • and do not cast malloc()/realloc() see [here](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Pynchia Jun 26 '15 at 17:01

1 Answers1

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

void print(int *tab, int size);

int main(void){
    int i, j, n;
    printf("Give the height:\n");
    scanf("%d", &n);
    int *tab = NULL;

    for(i=1;i<=n;++i){
        int *temp = realloc(tab, i * sizeof(*tab));
        if(temp)
            tab = temp;
        else {
            perror("realloc");
            free(tab);
            exit(EXIT_FAILURE);
        }
        tab[i-1] = (i == 1) ? 1 : 0;
        for(j=i-1;j>0;--j){
            tab[j] += tab[j-1];
        }
        print(tab, i);
    }
    free(tab);
    return 0;
}

void print(int *tab, int size){
    int i;
    for(i=0;i<size;i++){
        if(i)
            putchar('\t');
        printf("%d", tab[i]);
    }
    putchar('\n');
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70