Greetings.

I need help making a dynamic array with 3 columns, the problem is that i don't know at run-time how many rows there must be allocated.
So i need to allocate one row every time.

I have this as code:

gchar ***servers_array = NULL;
servers_array = realloc(servers_array, (detected_netstat_servers * 3 * sizeof(gchar*)) + (3 * sizeof(gchar *)));

I know it's wrong, so i tried this:

gchar ***servers_array = NULL;
	servers_array = malloc(3 * sizeof(gchar *));

			for(i = 0; i < 3; i++)
			{
				servers_array[i] = malloc(rows * sizeof(int));
			}

Or must i simple make a 1D array of pointers to arrays of 3 columns?

Assuming you can't just allocate the whole thing after getting the rows, it looks like you're on the same track for what I would consider doing. I'd create an array of three dynamic arrays at compile time, then allocate memory accordingly and use the array as if the rows were columns and the columns were rows (ideally with a nice helping of abstraction to protect myself from brain farts):

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

void fill_array ( int **p, int rows, int cols )
{
  int i, j;
  int k = 0;

  for ( i = 0; i < rows; i++ ) {
    for ( j = 0; j < cols; j++ )
      p[j][i] = k++;
  }
}

void print_array ( int **p, int rows, int cols )
{
  int i, j;

  for ( i = 0; i < rows; i++ ) {
    for ( j = 0; j < cols; j++ )
      printf ( "%5d", p[j][i] );
    puts ( "" );
  }
}

int main ( void )
{
  int rows;

  printf ( "Rows: " );
  fflush ( stdout );

  if ( scanf ( "%d", &rows ) == 1 ) {
    int *p[3];
    int i;

    for ( i = 0; i < 3; i++ )
      p[i] = malloc ( rows * sizeof *p[i] );

    fill_array ( p, rows, 3 );
    print_array ( p, rows, 3 );

    for ( i = 0; i < 3; i++ )
      free ( p[i] );
  }

  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.