C++ :: Function Argument As Array Size?
Feb 22, 2013
I made a function like follows:
void foo(const double va, const int q) {
int qaa[q];
......
return;
}
However, the compiler indicates allocator cannot allocate an array of constant size 0... how can I use the argument "q" to fix the size of array "qaa"?
View 8 Replies
Sep 14, 2013
I have a function
Code:
int exec_program(char * arguments[])
{
...
}
I can call it like this without a problem:
Code: char * uselessvariable[] = {"/bin/echo", "Testing", NULL};exec_program(uselessvariable);
However I get an error if I try to compile it like this:
Code: exec_program({"/bin/echo", "Testing", NULL});
How, in c, I can put this array inside of the argument in one line without having to name a new variable name?
View 2 Replies
View Related
Feb 7, 2013
The code below outputs this:
a[]= 00
a[]= 10
a[]= 10
a[]= 10
a[]= 11
a[]= 11
0.
But I was expecting this:
a[]= 00
a[]= 10
a[]= 10
a[]= 00
a[]= 01
0.
This describes how the process is running in machine:
1. Defining a[2]{0,0}; ii=0; aj=0
2. Calling function func(a,ii,aj) |func({0,0},0,0)|
3. func({0,0},0,0) defining w=0; static aa=0
4. func({0,0},0,0) if(0) returns aa=1
5. func({0,0},0,0) for j=0
6. func({0,0},0,0) for Outputing "00", because a[2]={0,0}, look (1).
7. func({0,0},0,0) for if(!0) | because a[0]=0| returns w+=func(a,ii+1,j) |func({0,0},0+1,0)| and calls func({0,0},1,0)
8. func({0,0},0,0) for if func({0,0},1,0) defining w=0
9. func({0,0},0,0) for if func({1,0},1,0) if(1) returns a[0]=1, because of static aa=1, см 4.
10. func({0,0},0,0) for if func({1,0},1,0) for j=0
11. func({0,0},0,0) for if func({1,0},1,0) for Outputing "10", because of a[2]={1,0}, look row #9
12. func({0,0},0,0) for if func({1,0},1,0) for if(!1) |because a[0]=1|
13. func({0,0},0,0) for if func({1,0},1,0) for j=1
14. func({0,0},0,0) for if func({1,0},1,0) for Outputing "10"
15. func({0,0},0,0) for if func({1,0},1,0) for if(!0) |because a[1]=0|
16. func({0,0},0,0) for if func({1,0},1,0) for if if(1==1) |because ii=1, func({0,0},ii,0)|
17. func({0,0},0,0) for if func({1,0},1,0) for if if return 0
18. func({0,0},0,0) for if w=0 |because func({1,0},1,0) gives 0|
19. func({0,0},0,0) for j=1
And from now, something is happening that I cannot understand:
20. func({0,0},0,0) for Outputing "10"
Why so? If func has itselfs local variables, including a[2]={0,0}.
I was expecting this:
20. func({0,0},0,0) for Outputing "00"
So a[2] array is not local variable. Why it happens?
Code:
#include <iostream>
using namespace std;
int func(bool a[],int ii,int aj) {
int w=0;
static bool aa=0;
[Code] ....
View 3 Replies
View Related
Feb 12, 2014
Write a function that accepts an array of integers and its size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth.
The function should return a pointer to the new array. Use ONLY pointer parameters instead of arrays in both functions; use pointers (not subscripts) to move through elements of both arrays. Before calling the function, display your original array. When the function call is completed, display the new array.
Here is what i got so far:
#include <iostream>
using namespace std;
int *shifted (int * , int);
const int SIZE = 10;
int main () {
int array_size [30];
[Code] ....
View 1 Replies
View Related
Jun 5, 2014
I'm working on a piece of code written long time ago. Without getting in the details or too much context here, there is a function that declares an array of char of a size of 350,000 elements, in order to fill it (using a pointer) with the list of all running processes on the machine (using "ps -ejf" on a Linux box).
The size of the char array has been changed from 40,000 to 350,000 sometime along the years, probably because of a lack of space required.
What kind on data structure / storage would you use to store the running processes in order to eventually search for a value in it?
View 2 Replies
View Related
Dec 26, 2014
I am writing a program in which a Fucntion has to be wriiten to parse the Command Line . When I include Code for parsing in main fuction iteslf ,its run ok . But I want to make a fucntion of that code and call it from main ,than it show Segmentation error .
By using Debugging I found Some thing is mess with " -m" Parameter of Command line , But Cant Rectify it ..
Code:
int main (int argc, char *argv[]){
//get_parameter_value(argc,argv);
// buffer[packet_size+1]= char ("'");
while (argc > 1) {
if (argv[h][0] == '-')
[Code] .....
View 3 Replies
View Related
May 21, 2013
How to pass an int that I got from user input into a function to use it. I am trying to print out the words to a string of numbers.
I got the input from user.
I got an absolute value of the input.
I then separate the string into individual digits and name them.
I can print these out.
Then I started my if statement by checking if the original input was zero, and if it is, printing zero and exiting.
Then I an trying to pass the digits into a switch function and this is where I go off the rails.
Code:
#include <iostream>
#include <string>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
[Code] .....
View 7 Replies
View Related
Mar 24, 2014
I was reading about void as function argument, but I did not fully understand it's meaning in C.
In C++
void foo(void) {}
and
void foo() {}
are the same. It means no arguments for foo function. But in C it's different. First function means the same as in C++, but second means
In C, an empty parameter list means that the number and type of the function arguments are unknown. But if it is unknown you can't use this arguments if user specifies same. Because here are no variables to store them. So doesn't result are the some? You do not get any arguments. O do I can get this arguments from some hidden variable?
For example.
void foo() {
printf("%d", var);
}
foo(5);
It is very unclear for me. Do this apply to main function too?
int main(void)
int main()
or can I use arguments given to int main() like given to int main(int argc, char* argv[])
View 4 Replies
View Related
Jun 12, 2013
I was wondering if one could write a function that could accept one or the other variable type.
Ex: I have 2 arrays, int** and double**, and a function
Code: void PGMWrite(double** Matrix, int Matrix_dimension){.....}
Is there any way to change the function to
Code: void PGMWrite(int** Matrix || double** Matrix, int Matrix_dimension){.....}
And then have some sort of type identifier in the function that picks the correct section via an if loop? If so how, and how would I identify in the function if the input it type double or int?
View 4 Replies
View Related
Feb 5, 2014
I am using a small robotic-car that is controlled by writing C/C++ codes under Linux. I need to use a particular function from the library provided by the manufacturer. The relevant API documentation for the function is:
BASEBOARD_ERROR_KIND ZMP zrc :: :: :: Baseboard GetRS232Data (char * msg )
RS232 data acquisition.
Argument:
[Out] msg Address of the acquired data.
Returns:
BASE_OK RS232 data acquisition success
BASE_BASE_232_GETDATA_ERR RS232 data acquisition failure
I have trouble writing the relevant code in the main program that invokes this function. Here is a snippet of what I have tried:
# include "Baseboard.h"
int main () {
Baseboard _Baseboard; // Class name is Baseboard
char *msg ;
[Code] ......
The part where I am uncertain is how to handle the char pointer "msg" in the declaration, function call and referencing. According to the documentation, the char pointer "msg" is the output of the function so I presume that is is somehow dynamically allocated. Am I handling the char pointer properly in the declaration, function call and referencing parts?
Another related question I have is: I am printing out the value of the variable "dummy". I always get 0 for it. Since the variable "dummy" is an enum of type BASEBOARD_ERROR_KIND which can take on two values (first value represents success and the second failure), it is alright to get a integer value of 0 for it if the function call was successful ? (I do not have much experience with using enums so this is a enum-related question on whether we can get an integer value representing the first enum value) .
View 2 Replies
View Related