tags:

views:

200

answers:

7

Hi,

I wanted to store 10 in 4th position of array of 5 positions. How to do ?

int main( ) 
{  
    int a[5] = {,,,,4} ;
    return 0; 
}

If i do that i get error. Please help.

Thanks in advance.

A: 

I don't think you can have an "uninitialized" int in C. This looks like it should give a compiler error.

I think you will have to use:

int main( ) 
{  
    int a[5] = {0,0,0,0,4} ;
    return 0; 
}
FrustratedWithFormsDesigner
+2  A: 

I'm assuming that when you say "4th position" you mean array index = 4 (which is actually the fifth position). If it really needs to be done in one line:

int main()
{
    int a[5] = { a[0], a[1], a[2], a[3], 10 };
    return 0;
}

This compiles and runs without warnings or errors with gcc -Wall -O0. Compiling with optimisation enabled, e.g. gcc -Wall -O3, generates warnings, e.g.

foo.c:3: warning: ‘a[0]’ is used uninitialized in this function

but it still compiles and runs without error.

Paul R
Undefined behaviour, probably works on my machine. Nice effort, given the stated constraints :-)
Steve Jessop
Thanks - I felt dirty after writing that. ;-) It compiles without any warnings with `gcc -Wall`, but I wouldn't want to ever see anything like that in real code.
Paul R
it crashes for you answer saying "The variable 'a' being used without being initialized"
mahesh
Why do you care about warnings, if you *want* to have uninitialized values? Turn off the warnings!
UncleBens
@mahesh: I only get warnings when I turn on optimisation (`gcc -Wall -O3`) - it still compiles and runs OK though - it doesn't "crash" - what compiler are you using ?
Paul R
+2  A: 

Just explicitly set the other elements to 0.

int a[5] = {0,0,0,0,10} ;
polygenelubricants
No, its not like that, my interviewer told its wrong
mahesh
He says he wants to "store 10 in 4th position"
Paul R
Yes, but NOT initializing other positions, he wants only 4th position
mahesh
What do you mean by "4th position" ? Index 3 or index 4 ?
Paul R
"my interviewer told its wrong" - then you ask interviewer for a different question. Knowing whatever right answer there is doesn't mean anything in evaluating your actual programming skill.
polygenelubricants
@ Paul, INDEX 4
mahesh
+16  A: 

You can't do it with initialization, but you can leave the data uninitialized and then assign to the one you care about:

int a[5]; a[3] = 10;

I'm not sure why you'd want to do this, but that's a whole separate question...

Edit: I should add that in C99, you can use initialization:

int a[4] = { [3]=10 };

This is called designated initialization.

Jerry Coffin
+1 For clean C99 solution
Tomas
+1  A: 

If you're using C99, you can use a feature called designated initializers to initialize particular array elements. In this case, you would do this:

int a[5] = { [4] = 4 };

which initializes the element at index 4 to 4, and all of the other elements to 0.

GCC also provides this feature as an extension to the C language, but keep in mind this is not valid ISO C90, nor is it valid in C++. It is valid in C99 only.

Adam Rosenfield
@adam, awesome, i am using VS 2005. help in that case i got error for your answer :).
mahesh
Microsoft has been very slow to implement C99 - they certainly hadn't done it in VS 2005 and I suspect probably not even in VS 2008. Maybe in VS 2010 ?
Paul R
@Paul R: I don't think MS has any plans to implement C99, ever. First hit for `microsoft c99` is this: http://connect.microsoft.com/VisualStudio/feedback/details/485416/support-c99
Steve Jessop
@Steve: I guess I shouldn't be surprised - it's just so annoying when you're trying to maintain cross-platform C code and everything has to be "dumbed down" or "uglified up" to make it compile on WIN32.
Paul R
Make it compile in Visual Studio, anyway. MS doesn't really want you to program for Windows in C, but portable C99 apps and libraries can be compiled for Windows on MinGW, or the Intel compiler, or whatever.
Steve Jessop
A: 

Try:

int a[5] = {-1,-1,-1,-1,4};

The other elements are set to -1 to indicate that they are un-initialized

webgenius
+1  A: 

I suppose you can use placement new.

int arr[4]; //uninitialized
new (&arr[3]) int(10); //"initializes"
UncleBens
Awesome, There you are :)
mahesh
This is just more complicated form of int arr[4]; arr[3] = 10;
Yousf
That's C++-only though - the tags are `c` and `c++`. And it's more than one line. And it's not an initializer. How is this a solution ?
Paul R
I guess it depends on the definition of "initialized", but the question doesn't seem to be asking how to do it on *one* line. (Could also be expanded to any type and how to have partly uninitialized arrays of those - like `std::vector` does.) - For C, there's an answer for C99 below.
UncleBens
@UncleBens: see all the comments from @mahesh - he subsequently added a lot of constraints to his original question.
Paul R
@Paul R: I don't see such a constraint. It has to satisfy an interviewer (which I suppose this did???). - It would be a semi-meaningful question for dynamically allocated arrays (see how Allocators in C++ separate allocation from construction), but I don't think in the general case it is possible to have a partially uninitialized automatic array of objects (with constructors).
UncleBens
@UncleBens: some of the comments have gone now (possibly due to deleted answers ?), but he did state earlier that it needed to be done (a) in an initialiser and (b) in one line. The `C` tag is still there.
Paul R
@Paul R, @UncleBens: yes, it was a comment to my answer, which I deleted because Jerry's was better. I gave the `a[4] = 4;` answer, mahesh commented, "no it should be in single line only". Looks as if there never were well-defined constraints.
Steve Jessop
@Steve: thanks, I was beginning to wonder if my memory was playing tricks on me. ;-)
Paul R