array-initialization

Does array initialization with a string literal cause two memory storage? [duplicate]

早过忘川 提交于 2021-01-28 06:13:17
问题 This question already has answers here : String literals: Where do they go? (8 answers) Closed 7 days ago . int main() { char a[] = "123454321"; } "123454321" is a string literal and a string literal sets aside memory storage. a is defined by the statement which also causes memory storage. That is, this simple statement char a[] = "123454321"; causes two memory storage, one is for a and the other is for "123454321" . Is it right? 回答1: Yes, that's right. Note that the object on the right of

Why can't you use shorthand array initialization of fields in Java constructors?

久未见 提交于 2020-01-01 23:54:09
问题 Take the following example: private int[] list; public Listing() { // Why can't I do this? list = {4, 5, 6, 7, 8}; // I have to do this: int[] contents = {4, 5, 6, 7, 8}; list = contents; } Why can't I use shorthand initialization? The only way I can think of getting around this is making another array and setting list to that array. 回答1: When you define the array on the definition line, it assumes it know what the type will be so the new int[] is redundant. However when you use assignment it

MISRA-C error in struct array initialization

浪尽此生 提交于 2019-12-23 08:02:14
问题 I have the following: typedef struct { uint8_t BlockID; uint32_t Copies; uint16_t Size; }NVMM_ConfigType; const NVMM_ConfigType NvmmCnf_Layout[6] = { { 1, 1, 4}, { 2, 3, 4}, { 5, 5, 16}, { 10, 1, 4}, { 11, 2, 32}, { 13, 1, 100}, }; Which seems fine to me, but, MISRA-C is giving the following error: MISRA C:2012 rule 10.3 violation: [R] The value of an expression shall not be assigned to an object with a narrower essential type or of a different essential type category I've tried to figure out

C# - Can I use an array initializer to build one byte array out of another?

可紊 提交于 2019-12-22 10:59:14
问题 I'd like to use an array initializer to build one byte array out of another byte array as well as some other bytes that form a header/trailer. Basically, I'd like to do something like this: byte[] DecorateByteArray(byte[] payload) { return new byte[] { 0, 1, 2, payload.GetBytes(), 3, 4, 5}; } GetBytes() above is fictional, unfortunately. Is there any nice/elegant way to do this? I solved this by using a BinaryWriter to write everything to a MemoryStream , and then converting this into a byte

F#: Why is Array.createZero so fast?

一笑奈何 提交于 2019-12-18 09:03:07
问题 I have this code: let timer = new System.Diagnostics.Stopwatch() timer.Start() Array.zeroCreate<int> 100000000 timer.Stop() printfn "%ims" timer.ElapsedMilliseconds timer.Reset() timer.Start() Array.create 100000000 0 timer.Stop() printfn "%ims" timer.ElapsedMilliseconds I tested it and had these results: 0ms 200ms How does Array.zeroCreate create array so fast and it's guaranteed that all it's elements have default value? In other languages I know there are no such possibilities (as far as I

Braces around string literal in char array declaration valid? (e.g. char s[] = {“Hello World”})

谁说我不能喝 提交于 2019-12-17 10:56:22
问题 By accident I found that the line char s[] = {"Hello World"}; is properly compiled and seems to be treated the same as char s[] = "Hello World"; . Isn't the first ( {"Hello World"} ) an array containing one element that is an array of char, so the declaration for s should read char *s[] ? In fact if I change it to char *s[] = {"Hello World"}; the compiler accepts it as well, as expected. Searching for an answer, the only place I found which mentioned this is this one but there is no citing of

Why is this array having all remaining values initialized to zero?

只愿长相守 提交于 2019-12-17 10:00:47
问题 Hello I am a beginner in C programming language , recently i started learning arrays , I have studied that by default all values in an int array are garbage . Then why i am getting different values in these two cases. Case-1 int arr[5]; in this case from arr[0] till arr[4] we will have garbage values, but in next case. Case-2 int arr[5] = {1}; in this case arr[0] will have a value 1 and remaining from arr[1] to arr[4] will have value 0. My question is that, When in case-1 each un-initilized

Initializing variable length array [duplicate]

断了今生、忘了曾经 提交于 2019-12-17 02:37:46
问题 This question already has answers here : C compile error: “Variable-sized object may not be initialized” (7 answers) C error “variable-sized object may not be initialized” [duplicate] (3 answers) gcc complains: variable-sized object may not be initialized (3 answers) Closed last year . On initializing a Variable length array compiler gives an error message: [Error] variable-sized object may not be initialized Code snippet: int n; printf("Enter size of magic square: "); scanf("%d",&n); int

Failed to deduce bounds from initializer for multi-dimensional arrays

江枫思渺然 提交于 2019-12-11 00:32:39
问题 This following code does not compile: int main() { int a[][] = { { 0, 1 }, { 2, 3 } }; } The error message produced is error: declaration of 'a' as multidimensional array must have bounds for all dimensions except the first int a[][] = { { 0, 1 }, ^ Is this specified by the standard? If so, why is that? I think deducing bounds here would be very easy. 回答1: Is this specified by the standard? Well, yeah. §8.3.4/3 When several “array of” specifications are adjacent, a multidimensional array type

Unexpected output on initializing array by using both “Element-by-Element” & “Designated” techniques together

懵懂的女人 提交于 2019-12-10 17:52:57
问题 C99 provides a feature to initialize arrays by using both element-by-element & designated method together as: int a[] = {2,1,[3] = 5,[5] = 9,6,[8] = 4}; On running the code: #include <stdio.h> int main() { int a[] = {2,1,[3] = 5,[0] = 9,4,[6] = 25}; for(int i = 0; i < sizeof(a)/sizeof(a[0]); i++) printf("%d ",a[i]); return 0; } ( Note that Element 0 is initialized to 2 and then again initialised by designator [0] to 9 ) I was expecting that element 0 (which is 2 ) will be replaced by 9 (as