I'm sure others have already asked this, but is it possible to insert an element into the next available index of an array without using a for-loop to find that index first? Almost like a list.add() function but for arrays in C.
no, you will have to loop through the array.
If it's really list functionality you want you could implement a simple linked list instead of using arrays, for example like this: http://www.cprogramming.com/tutorial/c/lesson15.html
Related
If I have an array with elements linked to xassets with names like "card1", "card2", etc up to "card100" is there a quicker way to write these elements in the array format without having to write out all 100?
Also if this has been answered already please let me know or link to it. About 2 days into trying to teach myself to program.
Not exactly sure what you're asking but you may want to look at loops - Swift Loops. You can add items to an array in a for loop from 1 to 100.
Is this what you are looking for ?
let cards = Array(1...100).map({"card\($0)"})
I am learning coffeescript and some of my code uses the very convenient array ranges syntax.
My understanding was that using [a..b] includes indices a and b in the result and that [a...b] excludes index b in the result.
Also, I thought that [a..] would go to the end of the array and that [..b] would go from the beginning of the array.
Now my issue is, I have some code that needs to take the entire array except the last element. I defined it is
parameters[...]
thinking that this would exclude the last element (i didnt specify an endpoint so it should go to the end and ... is exclusive). However in my tests it is looking like
parameters[...] == parameters[..]
so that I end up having to write
parameters[...-1]
which looks pretty ugly to me
am i doing something wrong or is this a bug/intentional part of the language?
As answered in no difference between [..] and [...] for array?, this appears to be an intentional default behaviour. From the docs:
Slices indices have useful defaults. An omitted first index defaults
to zero and an omitted second index defaults to the size of the array.
The question is in the title: does mxDestroyArray() recursively destroy elements of cells and structs? Is is about MATLAB's C matrix library interface.
To explain in more detail through a concrete example, suppose that I create a 1 by 1 cell using mxCreateCellArray(), then create a numeric matrix using mxCreateNumericArray() and set it as the only element of the cell. Now will calling mxDestroyArray() on the cell destroy the numeric array as well, in one go? Or do I need to call it separately for the numerical array, then the cell? I am hoping for the latter, as this is more reasonable for complex manipulations.
The documentation is ambiguous on this point. Also, it's not easy to devise a test that would give a definitive answer to this.
According to the reply I got on MATLAB answers, mxDestroyArray() does free elements of cells and structs recursively. Please see that answer for an example program that confirms this.
I am working with an older and undocumented set of ActionScript (AS2) and I have found that an array, when looping through it the second time, does not give the proper results. It has been a while since I used ActionScript - does the array need to be reset before the second time through another for loop?
For instance PHP has reset() which returns the array's pointer back to the first item in the array.
There's is no such thing as pointers in ActionScript.
You can target each and every item in an Array by simply targeting it with myArray[index], and no pointer needs to be reset to be able to re-read it.
If your two loops produce different results, I would suggest looking into code that could change anything in it between the two loops or in the first one.
Maybe you could post it here ?
Is it possible to create a multidimensional array of gtkwidgets? Specifically something like this:
mywidgetlist[2]["title"];
Or should I be doing this in a different way? How would I do this?
Basically I have a number of "widgets" (Loaded from gtkbuilder) composed of smaller widgets and I want to be able to change certain values, so this array setup seems preferable.
Is there another way of doing this (Other than actually coding a complete widget using signals etc and placing them in a simple array?)
In C, you cannot use a string to index into an array. Or, strictly speaking you can, but it's almost never what you want to do.
For C solution using glib (handy if you already use GTK+), consider a single-dimensional array of GHashTable pointers.