-2

Without using the following code how can I initialize an entire column of a 2D-array with a particular value, say 1?

int a[row][col];
for(i=0; i<row; i++)
{
   a[i][col]=1;
}
  • `int a[n]= {1}` will only initialize the first element to `1` the rest will be initialized to `0` – rullof Jan 16 '15 at 15:16
  • How about `int a[col][row];` and then in the loop `a[col][i] = 1`. Seems valid to me. You can even use `memset`. Not that internally it doesn't use a loop, though. – dmg Jan 16 '15 at 15:21
  • @rullof yes you are right guys, is there any way to initialize 1d array with any particular value with just one line of code?? – Subhankar Paul Jan 16 '15 at 15:25
  • TDM-GCC compiler, version 4.8.1 @2501 – Subhankar Paul Jan 16 '15 at 15:31
  • then my answer fits totally to your needs I guess as you are using GCC compiler !! @SubhankarPaul – Meninx - メネンックス Jan 16 '15 at 15:36
  • @SubhankarPaul That is a compiler... not the C version. – 2501 Jan 16 '15 at 15:36
  • @dmg but `memset` can be applied only in case of `char` type array as its syntax is `void *memset(void *str, int c, size_t n)` ... – Subhankar Paul Jan 16 '15 at 15:38
  • @SubhankarPaul; `memset` can be used to initialize an `int` array too if it will be initilized with [`0 or -1`](http://stackoverflow.com/q/24207698/2455888). – haccks Jan 16 '15 at 15:40
  • The numerous answers given including mine explain that you can't do what you ask without a loop or using non portable extensions. If you want a one liner remove the line-feeds on your loop and put it all on one ugly line. All the well intentioned answers that suggest memset for column setting are WRONG because memset works on chars, not ints as my answer ecplains so you get the wrong contents in each element the array. – Andrew Hacking Jan 17 '15 at 14:47

4 Answers4

1

As of C99, standardized 15 years ago, you can use explicit indexing:

int a[10][10] = { [0][0] = 1, [1][0] = 1, /* more code here */, [9][0] = 1 };

With GCC you can use even more powerful syntax but of course you lose portability and are no longer programming in standard C.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • I appreciate your ans but I need the solution in C only – Subhankar Paul Jan 16 '15 at 15:19
  • 1
    @SubhankarPaul C99 is not even the most recently standardized version of C, it's 15 years old. C11 is current. – unwind Jan 16 '15 at 15:19
  • 1
    @unwind what about C 2003 and C11? – rullof Jan 16 '15 at 15:22
  • @rullof Gaah, you're right, I tend to forget about C11. :) Thanks, fixed. – unwind Jan 16 '15 at 15:24
  • how do i know which version of C i'm using @unwind ?? – Subhankar Paul Jan 16 '15 at 16:13
  • @SubhankarPaul It depends on your compiler, there should be documentation stating which version of the language the compiler implements. – unwind Jan 16 '15 at 16:14
  • @SubhankarPaul You can only see which version of compiler you are using, but you can't see which C version you use. This depends entirely of the way you write C! Lets say you start writting your code and you then compile it by adding a parameter `-std=90` when you compile. This is how you limit your compiler to only support syntax untill standard C90 but not syntax from newer standard e.g C18. So if your program compiles this way you can state that your C code is C90 compatible. I learned late that most of the compile time parameters only limit the compiler. – 71GA Apr 24 '20 at 18:50
  • For example you want to write an assembly code for ARM *"Cortex-M4"* microprocessors. Fine! Just use parameter `-mcpu=cortex-m4` at compile time and you will **limit** compiler so that it prevents you with an error/warning to write wrong commands! Does your microprocessor have a vector floating point of version *"fpv4-sp-d16"*? Just add a parameter `-mfpu=fpv4-sp-d16` and you will let compiler compile only the instructions compatible with this vector floating point unit. Also if you want to soft floating point format instead of hard use `-mfloat-abi=softfp` or thumb instructions `-mthumb`... – 71GA Apr 24 '20 at 18:57
0

C99 and latter will let you initialize the entire row/column using designators. But you can't assign 1 to entire column without using a loop.

int a[n]= {1}; will initialize first element of array to 1 and rest of elements will be initialized to 0 by default. It is not initializing all elements to 1. These are the areas of C where you need loop.

haccks
  • 104,019
  • 25
  • 176
  • 264
-1

if you want to initialize all the elements of the 2D-array by the same number you can proceed by what gcc provides for a good array initialization shortcut for instance :

int a[5][6]={ [0 ... 4][0 ... 5]=1};

to see the effect we can use the loop

int i,j;
for(i=0; i<5; i++)
{
  for(j=0; j<6; j++)
    printf("%d ",a[i][j]);
  printf("\n");
}

which generate the matrix

1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1

There is another possibility by using memset() but it is only restricted for type char (-128 ... 127 ) or unsigned char (range between 0 ... 255)

If you are SURE that the values of the array will not at any case exceed the limits then you can use it by defining the array like that

 char a[row][column];

and initialize it with the memset function in string.h library

memset ( a,1, sizeof(a));
-1

What you have is fine for a column.

If you want to init the entire array use :

for (i=0; i < sizeof(a)/sizeof(int) ; i++) {
    a[i] = 1;
}

The above initializes array a using a single loop counter. Since a is an array of type int and not char, you cannot use memset()

Andrew Hacking
  • 6,296
  • 31
  • 37