Passing pointer to function WITHOUT modifying argument? - c

I have a matrix M[2][2] and want to make a call to function dontModify(M) that will play around with the elements of M but not change them. Something like:
dontModify(M):
swap off-diagonal elements;
take determinant of M;
return determinant;
...but without having the function change M in the process. Anything convenient that would accomplish this?

Create a local copy of the matrix inside the function, one that you can do whatever you want with.
int some_function(int matrix[2][2])
{
int local_matrix[2][2] = {
{ matrix[0][0], matrix[0][1] },
{ matrix[1][0], matrix[1][1] },
};
/* Do things with `local_matrix` */
/* Do _not_ use `matrix` */
...
}

Frankly speaking do not undestand your problem. You are working with matrix, so it will be passing via pointer to function. So just create a copy of your matrix, play with it, destroy the copy before returning back. In case this call will be very frequent, you can try to save some time and just work in-place, just do not forget to swap off-diagonal elements back before return.

Related

How to use 2D calloced arrays in functions in C?

I need to create a matrix calculator, for that I think the best is to create functions for +,-,*,/.
But a lot of troubles come with this idea.
I create a calloced array like:
int **matrix = NULL;
matrix=calloc(cols,sizeof(int*));
for(int i=0;i<cols;i++) {
matrix[i]=calloc(rows,sizeof(int*));
}
For now I want to create a function where I want to work with this array.
void addition(int **array,int rows, int cols){
// ** some algoritm here**
return (the result of addition stored in 2D array);
}
I also tried to google the problem, but I didn't underestand the solutions. What I need to explain the way how to pass the array to function, just for reading.
After that I need to return some pointer to new array created as the result of addition. I will probably create the new array in the function.
But If I wanted to write something in array created in main, how to pass and use it in the function?

Passing a pointer of a structure from a function to another function

I have a structure of structures which is global. I am using functions to change the data of the structure.
In general I manipulate the structure easily (e.g. send it to a function).
My problem is that now I have a function that changes the data of the structure, but it also has to call another function. And this is my problem.
In general my structure is the: "name".
I have a pointer: name_ptr->name[i]....
In case of a function I am passing it like this:
find_max = calc_max(i, &name_ptr);
and this function id declared as:
int find_max(int x, vectname **pr)
So inside the function I am working like this:
(*pr)->name[i]...
If I am into the function and I want to sent this pointer to another function how can I call it?
In simple words you have an array of structures and to your first function you get the pointer to the array.
So either you can use:
int find_max(int x, vectname **pr)
{
// method - 1 (if your second function is intreseted only in changing the nth structure instance of the array )
SecondFunc( &pr[n]);
// method - 2 (if your second function is intreseted in changing the any structure instance of the array )
SecondFunc( pr );
}
Your question is quite cryptic but i'll give it a shot:
int find_max(int x, vectname **pr)
{
...
another_function(x, pr);
...
}

Keeping only a few changes from the data structure

I was not sure, about the title of the question, but here's the problem.
I have an array of structures, now I pass it by reference to a function, where in, I have a priority queue, of the same structures.
Now, I process my array, by making use of the priority queue, and in the end of the process, I will have two of the attributes(say A and B) of my structure, in array changed, while I want only one of the change(A) to be reflected outside the function, and I want the other(B) change to be restored, the way it was before passing the array. I have to do it, because for the second time when I have to process the same array, but with different parameters, I want the changes of A to remain, but B to not.
What I do right now is that, after the process, once I am out of the function, I process the entire array, and where ever I have the value changed for attribute B I revert it back to the initial value. Now this is obviously a O(n2) operation which is no good for me.
Can I do something to make it more efficient.
I am working with C.
If your B is of type bool for example, you can create a bool array in the function. Use the indices of the input array (integers) in the priority queue instead of structures. Use the bool array to access the Bs of the input array:
void func(Type *Input, size_t n)
{
bool B[n];
queue<size_t> pqueue;
for (size_t i = 0; i < n; i++)
B[i] = Input[i].B;
size_t index;
while (index = pqueue.front()) {
// do something with Input[index]
// B[index] = false;
// rest of the function
}
}

I need to make a global array in C with a size inputted by the user

Basically I need to make a global variable in C that is an array. The Array will be [n][22][n+1] where n is either 3,4,5 or 6 and is selected by the user.
Is there a way to do this or should I just make the array [6][22][7], and have the functions dealing with it only use the parts up to n (if that makes any sense)?
I've had to do this before for a computer science class but can't remember exactly how to do it.
For an array that small (well, assuming reasonably sized data types), you might just be better off making the [6][22][7] allocation you mention in your question - it's not like you're going to waste that much space. Unfortunately for you, C99 variable length arrays don't work for global arrays. That means your only other option is dynamic allocation using malloc()/free().
You can use a file scope pointer that points to the first element of an array you dynamically allocate (malloc function) in a function.
As was mentioned previously, in this particular case, doing anything else than a static assignment of [6][22][7] would be a waste of time. If you really want to dynamically allocate the array using malloc :
/* Suppose that you want a [5][22][6] */
int main() {
int i,j,k;
int ***boo;
int d_1,d_2,d_3;
d_1=5;
d_2=22;
d_3=6;
/*
+------------------------------------------+
| For each dimension, a malloc is needed |
+------------------------------------------+
*/
boo = malloc(d_1*sizeof(int*));
for (i=0;i<d_1;i++) {
boo[i] = malloc(d_2*sizeof(int*));
for (j=0;j<d_2;j++) {
boo[i][j] = malloc(d_3*sizeof(int*));
for (k=0;k<d_3;k++) {
boo[i][j][k] = i+j*k;
}
}
}
/*
+----------------------+
| Testing the values |
+----------------------+
*/
for (i=0;i<d_1;i++) {
for (j=0;j<d_2;j++) {
for (k=0;k<d_3;k++) {
printf("%d ",boo[i][j][k]);
}
printf("\n");
}
}
return 0;
}
This would essentially do the trick. It might be useful, if you have a greater amount of data.
Don't forget to deallocate the memory using free()

how to change one array into another while canceling the recurring variables?

I'm new to programing and was given a task of making a function that puts one array into the other with the following criteria: a variable in the destination array will repeat only once and the source and destination array will be of the same size.
the function i came up with is:
int RemoveDup (int src[],int dst[])
//recive two array compare them and copy the src array to dst,and only the none reacuring
//numbers,the arrays must be from the same size
{
int size_src;
int size_dst;
int i,n=0;
size_src = sizeof(src)/sizeof(int);//determine the size of source array
size_dst = sizeof(dst)/sizeof(int);//determine the size of destination array
if (size_src = size_dst);//checks that the array are in the same size
{
for(i = 0;i < size_src;i++)//the loop for advancing the copying process
{
dst[i] = src[i];
}
while (i<size_dst)
{
dst[i] = dst[i++];
if (dst[i] = dst[i++])//relay on the fact that if the function will find a similar varibale, the tested varibale will be set to 0 and the other one will come out clean in the check
dst[i] = 0;//eliminating the varibale in that specific address
}
}
return dst [i];
but it doesn't seems to work and have no idea where it is going wrong.
any help or clue will be appreciated .
I noticed that you're using sizeof(src) within a function that takes int src[] as a parameter. This is not doing what you think it is doing. In C, the size of arrays is not passed to functions along with the array itself (unlike some other languages you may be familiar with). You will have to pass the actual size as a separate parameter.
Also, some printf() statements will definitely help your debugging efforts. Make sure values are what you think they should be. Hopefully you have access to an interactive debugger, that would probably be really useful for you too.
In C you cannot declare a function that takes a parameter that is an array. When you use an array declarator as a function parameter the type is silently adjusted to the corrsponding pointer type. Any explicit array size (if specified) is discarded.
In other words, when you use this:
int RemoveDup (int src[],int dst[])
it is exactly equivalent to this:
int RemoveDup( int *src, int *dst )
It should now be obvious why sizeof(src)/sizeof(int) doesn't do the calculation that you wanted it to do.
well in fact it is possible to make a function recieve an array and not a pointer (given of course the size of the array is pre-defined).
so you can use:
int RemoveDup(int src[M],int dst[N]){
.
.
.
return whatever;
I will agree though that using pointers is better.
In my opinion you should write a recursive function to do that using of course pointers.
so that the next call is (*src+1) so you look at the next cell.
the exit condition is then:
if (sizeof(src) == 0) {
//exit recursion statement.
}

Resources