Dynamic allocation issue using ANSI C

Hi all,
I'm trying to dynamically allocate a triple-dimension array, but I'm getting a runtime error exactly in the point I make the allocation, but I don't know what did I do wrong.
Here goes my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>   
#include <stdlib.h>   
  
void main()   
{   
    int ***vetor,   
        *nodos;   
    int grafos,   
        num = 0;   
           
    scanf("%i", &grafos);   
       
    if((**vetor = (int*) malloc(sizeof(int*) * grafos)) /* ERROR HERE*/   
    && (nodos = (int*) malloc(sizeof(int)  * grafos)))   
    {         
        while(num <= grafos)   
        {   
            scanf("%i", nodos[num]);   
               
            if(*vetor[num] = (int*) malloc(sizeof(int*) * nodos[num]))   
            {   
                int i;   
                   
                for(i = 0;i<nodos[num];i++)   
                {   
                    if(vetor[num][i] = (int*) malloc(sizeof(int) * nodos[num]));   
                    else   
                    {   
                        printf("Erro na alocacao de memoria.\n");   
                        exit(-1);   
                    }   
                }   
            }   
            else   
            {   
                printf("Erro na alocacao de memoria.\n");   
                exit(-1);   
            }   
        }   
    }   
    else   
    {   
        printf("Erro na alocacao de memoria.\n");   
        exit(-1);   
    }   
}  


I'd appreciate if someone could help me find out what's wrong in it.
Last edited on
I believe the problem is actually on the line above.

There is no such thing as scanf("%i",x). If you want to get an integer value, you want "%d".
Actually, there is a %i specifier. Though if I remember correctly it will look at its input and decide whether it is hex, octal, etc. while %d explicitly specifies decimal/base 10.
Use of the %i format specifier is POSIX, not C.
http://linux.die.net/man/3/scanf
Topic archived. No new replies allowed.