0

I have this loop which creates a certain number of array depending on a value input by the user. I want to include the counter of the array at the end of the array name such that it is: array1[], array2[], array3[] and so on, one for each iteration. Would this be possible? We have just started learning C now at university so I don't know much about it yet. I tried the following:

#include <stdio.h>

int main(void)
{
    //Variables
    int i, columns, column_size;

    //Read input
    printf("Input number of columns:\n");
    scanf("%d", &columns)

    //Loop to create arrays
    for (i=1; i<=columns; i=i+step)
    {
        //Read column size
        scanf("%d", &column_size);

        //Create an array of given size for this column
        int column+"i"+[column_size];
    }

    return 0;
}

enter image description here

Marco
  • 7,007
  • 2
  • 19
  • 49
Yiannis
  • 929
  • 2
  • 11
  • 14
  • 3
    No, it's not possible. – Yu Hao Feb 14 '15 at 16:40
  • 1
    Note: there is no array in the source. – wildplasser Feb 14 '15 at 16:40
  • 5
    You could use a two-dimensional array, and then set `column[i][column_size]`. – yellowantphil Feb 14 '15 at 16:41
  • You can create a program that outputs source code, then compile that into a different program. But why??? Once a program is compiled there are no more names present. – pmg Feb 14 '15 at 16:41
  • 1
    `main()` returns `int`. – Iharob Al Asimi Feb 14 '15 at 16:42
  • @wildplasser what do you mean there is no array in the source? – Yiannis Feb 14 '15 at 16:42
  • You haven't declared any – Quest Feb 14 '15 at 16:43
  • 2
    Why are you trying to do that? there is no possible problem I can think of for which the solutions is what appears to be what you ask ro. – Iharob Al Asimi Feb 14 '15 at 16:43
  • @S.Morgenstern yes i think thats what i need, couldn't remember how I had seen this done before in java thaks – Yiannis Feb 14 '15 at 16:43
  • @Yiannis that has to be what you need. – Iharob Al Asimi Feb 14 '15 at 16:44
  • @iharob university exercise, user inputs number of columns, then size of each column and then inputs values in rows and i need to output the sum of each column – Yiannis Feb 14 '15 at 16:45
  • @Yiannis Think about it carefully. If the user can enter an arbitrary amount of rows, why would you want to deal with each *i*th row manually? Use an array and a loop. –  Feb 14 '15 at 16:47
  • I mean: the program source in the question contains no definition or declaration of an array. – wildplasser Feb 14 '15 at 16:47
  • @wildplasser: the definition `int column+"i"+[column_size];` is supposed to be `int column6[column_size];` where the `6` (or whatever) comes from the loop variable. – pmg Feb 14 '15 at 16:48
  • I see. Probably someone who has been exposed to SAS-macros... – wildplasser Feb 14 '15 at 16:51
  • @wildplasser yes the last line is supposed to be the array declaration but it is incorrect – Yiannis Feb 14 '15 at 16:52
  • @Yannis. Names are only needed and must be known at compile time. The *value* of your `i` varable is only known at run time, so it cannot be used to construct variable names. – wildplasser Feb 14 '15 at 16:57
  • @Yiannis: You can't create "dynamically named" variables like this. You can either create a full 2-D array (ask the user for the max size of any column and then create one that is [number of columns][max size]) or use techniques that you might not know about yet like allocating memory for arrays dynamically (using malloc). – Ben Zotto Feb 14 '15 at 16:57
  • @BenZotto the columns have different sizes though, in 2D array all would be the same size.. because the input is basically just a series of numbers entered I was going to use the size of each array to know when to stop inputing values into each array. I have added an image from the exercise – Yiannis Feb 14 '15 at 17:03
  • i think i could also add the first row to the 2D array and when putting the values into the array i can compare the iteration with the first value in that column which will the size so i know if the array is full or not yet! :) thanks @S.Morgenstern for the 2D array idea, helped me get to the solution!! – Yiannis Feb 14 '15 at 17:12

1 Answers1

5

I want to include the counter of the array at the end of the array name such that it is: array1[], array2[], array3[] and so on, one for each iteration

That is not possible. C is a compiled language, meaning that a program (the compiler) creates the program at one point in time and the program eats user-input at a different point in time.

Even the names of the "variables" might vanish after compilation, they are not needed to execute the program.

int a;

All this does is to tell the compiler that you, the programmer need 32bits of space to store something. If you want to store something there later on, you use the name "a":

a = 42;

The compiler calculates the offset to your current location in RAM and stores the "42" at that address. At runtime the used "names" are completely irrelevant, there is no lookup for the right place involved.

This is the difference to an "interpreted language" like Python.

akira
  • 6,050
  • 29
  • 37
  • This is all correct but doesn't really address the heart of the question the OP has, which is how do I build a data structure like a "ragged" 2D array given C's syntax? – Ben Zotto Feb 14 '15 at 16:58
  • @BenZotto: feel free to click the "answer" button and write down your answer. – akira Feb 14 '15 at 17:07
  • And no, OP's question, as written above, is pretty clear: It's the highlighted part at the beginning of my answer. I know what OP actually wants but that's a misconception in OP's head of how things work. That phrase shows that. Changing the question to "how to create a multidimensional array" would not help OP2, which has the same misconception of how a C compiler works. – akira Feb 14 '15 at 17:12