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

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 ;)

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;

How do I call this function with the arguments (void (*store) (int*,int))?

I'm new to c and not really familiar with pointers or how this method is setup to be called in main with these arguments. I have a bit of an understanding of pointer snow, but i'm still confused with one being in the method arguments. Do I pass in a pointer and an int? Do I need to pass in anything at all? Do I even need the main method or can I just run the program with is_little_endian as my main method?
#include "test_endian.h"
#include <stdio.h>
int is_little_endian(void (*store)(int*,int)) {
int x;
unsigned char *byte_ptr = (unsigned char*)(&x);
store(&x, 1);
printf("the address for x is %u\n", *byte_ptr);
return 0;
}
int main() {
}
Function is_little_endian accepts only one parameter which is neseccary.
This parameter is a pointer to a function, which accepts pointer to int, then int and returns nothing (void). You just need to pass there a pointer to some function, like that:
void example(int * a, int b) { }
int main() {
is_little_endian(example);
}
Or any other function you wish. You can read more about pointers to function there: How do function pointers in C work?
And yes, you need the main method to run the program, like your body needs your heart. ;)

How to pass a numeric value into a function that requires pointer value in C

My function is void x(int *y);
I want to call it like x(&6); or x({6});, I do not want to define an integer and assign 6 to it, I want single line.
What you need is compound literals
#include <stdio.h>
void x(const int *y)
{
printf("%d\n", *y);
}
int main(void)
{
x(&(int){6});
return 0;
}
This can be done using compound literals:
x((int []){6});
or somewhat simpler:
x(&(int){6});
The called function, of course, can make no assumptions about the longevity of the pointed-to data, since it will be gone by the time the call returns.

Array size in function declaration

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);

Pointers as function parameters in C

I am having trouble getting this to work.
I have variables initiated in main which I want to pass onto other functions and have changed. I know the only way this can be done is with pointers or to declare the variables outside the main function.
I would prefer to use pointers
How is it done?
eg
int main(){
int variable1 = 5;
add(&variable1, 6);
printf("%d", variable1);
return 0;
}
int add(int *variable1, int addValue){
variable1 += addValue;
return 0;
}
I want to print 11 but I don't know how these pointers work through other functions
You simply need to dereference your pointer:
void add(int *variable1, int addValue)
{
*variable1 += addValue;
}
In your function call, you pass in "&variable1" which means 'a pointer to this variable'. Essentially, it passes in the exact memory location of variable1 in your main function. When you want to change that, you need to dereference by putting an asterix "*variable1 += 6". The dereference says 'now modify the int stored at this pointer'.
When you use the asterix in your function def, it means that 'this will be a pointer to an int'. The asterix is used to mean two different things. Hope this helps!
Oh, and also add the explicit type to the function call:
void add(int *variable1, int addValue)
You simply forgot to dereference the pointer:
*variable1 += addValue;
And all the function parameters must have an explicit type.
void add(int *variable1, int addValue)
...you just need *variable1 = addvalue;, it was almost right...as is you just added 1 to the pointer, which vanished as soon as add() returned...

Resources