fenigo69 fenigo69 - 1 year ago 92
C Question

Error in array allocation in C

i've try to compile this simple program, it will alloc a dyamic array and return it with a multiple of 5 in every location. but it doesn't work, it report me an error in the pointer.

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

int main(int argc, char const *argv[]) {
int n;
int i;
int* ptra;

return 0;

usr usr
Answer Source

Index range for ptra must be from 0 to n-1 (both inclusive). But here:


you are going out of bounds, which is undefined. Change it to:

for(i = 0; i < n; i++) {

Note: Always check the return of all the standard functions for failures (scanf() and malloc() in your code).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download