0

I have not been able to find examples that initialize two-dimension array at run-time.

This is code in perl; can anyone "translate" this code into C?

  my @grid;     # grid = 2D array
  my $gr=0;     # rows in grid
  my $gc=0;     # cols in grid
  my @ct;
  if( $ARGV[0] eq '5x5' ) {
    $gr=5; $gc=5;    # grid is all zeroes
    @ct=(2,2,2,2,0);
  }
  if( $ARGV[0] eq '9x9' ) {
    $gr=9; $gc=9;    # grid is all zeroes
    @ct=(2,3,4,2,3,5,3,5,3);
  }
  if( $ARGV[0] eq '6x10' ) {
    $gr=6; $gc=10;
    @grid = (
        [0,8,0,0,0,9,3,5,6,7],
        [6,0,0,5,0,7,0,0,1,0],
        [5,0,2,0,4,1,0,0,0,0],
        [0,0,0,0,2,0,0,0,0,1],
        [0,0,0,1,0,0,0,0,0,0],
        [1,5,0,4,2,6,8,0,0,0],
    );
    @ct=(14,41,15,29,26,33,32,27,32,21);
  }
2
  • just use malloc() with pointer. you can then treat it like an array with ptr[1] for example. if you insist of having an real array at runtime it is not possible, at least at C89. This is because arrays in C are stack - allocated, and you need to specify the size at compile time. If it is fine with you just use int foo[5] Commented Aug 29, 2020 at 19:30
  • 2
    It isn't clear what you mean. The C initializer executes at runtime if inside a function — and arguably even at file scope. You can't assign arrays, which limits what you can do with compound literals. Please clarify what you're hoping to achieve. Commented Aug 29, 2020 at 19:32

1 Answer 1

2

“Initialization” is just giving initial values to an object. To do this at run-time, you can do any of the following, among other possibilities:

Initialize in the definition, as shown in the question:

int myPoints[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9} };

Initialize with individual assignments:

myPoints[0][0] = 1;
myPoints[0][1] = 2;
myPoints[0][2] = 3;
myPoints[1][0] = 4;
myPoints[1][1] = 5;
myPoints[1][2] = 6;
myPoints[2][0] = 7;
myPoints[2][1] = 8;
myPoints[2][2] = 9;

Initialize with code that computes values:

for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
    myPoints[i][j] = 3*i + j + 1;

Copy from a compound literal:

memcpy(myPoints, (const int [3][3]) { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9} }, sizeof myPoints);

In the first case (initializing with the definition), if myPoints is a static object, the compiler is likely to store the values in the object file, to be loaded as part of program loading. If myPoints is an automatic object, it is likely to generate instructions to store values for the array. Technically, the C standard permits the compiler to do either of these for any of the examples above. Which is used is a matter of optimization and compiler implementation. So the distinction of initializing at “run-time” is largely irrelevant unless performance issues are important.

Sign up to request clarification or add additional context in comments.

1 Comment

My question has been modified to clarify my request; "myPoints" has been removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.