C :: Expanding Array In One Of Function
Jun 3, 2013
I have the following struct array list...
Code:
typedef struct ArrayList {
char **array;
//number of elements inside list
int elements;
//length of array
int maximum;
}
ArrayList; I've already created a pointer to ArrayList called "mylist" but this list only has a size of int maximum =10....I want to be able to call expand ArrayList in one of my functions incase I need to make my list greater than the size I already allocated for it. expandArrayList(myList, myList->maximum*2+1); is what I would call inside of my function if I needed to expand the list and here is my actual function...
Code:
ArrayList *expandArrayList(ArrayList *myList, int length) {
int i;
char **newArray;
myList->array=malloc(sizeof(char*)*length);
[code]...
I need this code to allocate a new array of length, which would now be 21. Then I need to copy the contents of the lists old array into the new array...Which I am very confused on this part... I created a temporary char **newArray but I don't know if its even taking any of the strings in. I then need to point the old list->array to the newly created array, update all of the values for elements and maximum and then return a pointer to ArrayList... I've tried a bunch of different code and nothing seems to be expanding my original list at all.
View 2 Replies
ADVERTISEMENT
May 28, 2013
Consider the following:
Code:
int *p;
p = new int[5];
for(int i=0;i<5;i++)
*(p+i)=i;
Now suppose we want to add a 6th element (without using vector)
One way is to copy it across to a larger array:
Code:
int *p;
p = new int[5];
for(int i=0;i<5;i++)
*(p+i)=i;
// realloc
int* temp = new int[6];
std::copy(p, p + 5, temp);
delete [] p;
p = temp;
This looks like a very expensive operation and im looking for other ways.
Three questions regarding the above: Not using vector, is this the best way to do this?What about using realloc? How would I use realloc in this situation?Any other ways apart from realloc or vector?
View 1 Replies
View Related
Feb 1, 2013
I'm trying to expand a template + argument parameter list inside a lambda function like this:
template <typename Class, typename ...Args>
static void create(Args ...args) {
// Perform pre-thread creation work.
std::thread([=]()
[Code] ....
But this does not work:
The compiler error is "error: parameter packs not expanded with ‘...’:|"
However, when I do the following:
template <typename Class, typename ...Args>
static void create(Args ...args) {
// Pre-thread work.
auto tthr = [](Args ...ar) -> void {
[Code] ....
It works just fine. That shows that lambda threads are able to take variadic arguments...
So here is my question; what is the correct capture clause for capturing the variadic object correctly?
View 3 Replies
View Related
Aug 20, 2013
I want to use one median function "selectfunction" to choose one of the 2 other functions at random to pass my 2-dim array to the selected function. There is a problem in the median function
#include <iostream>
#define random(x)(rand()%x) // for random number between numbers of 0 and 1
using namespace std;
void proc1 (int iArray[][2]);
void proc2 (int iArray[][2]);
void selectfunction(int iArray[][2]);
int A[4][2] = {{1, 2} , {3, 4} , { 5, 7} , {8, 1} };
[Code]...
View 1 Replies
View Related
Jan 21, 2013
Write a function that takes an array and returns true if all the elements in the array are positive, otherwise, it returns false.
View 6 Replies
View Related
Jul 30, 2013
im trying to read in 1 array and get 2 as outputs from 3 different functions.my read array is easy enough were im getting confused is how to read that array, separate it and take out only the parts i want and place them into a 2nd, then again a 3rd array.i have the following so far:
Code:
#include<stdlib.h>#include<stdio.h>
#include<unistd.h>
#define SIZE 10
}
[code]....
this compiles without a complaint, but when i go to run it no longer responds after taking the 10th element (well 9th if counting from 0).I think i have the if correct for the even odd section, but when i try to populate B or C array with the output of that if statement from A is were i think things are dying...
View 12 Replies
View Related
Nov 1, 2014
I wanted to print the values of a array from a function by passing the array as well as the number of elements to be read. For a single dimensional array, this is how i have written it. It's pretty straight forward. I want to read 5 elements from the 5th element in the array.
Code:
#include<stdio.h>
void display(int array[],int size) {
int i;
[Code]....
With this code I want to print the five elements from the element present in [0][4].
But shows an error that
Code:
D:BennetCodeblocks CLearning CSingleDimentionalArray.c||In function 'main':|
D:BennetCodeblocks CLearning CSingleDimentionalArray.c|18|warning: passing argument 1 of 'display' from incompatible pointer type [enabled by default]|
D:BennetCodeblocks CLearning CSingleDimentionalArray.c|2|note: expected 'int (*)[10]' but argument is of type 'int *'|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
I know when you pass a array as an argument it gets decomposed into a pointer, but with a multi-dimensional array this is not the case. how this works for mult- dimensional array's?
View 3 Replies
View Related
Jun 27, 2014
I'm wondering if it is possible to pass a 2d array to a function where the size of the array is not known at runtime.
I've tried
function ( array[][6] ) ;
But the size of the array has to be constant so it cannot be declared later.
I've tried using a template but you still have to declare the size of the array at runtime. Is this even possible at all?
The only other way I can think of is using a dynamic 2d array but how to create one and manipulate it.
View 2 Replies
View Related
Apr 17, 2014
use 2D array in function and change the array values. I do not know if I can use array by calling from a function. I have 6 row 6 column array, I used it inside a function and for the another function I just need to change 4. row 4. column and I do not want to type array to just change one part. I do not know if there is another way or not
View 7 Replies
View Related
Oct 1, 2013
I want to pass an array to a function and change the array, how can I do that?
Code:
#include<stdio.h>
/*function to change the array*/
void rearng(int *qw) {
int i,j,a;
int sa[8]; //temp array
int c = 0;
int high;
[Code] ....
View 6 Replies
View Related
Oct 19, 2014
my program is just counting the maximum of the array, however, I'd like to do that with the dynamic array (with pointers).
Code:
#include <stdio.h>
#include <stdlib.h>
void ArrayScan(int n, int *h);
void ArrayMaximum(int n, int *h, int x);
int main()
}
[code]....
View 3 Replies
View Related
Nov 12, 2013
I need to use 3 different functions. The first function should read in N amount of integer values from the user.If any values are less than 1, store a value of 0 instead of the input value.
Second should go through the entire array to find the largest value and send the largest value back to the main program. The last is just an output function where I write out all of the array values and the largest value.
Here is the program.
using namespace std;
#define ARRSIZE 20
void getData(int A[]);
void findBig(int A[ARRSIZE], int max)
[Code].....
View 2 Replies
View Related
Sep 30, 2014
#include <iostream>
#include <fstream>
#define SIZE 50
using namespace std;
//Function Prototypes
void getData(ifstream&, int[], int[]);
double calAverage(int [], int []);
[code]....
View 11 Replies
View Related
Nov 29, 2014
How to power value in function too.
View 2 Replies
View Related
Dec 4, 2013
I want to write a function that can accept any arbitrary array of doubles and return the index of the first element that is out of order or -1 if the elements are in order. Why my for loop exists immediately after an element is found to be out of order. What is wrong with my code and why?
Code:
int out_of_order(double stuff[], int size)
{
int i;
//run through entire array
[Code]....
View 2 Replies
View Related
Jul 15, 2013
Code:
Int** d = malloc( ROWS * sizeof(int*));
for (i = 0; i < ROWS; i++)
d[i] = malloc(COLS * sizeof(int));
fx(d);
My question is, in a function declaration, why do I not have to specify the number of columns. How is this different than when I pass a static 2D array to a function, in which I must declare the function parameter with the number of columns.
Code: void fx(int d[][COLS]);
VS.
Code: void fx(int **d);
View 7 Replies
View Related
Jun 1, 2013
I am trying to write a program that averages an array over every 5 values and stores this in a new array.
e.g. array = [1,2,3,4,5,6,7,8]
averaged_array = [ (1+2+3+4+5)/5, (2+3+4+5+6)/5, etc.]
= [3, 4, etc.]
ive been struggling a bit with passing arrays between functions but have most things working. the sum function works fine but the averaging one crashes when it tries to leave the function and return to main. so it prints the first 2 values of output at the end there and then dies. i have all the proper declarations and libraries.
also, how can i properly find the size of an integer array? like ive used sizeof but i dont know how long the output will be so can't calculate the required size to delcare it :
Code: int main()
{
int start_array[25] = {1,2,4,8,15,9,7,4,2,2,1,3,4,8,13,20,15,8,5,3,2,1,1,0,'$'};
int avg1[256];
[Code].....
View 14 Replies
View Related
Jul 31, 2014
I want to pass array to function, to fill array with new values and then to print the array in the main. But I have problem because it prints me just array of zeros. Maybe the concept is wrong, I'm new with passing arrays to function.
function:
Code:
void printSum(int *return_array) {
int return_array[3];
int i;
for(i = 0; i < 3 ; i++){
return_array[i] = 5;
Code:
void printSum(int *return_array);
int main {
int m_return_array[3];
int i,j;
for(i= 0 ; i < 3 ; i++){
m_return_array[i] = 0;
} printfSum(start,m_return_array);
[Code]...
View 1 Replies
View Related
Sep 17, 2013
I need to pass a 2D array to a function. I want to know, where I may have made a mistake. This is the piece of code that I found on the web, and I am using it to allocate my 2D array.
Code:
signed char allocate2D( int** arr2D, int rows, int cols )
{
int i;
arr2D = malloc( rows*sizeof( int* ) );
if( !arr2D )
return -1;
[Code]....
My main function passes the 2D array to the function
Code: signed char read_root_message_file( int **Root_Messages, int *num ) This is how I pass the array in the main function to the above function:
Code:
int main( int argc, char *argv[] )
{
int **Root_Messages;
...
...
Root_Messages = NULL;
read_root_message_file( Root_Messages, &num_root_msg );
[Code]....
View 7 Replies
View Related
Jan 4, 2015
So I have to type couple of numbers, and find first bigger odd and and first smaller even, and print them in function. So I guess, its void function. Now, I gotta question, how to do it? This is my code, don't mind printf-s, its croatian languge, I just want to know how to transfer array to function. I see alot of people doing it with pointers, but we still havent got to them, so I wonder is it even possible to do it without pointers?
Code:
//!!!CODE DOES NOT WORK!!!
//IT COMPILES, BUT AFTER INPUT, ERROR OCCURES
#include <stdio.h>#define UK_BR 2
void parniNeparni(int brojf[UK_BR]);
int main (){
int broj[UK_BR],i=0;
for(i=0;i<UK_BR;i++){
[Code]....
We're on vacations now, and the professor didn't show us how to do it, and its my homework that has to be done until we get back to college.
View 5 Replies
View Related
Nov 15, 2013
I'm trying to define a 7x5 array in main and then use it in a different function that will fill that array with random floats between 0.0 and 1.0 and then use main to print the final, filled array.
Here is what I have so far:
Code:
#include <stdio.h>
#include <stdlib.h>
main () {
float array [7][5];
printf ("%f", array);
[Code] ....
I keep getting the following error message:
testinghw10.c: In function 'fillArray':
testinghw10.c:17: error: subscripted value is neither array nor pointer
I'm not sure what the problem is? Am I calling "array" variable incorrectly or did I initialize it wrong? Or something else entirely?
View 8 Replies
View Related
Apr 6, 2013
Would this be an example of passing an array to a function??
Code:
void wipe (int theArray[] ) {
int index;
printf("
Inside function wipe() sets each array element back to 0.
");
[code]....
View 4 Replies
View Related
Jan 19, 2014
I am following a tutorial and the topic was passing array to function so i tried to do a BMI calculator by myself. I am using code blocks to compile the codes, it is actually working while using compiler's run button. But when I open the exe file, its closing the window after entering the persons' weights and heights. Here is the code
Code:
// Passing array to function example BMI calculator of n person
#include<stdio.h>
#include<math.h>
void assess(float bmi[],int a);
int main(void){
int n,i,j;
}
[code]....
View 4 Replies
View Related
Mar 31, 2013
I need to create a function which will print a list from 100Hz to 1000Hz then 1000Hz to 9000Hz. I have created a function in order to calculate and set up the frequency values from 100Hz to 9000Hz using two for loops as shown below. However I am unsure how to return this to the array at the main.
int main(void) {
double Frequency[18];
system ("PAUSE");
return(0); } double Frequency (void)
{
int count;
[Code]....
View 1 Replies
View Related
Jan 8, 2014
I am learning arrays i want to whats the following effect of cods while passing array to the following function
void fun(array[])
void fun(array[4])
void fun(array[],4)
void fun(array[5],4)
View 3 Replies
View Related
Mar 15, 2013
I'm trying to pass an array from one function to another without resorting to pointers and I have to use Pass by Reference. I can't seem to figure it out. I've been looking at many examples and just don't get it.
the function createAbc makes an array populated with the Alphabet. I need to pass that into my display function so that i can work with the array there. I don't know how to pass it in there... :(
#include "stdafx.h"
#include <iostream>
#include <cctype>
[Code]...
View 9 Replies
View Related