Asslign a value to an LLVM array

Viewed 133

If I create an LLVM array as

%a = alloca [3 x i32]

and want to assign a value to the first element in the array, how do I do that? Do I have to recreate the array with that value in place somehow? I want to use pure LLVM code and not the LLVM API.

1 Answers

First you need to get the pointer to the first element using the getelementptr instruction, which you then use to store your value:

%p = getelementptr [3 x i32], [3 x i32]* %a, i32 0, i32 0
store i32 4, i32* %p

This stores the value 4 in the first element. To store in a different index, you change the last integer to the index you need:

%p = getelementptr [3 x i32], [3 x i32]* %a, i32 0, i32 2
store i32 4, i32* %p

This one stores 4 in index 2.

Related