Array size in function declaration - c

I want to use a function that takes a two dimensional array of fixed size as parameter. The value of nm_pipe is known during the execution and the value of the other dimension is always 2.
int main(int argc, char const *argv[]){
int num=2;
int nm_pipes;
/*some code that computes nm_pipe*/
int pipe_container[nm_pipes][num];
fill_pipe_container(nm_pipes,pipe_container);
}
Now, I have a problem with the declaration for the function fill_pipe_containter.
If I try somehting like this
void fill_pipe_container(const int nmp,int pc[][]);
I get an error because the array has no bounds.
If I try this:
void fill_pipe_container(const int nmp,int **pc);
I also get an error because of the type conversion.
I also tried this
void fill_pipe_container(const int nmp,int (*pc)[2]);
But I am pretty sure that it is wrong since that would mean that I expect a pointer on an array of 2 elements, however the program compiles with this one.
I can also do this:
void fill_pipe_container(const int nmp,int pc[][2]);
But I want to be able to declare the function without giving any size explicitly. What is the correct way of declaring this function and what should I avoid?

void fill_pipe_container(const int nmp,int (*pc)[2]);
This one is the correct one. Why do you think it is wrong?

Try void fill_pipe_container(const int nmp,int pc[][num]). Should be working.

You can use this:
void fill_pipe_container(const int nmp,int **pc);
and in the function call use this:
int **pipe=(int **)pipe_container;
fill_pipe_container(nm_pipes,pipe);

Allocate the memory of the array in the heap:
int **pipe_container=(int **)malloc(nm_pipes*sizeof(int *));
int i;
for(i=0;i<nm_pipes;i++)
pipe[i]=(int *)malloc(num*sizeof(int));
Prototype:
void fill_pipe_container(const int nmp,int **pc);
Then call the function:
fill_pipe_container(nm_pipes,pipe);

Related

double pointers error in C when compiling error when using void as well

I'm new to C and im currently learning about pointers.
I'm not sure why I am getting an error with the following sections of code in regards to pointers :
char ch;
char** pointer;
pointer = &ch;
and
int function1(void)
{
return 42.0;
}
void function2(void)
{
void (*pointer)(int);
pointer = &function1;
...
}
Any help will be appreciated :)
The very first problem is that you are using a double pointer in char** pointer ,as you are not storing the address of some other pointer so you should use char *pointer instead.
Then your function1 has return type as int but you are returning a float value ,although it won't give you any error but it can create some logical issues in your program,so better to properly write the return type in function definition and its prototype.
Then the next problem is in the function2,your function1 returns int but does not take any arguments but your function pointer return void and take int ,so you should better modify this to
int (*pointer)(void);
and then store the address of function1 in pointer ,it will work fine.
* is a single pointer and ** is a pointer to pointer.
So instead of
char** pointer;
It should be:
char* pointer;
In the second case, the function pointer prototype is not matching the prototype of the function it is pointing to.
So instead of
void (*pointer)(int);
it should be:
int (*pointer)(void);
you second section have some mistakes
you function1() return int and not take args
but your fucntion ptr return void and take int
so change it to:
int (*pointer)(void);
pointer = &function1;

Function parameter as array with declared size

I frequently use the following convention to inform client code that a function requires an argument of an array with defined size:
/* File foo.h */
int foo (int arg[10]);
The message I want to give to client code is that they must provide an array of type int with 10 positions.
I am aware that it is not very usual, so I came here to ask: Am I missing any side effect of this convention ? Is it anyhow harmful?
Thank!
If you want to insist on getting an array of size 10, you can use:
int foo (int (*arg)[10]);
The ill-side effects of this are:
In the function, you have to use:
(*arg)[index]
instead of just
arg[index]
The calling function must use:
int array[10];
foo(&array);
instead of
int array[10];
foo(array);
You cannot use an array that has more than 10 elements.
int array[20];
foo(&array); // Not OK.
You cannot use a malloced array.
int* array = malloc(sizeof(int)*10);
foo(array); // Not OK.
Now pick the solution that is least harmful.
struct arrayContainerTen{
int data[10];
}
void aFunction(struct arrayContainerTen *pAnArray)
{
size_t size = sizeof(pAnArray->data);
}
main()
{
arrayContainerTen anArray;
aFunction(&anArray);
}
There's no harm in writing it like this. But just be aware that the compiler will not enforce the requirement. A declaration like that is treated by the compiler as if you'd written.
int foo(int *arg);

call-by-reference with an int array. 2 functions

hey i try a call by reference from an int array:
void function2(int stapel,int colour){
stapel[1]=stapel[0]
stapel[0]=colour;
}
void function1(int stapel){
int colour=2;
function2(stapel,colour);
}
int main(){
int *stapel;
stapel=malloc(sizeof(int)*2);
function1(stapel);
}
whats wrong? :O i want to use stapel now in my main function.
You had wrong function declarations, your functions are receiving pointers.
You need to use
void function2(int *stapel,int colour){...
void function1(int *stapel){...
Instead of just int stape. This is the complete code:
void function2(int *stapel,int colour){
stapel[1]=stapel[0]
stapel[0]=colour;
}
void function1(int *stapel){
int colour=2;
function2(stapel, colour);
}
int main(){
int *stapel;
stapel=malloc(sizeof(int)*2);
function1(stapel);
free(stapel); // Also free the memory
}
As pointed out in the comment, also remember to free the memory at the end (here it makes no actual difference because the program will be terminated, but is always a good practice).
In the main function you are passing a pointer to int as argument, but the functions does not take pointer to int. Change the functions to take pointers. The errors you get should be pretty obvious in this regard.
You also have a ordering-problem in our function2 where you use uninitialized data to initialize stapel[1].
stape1 is a pointer to int and you can not pass it as argument to a function that expects a integer value. However from the use in the function bodies it seems you should change the function declaration to expect int* instead of ints.
check this
Edit this
function1(int *stapel)
and
function2(int *stapel,int colour)
You are not "calling-by-reference" you just say int stape1 in the function signature, if you wish to use a reference (or an array) in C pass a pointer.
void function1(int * stapel){
//...
}
Then you can pass the pointer from main
function1(stapel);
Don't forget to free it afterwards
free(stape1);
#include<stdio.h>
#include<malloc.h>
void function2(int *stapel,int colour){
stapel[0]=colour;
stapel[1]=stapel[0];
}
void function1(int *stapel){
int colour=2;
function2(stapel,colour);
}
int main(){
int *stapel;
stapel=malloc(sizeof(int)*2);
function1(stapel);
printf("stapel[0]=%d stapel[1]=%d",stapel[0],stapel[1]);
return 0;
}
try this code it works correct.......
in your program you are passing a pointer but defined it as integer
and there are other problem that i corrected ........
if you have any confusion you can ask............
I think you may have to use int *stapel in the function parameter list. I think int stapel is a value of type int pushed on the stack of the function. I think int *stapel is a pointer to an address pushed on the stack. Finally, I seem to remember using &stapel to dereference the pointer. It has been a long time for me. I am sure one of the bright folks will give you a better answer ;)

How to put parameters(2nd grade array) to sub() ,,...void func( int* Array2nd)

void HowToPutParameters( XXXXXX) // how to put parameters in XXXXXX
{
array[0][0]=5;
}
void main()
{
int array[2][2]={{1,2},{3,4}};
HowToPutParameters(&array[0][0]); // &array[0][0] is correct or &array[0]
}
// I wana put this array to HowToPutParameters;
// 1.I am not sure "&array[0][0] or &array[0]" are correct,The compiler seems identify;
// I've tried void HowToPutParameters(int *array ) ==> compile failed
// I've tried void HowToPutParameters(int *array[][] ) ==> compile failed
// My compiler is MPLAB X IDE V1.8 , Chip is Microchip>PIC32MX795F512
If you want use pass two dimensions array params,use:
void func(int (*a)[2]);// tell the function the first dimension size!
func(array);
It is the same as:
void func(int a[][2]);
func(array);
If you pass &array[0][0], it means you pass a pointer point to a int.
void func(int *element);
Like this, in your code you are only passing a pointer to an int element of the array, not the array itself.
void HowToPutParameters(int* element)
And you should always use int main instead of void main.

Handing array over to function. Correct use of pointers?

I have an array/pointer related problem.
I created an int array myArray of size 3. Using a function I want to fill this array.
So I'm calling this function giving her the adress &myArray of the array.
Is the syntax correct for the function declaration`? I'm handing over the pointer to the array, so the function can fill the array elements one by one.
But somehow my array is not filled with the correct values.
In Java I could just give an array to a method and have an array returned.
Any help is appreciated! Thanks!
#include <stdio.h>
int myArray[3];
void getSmth(int *anArray[]);
int main(void)
{
getSmth(&myArray);
}
void getSmth(int *anArray[])
{
for(i=0...)
{
*anArray[i] = tmpVal[i];
}
}
Remove one level of indirection:
#include <stdio.h>
int myArray[3];
void getSmth(int anArray[]);
int main(void)
{
getSmth(myArray);
}
void getSmth(int anArray[])
{
for(i=0...)
{
anArray[i] = tmpVal[i];
}
}
Also, as others have suggested, it would be a good idea to pass the size of the array into getSmth().
No, the syntax is not correct. You have an extra *, making the argument into an array of pointers.
In general, it's better to use:
void getSmth(int *array, size_t length);
since then the function can work on data from more sources, and the length becomes available which is very handy for iterating over the data as you seem to want to be doing.
You'd then call it like so:
int main(void)
{
int a[12], b[53];
getSmth(a, sizeof a / sizeof a[0]);
getSmth(b, sizeof b / sizeof b[0]);
}
Note the use of sizeof to compute (at compile-time) the number of elements. This is better than repeating the numbers from the definitions of the variables.
Right now, your function accepts an int *anArray[] parameter, which is an array of pointers to int. Remove the unneccessary * and your function signature should look simply like this:
void getSmth(int anArray[]); // array of int
or
void getSmth(int *anArray); // pointer to first array element of type int
You should use either int anArray[] or int *anArray (which is effectively the same, because array decays to pointer). You should also make sure that the function knows how big your array is either by agreement or passing it as a parameter for it can not use sizeof for the purpose.

Resources