Programming :: Multi Dimensional Arrays In C Of Variable Size
Apr 15, 2010
I am curious if there is a way to describe and use variable sized multi-dimensional arrays in C using pointers. I mean, for 1-dimensional array e.g. I can use the following piece of code:
...
int i , N;
int *array;
scanf("%d", &N);
array = (int*) malloc(N*sizeof(int));
for (i=0; i<N; ++i) array[i] = i; // Assigning values to the array
for (i=0; i<N; ++i) printf("%d ", array[i]); // Printing the assigned values
free (array);
...
But what about the two dimensional array[N][M] ? I guess I have to use a double pointer e.g: int **array. But how exactly? Note that I use C90 and not C99.
I just started programming in PHP so I haven't figured out how to do this yet, but I have a multi-dimensional array that I need to sort by one column. That's fine...but I need the sort to ignore case! Right now I have it sorted by 'name' (the other column is 'uid').
The problem is that by the default the sort is case-sensitive so the array looks like this: Code: Apple 4015 Banana 4011 Cherry 4045 avocado 4046
I want to be able to sort the the 'name' column in a case-insensitive manner so that the array actually looks like: Code: Apple 4015 avocado 4046 Banana 4011 Cherry 4045
How to accomplish this? Just FYI I'm not actually sorting the PLUs for fruits...but it was a simple example. I'm actually doing this for a Facebook application.
I do a lot of scientific work - mainly in C/C++ but want to do some of the calcs in python, however I am finding it a problem with the lack of dimensionality. I frequency have a list of n particles and a multi-dim array for each particle and want to be able to access directly any component with ease. How best to go about this...
my prop = [n][x][y][z]
I might need a particular value and then to update that. I do not want to have to deal with lists etc etc as this feels rather cumbersome, adding, removing etc. There must be an easy way.
I need to sort it by array[ $key ][ 2 ] so that it is sorted like so: Code: Array ( [0] => Array ( [0] => Eleven [1] => NumberEleven [2] => 11 ) [1] => Array ( [0] => AnotherEleven [1] => Eleven,Again [2] => 11 ) [2] => Array ( [0] => Twelve [1] => NumberTwelve [2] => 12 )
I've looked at the php array docs the closet thing I found was array_multisort which won't work for my above requirements, does anyone have any insight to how to sort this way?
I am parsing through XML documents with a PHP script and creating an array because, as far as I know, I can manipulate arrays better than the features DOM gives me. That part is not as important, though. The array it produces though is multi-dimensional, but somewhat inconsistent. If I have the following XML:
To call the first bookmark from the first domain, it would be: PHP Code: $array['domains']['domain'][0]['bookmarks']['bookmark'][0] To call the first bookmark from the second domain, it would be: PHP Code: $array['domains']['domain'][1]['bookmarks']['bookmark']
The problem is, I want to simply call bookmarks through a simply 'foreach()' or 'for()' function and the lack of consistency might be a problem, but the fact that some levels have a '0' array key and others do not is causing a problem.
I have never used multidimensional array for a long time so please consider this as a newbie question. I am editing a program that uses a 4 dimensional array and I think I'm losing track of the indexes.
day = 0 to 30 hour = 0 to 23 channel = 0 to 7 len = 0 to 63 unsigned char idx[day][channel][hour][len];
I have seen that the assignment to a string is snprintf(idx[day][hour][channel], 64, "%s/%s", dayofmonth, hourofday); Is this correct? Because when I access with a day argument, that day value goes into the hour part.
I tried to look for this, but there are so little of perl on internet: Code: @array = [$title, $description, $pubDate, $link]; push(@feeds1, $array);
I am trying to create a 2 D array by PUSH, so the output would look like: ([ $title, $description, $pubDate, $link ], [ $title, $description, $pubDate, $link ], [ $title, $description, $pubDate, $link ], [ $title, $description, $pubDate, $link ]...etc )
I'm trying to read content of file to variable and use this variable in for loop. The problem is, when I have c++ comment style in file - /*. Spaces in line are also interpreted as separated lines.
For example:
Code:
Changing $files to "$files" eliminate these problems but causes that whole content of variable is treated as one string (one execution of loop).
my script has a variable which comes in the form +00.00 +0.00 -00.00 or -0.00 (the numbers can be any in that form) for any that have a + symbol I need to remove the +, but if it has a - symbol it needs to stay.
i need to make a new variable with the string from the old variable btut without any plus sign. I have tried a lot of different ways with no success, each thing I tried either left the + or removed the entire string. I think this should work but doesn't
I have a problem on a program,the problem take a very long time when i try to add two big arrays element by elementI know that matlab make adding vectors more faster than adding element by element but I don't know how!!!do any body knew how matlab make vectors operations more faster than element by element???I want to make my calculation more faster because the program is very big
I was reading Kernighan Ritchie book chapter 4 which deals with character pointers.I am not able to understand following different type of declarations
Code: char aname[][15] = { "Illegal month", "Jan", "Feb", "Mar" }; char amessage[] = "now is the time";
need to process files within multiple directories and transfer these to a remote server. What I had in mind was to use arrays for this; code pasted below (explains it better):
Code: #set the arrays array_A=( fileA* pathtodestA hostA passwordA )
I'm making script for automated compiling of one program which would output builds optimized for many architectures. For clearancy, I do each build in it's own catalogue and list of builds with their respective catalogues would be stored as array. I'm using this guide as reference. It describes making array as naming a variable with additional brackets denoting it's position in array.
I have a Perl script that has two arrays - they are related. I would like to print out the contents into two columns next to each other.
#!/usr/bin/perl open(PINGFILE, </home/casper/pingdata.txt") or die " can not open file "; my @totalfile=<PINGFILE>; foreach $string(@totalfile) { if ($string =~ m/(^1sping)(?=.*max))/) { push(usecstring,"$string");
I am working on a project that needs to use structures and I'm pretty sure string arrays. First I declare my structures and they must be exactly like this.
a project using bluetooth to send data byte by byte to external devices buti'm not familiar using arrays to read file from another location before sending the data.If you could,do correct my codes.Here's my code,
I'm trying to figure out how to code for this specific type of instance - I want to use a hash and have the key be a reference to an array, and not use the key in the standard way of it being a scalar. Basically, I have a large output that I need to process line by line, and rather have access to it as an array than a big block in a scalar. For the big block hash as a scalar I would do -
How would I code it that I would have access to the key information as an array and not a scalar? I know it needs to be a pointer and we're going to have -> in there somewhere, but not sure how to approach it. Some of the documentaiton I've been reading about referencing I've found a little confusing so far, and trying to figure out how to use them in context of what I'm working on.
I'm writing a code to get the index of the last occurrence of a given substring in a string.
Code: int StringHandler::lastIndexOf(string src, const string s) { unsigned int lastIndex = string::npos; if (src.find(s, 0) == string::npos) { return -1; } else { bool isLast = false; unsigned int i = 0; do { lastIndex = src.find(s, i); if (lastIndex != string::npos) { i = lastIndex + 1; if (i == src.size()) { isLast = true; } } else { isLast = true; } } while (!isLast); } return lastIndex; }
My problem is: execution steps into an if with a false condition! Check the values of i and src.size() at the right panel. How could be possible for the program to run the highlighted statement? The if condition above is false! [URL]. I could use char arrays, for example, but this kind of false validation has happened to me more than once, I'm using G++ 4.5 with these flags: -O0 -g -Wall -c
In my code printf seems to have a problem with elements that have the same letters but a space inbetween. For instance "new foo", "newfoo" are the same for printf
I've got a situation. I'm having GNU bash version 3.00.16(1) on Solaris 10. I need to declare an array say arr1 which will be populated by an output of a command.
declare -a arr1 arr1=( $(/some/command) )
Supposing it will eventually (after executing the command) have element values as -
arr1[0]=1234 arr1[1]=5678 arr1[2]=7890
Now, I need to declare another set of arrays, one for each of the element values above - e.g.
declare -a arr1_1234 declare -a arr1_5678 declare -a arr1_7890
And I also need to populate elements of each of above 3 arrays with output of another command in a loop. So, these arrays will hold values something like -
I'm able to declare and populate arr1[*]. My question is how do I declare, populate and print the subsequent arrays and their elements?I am feeling rather thick to get this working.
I have a problem with arrays in awk. What i want is to take some data from a file (ssh log) and print it to a html table. I have managed to print some stuff (user logged in and how many times they have logged in) What i want more is to take all the ip that each user logged in from and print it in a row next to the username and times (in the code i typed blabbla where i want the ip to be shown. How do you think i should approch that, multidimensional arrys maybe?