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

typedef int * pinax;

int main()
{
  pinax *A;
  int size,i,j;
  int *cols;
  printf ("How many sets of numbers: ");
  scanf ("%d",&size);
  cols = (int*) malloc (size*sizeof(int));
  A = (pinax *) malloc (size * sizeof(pinax));

  //Above part i cant understand

  for (i=0;i<size;i++)
  {
     printf ("How many numbers in set %d: ",i+1);
     scanf("%d",&cols[i]);
     A[i] = malloc (cols[i]*sizeof(int));
     printf ("Give %d numbers seperated by space or newline: ",cols[i]);
     for (j=0;j<cols[i];j++)
       scanf ("%d",&A[i][j]);
  }
  for (i=0;i<size;i++) // First dimension is not stable
  {
    for (j=0;j<cols[i];j++) // Second dimension is not stable
      printf ("%d ",A[i][j]);
    printf ("\n");
  }
  return 0;
}

This is a come in my uni's notes that basically creates a dynamic 2D array.I have trouble understanding how it works above the comment i came.The 2 last comments are my prof's and i really cant understand why the dimension in the last 2 for loops is stable.Can someone explain me?

DYZ
  • 55,249
  • 10
  • 64
  • 93
Edward
  • 47
  • 1
  • 9
  • the comments of your professor state the dimension in the for-loops are "*not* stable", why you think they *are* stable? – chickity china chinese chicken Jan 26 '17 at 02:04
  • 1
    1) Never ever `typedef` a pointer! 2) There is **no** 2D array in your code. A pointer is not an array, a pointer to pointer is not a 2D array. 3) We are not a tutoring site. If you have fundamental problems with that code (try formatting it properly), ask the author or your tutor. – too honest for this site Jan 26 '17 at 02:11
  • 2
    @Olaf (1) SO disagrees: http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers – DYZ Jan 26 '17 at 03:20
  • 1
    @DYZ: Even on SO there are bad answers, even highly voted ones. You can `typedef` an incomplete `struct` type, but don't need to `typedef` a pointer. Such pointer types defy writing qualifier-correct code (you cannot qualify such a `typedef`ed pointer e.g. as `const` or `restrict`) or pollute namespace (for all qualifiers you'd need up to 16 `typedef`s!). Let alone it obfuscates the code, as most beginner's questions using such nonsense show. – too honest for this site Jan 26 '17 at 05:08

1 Answers1

0

In many compilers, the array size really needs to be predefined before the compilation time. In your case, you really don't know beforehand how many sets of numbers you'll have to deal with (because the number comes from `scanf, which is user input), so there are two ways: to have an array so big that it's guaranteed that the user input will never exceed the size of your array (which sometimes is very hard to impossible), or to dynamically allocate some memory for the array.

In this case, the second way is implemented. The malloc function allocates some memory for the array (hence its name).

Basically, cols = (int*) malloc (size*sizeof(int)); is pretty much the same as saying int cols[size]; with the difference that the second way is not a proper way and won't work in many compilers, because size is a variable.

*Note: there is another difference between dynamically vs. statically allocated memory, and this is where it is allocated. You can read about it yourself if you're interested.

ZenJ
  • 313
  • 3
  • 15
  • Pretty helpful Zen,can you please explain why the double pointer(int **A) ??And where do it points and stuff,thats the part that confused me – Edward Jan 26 '17 at 02:40
  • @Edward: Excuse me, see [this question](http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome) – tonysdg Jan 26 '17 at 02:49
  • @Edward That is because you are emulating a 2-dimentional array there. Imagine writing `int* a = (int*) malloc(...)`. This way you are allocating a memory to store a 1-dimentional data. Now, you take each element a[], and store there a pointer to the another array (well, not array, but another piece of allocated memory). This way you got something like a 2-D array. But you got a double pointer: a pointer to a pointer – ZenJ Jan 28 '17 at 02:03