I understand how to pass a 2D array to a function in C, but I would like to have the function update the original structure rather than a copy of it. How would I go about this? Why does the method I use create a copy of the structure - I was under the impression it was another syntax equivalent to using a pointer?
Thanks for any help. I've included code snippets underneath:
Declaring the variables
int R[rowsize][colsize], G[rowsize][colsize], B[rowsize][colsize];
int Rnew[rowsize][colsize], Gnew[rowsize][colsize], Bnew[rowsize][colsize];
Initialising the function
void blur(int rowsize, int colsize, int R[][428], int G[][428], int B[][428], int Rnew[][428], int Gnew[][428], int Bnew[][428]){
. . .
}
NB: Within this function, Rnew, Gnew and Bnew should be updated - each are a 2D array. I would like this to be done without returning anything.
Calling the function
blur(rowsize, colsize, R, G, B, Rnew, Gnew, Bnew)
You may try this way..
# include <stdio.h>
#define rowsize 3
#define colsize 3
int main()
{
int R[rowsize][colsize]={1,2,3,4,5,6,7,8,9};
int Rnew[rowsize][colsize];
copy_from_R_to_Rnew(R,&Rnew);
int i,j;
for(i=0;i<rowsize;i++)
{
for(j=0;j<colsize;j++){
printf("\t %d",Rnew[i][j]);
}
printf("\n");
}
return(0);
}
void copy_from_R_to_Rnew(int *R,int *Rnew)
{
int i,j;
for(i=0;i<rowsize;i++)
{
for(j=0;j<colsize;j++){
*(Rnew+i*colsize+j) = *(R+i*colsize+j);
}
}
}
Related
I'm writing a program that can modify rows and cols from a function of a function. I don't understand how to do pointer of a pointer.
void changeNum(int*setRows, int *setCol)
{
changeNum2(*setRows,*setCol);
}
void changeNum2(int*setRows, int *setCol)
{
*setRows=5;
*setCol=5;
}
int main() {
int*row=10;
int* col=10;
changeNum(&row,&col);
printf("%d %d",row,col);
return 0;
}
First
int*row=10;
int* col=10;
This is wrong. Assigning hardcoded address. You don't want this
int row=10;
int col=10;
How to get the address of the row and col?
&row and &col.
How to pass it to function?
Call it, changeNum(&row,&col);
void changeNum(int*setRows, int *setCol)
{
...
}
How to pass pointer to pointer?
void changeNum(int*setRows, int *setCol)
{
chnageNum2(&setRows, &setCol);
}
ChangeNum2 how it would change value?
void chnageNum2(int **setRows, int **setCol){
**setRows = 110;
**setCol = 110;
}
Can we do the same change using changeNum() only?
Yes we can do that.
void changeNum(int*setRows, int *setCol)
{
*setRows = 110;
*setCol = 110;
}
Definitely check this. Grab a book. It will help a lot.
The complete code will be
void changeNum(int*setRows, int *setCol)
{
changeNum2(&setRows,&setCol);
}
void changeNum2(int**setRows, int **setCol)
{
**setRows=5;
**setCol=5;
}
int main(void) {
int row=10;
int col=10;
changeNum(&row,&col);
printf("%d %d",row,col);
return 0;
}
#include <stdio.h>
void changeNum(int*, int*);
void changeNum2(int*, int*);
void changeNum(int* setRows, int *setCol) {
changeNum2(setRows,setCol);
}
void changeNum2(int* setRows, int *setCol) {
*setRows=5;
*setCol=5;
}
int main() {
int row=10;
int col=10;
changeNum(&row, &col);
printf("%d %d\n", row, col);
return 0;
}
It takes sometime to grasp that every C function parameter is passed by value. So you can safely pass setRows pointer to the second function simply by its value.
Also, it's necessary to declare previously the function changeNum2, I've included the declaration without parameter names to clarify it's possible.
I strongly recommend reading this book: https://en.wikipedia.org/wiki/The_C_Programming_Language, specially chapter 5 (Pointers and arrays).
You can find a PDF copy easily. It's where I finally learned this concept.
can you tell me why this doesn't work.
int function(int);
int main()
{
int g[20],N;
printf("Type N");
scanf("%d",&N);
g[20]=function(N);
printf("s[0] is %d\n",g[0]);
printf("s[1] is %d\n",g[1]);
printf("s[2] is %d\n",g[2]);
}
int function(int N){
int s[20];
s[0]=1;
s[1]=3;
s[2]=5;
return s[20];
}
I just want that my function return this numbers 1,3,5 but it returns some weird numbers, i thinks it's adresses or something.
PS. I just began to learn C.
You need to something like this.
void function(int *);
int main()
{
int g[20],N; // array g local to main funciton
printf("Type N");
scanf("%d",&N);
function(g); // invoke function by passing array base address as an argument
printf("s[0] is %d\n",g[0]); // the first three positions of the g array
printf("s[1] is %d\n",g[1]); // have not been set by function
printf("s[2] is %d\n",g[2]); // they are also all unknown values
}
void function(int *s){
s[0]=1; //*(s+0)
s[1]=3;
s[2]=5;
}
From your code, you seem to have some misunderstanding about arrays and scope. The two arrays you declare start with unknown values until you set them. see my comments to your original code:
int function(int);
int main()
{
int g[20],N; // array g local to main funciton
printf("Type N");
scanf("%d",&N);
g[20]=function(N); // invoke function with int typed in and return int
printf("s[0] is %d\n",g[0]) // the first three positions of the g array
printf("s[1] is %d\n",g[1]); // have not been set by function
printf("s[2] is %d\n",g[2]); // they are also all unknown values
}
int function(int N){ // you never use N in this function, why is it a parameter
int s[20]; // declare a int array local to function
s[0]=1; // you only set the first three items in array, rest are unknown values.
s[1]=3;
s[2]=5;
return s[20]; // return the 20th int item of the array, (unknown memory contents)
}
My question is in general how to use pointers in functions correctly.
if to be more specific I need to write a function the recives 3 values from a user and then retruns it to the main one for further actions.
This is the code I have written so far:
#include <stdio.h>
#include <conio.h>
int inputThree(int, int, int);
int sortTwo(int, int);
int sortThree(int, int);
int main()
{
int a=0, b=0, c=0;
printf("before: func %d \n", b);
inputThree(a,b,c);
printf("after func: %d%d%d \n",a,b,c);
getch();
}
int inputThree(int a, int b, int c)
{
printf("Input three integers values: \n");
scanf("%d%d%d", &a, &b, &c);
return 0;
}
I'm intersted in understanding how to keep the values of scanf via pointers. When I return to the main function they are lost because they aren't global...
Also, I couldn't leave the function inputthree without parameters even though I want it to get them from scanf itself, so I had to put some values for it to run.
thanks in advance!
Pass pointers to the variables from main to inputThree.
Change the function declaration.
int inputThree(int* aPtr, int* bPtr, int* cPtr);
Change the call.
inputThree(&a, &b, &c);
Change the implementation.
int inputThree(int* aPtr, int* bPtr, int* cPtr)
{
printf("Input three integers values: \n");
scanf("%d%d%d", aPtr, bPtr, cPtr);
return 0;
}
You can either return a struct or make a function that handles passed pointers as argument.
#include <stdio.h>
struct Foo{
int x;
int y;
};
//one way
struct Foo do_work();
//or another
void do_work(int *x, int *y);
int main(void) {
return 0;
}
struct Foo do_work(){
//e.g.
struct Foo foo;
foo.x = 1;
foo.y = 2;
return foo;
}
void do_work1(int *x, int *y){
//e.g
*x = 1;
*y = 1;
}
Technically, only 1 thing (or none) can be returned from a function at a time. If you wanted to change the values of two or more variables via a function even after the function ends, you would need to pass into the function's parameters/arguments the memory reference of the variable.
I want to declare procedure with matrix parameter. But I get error. How to declare matrix parameter ?
#include <stdio.h>
//I get error when I declare this
void solvingSudokuPuzzle(int [][]);
int main()
{
return 0;
}
void solvingSudokuPuzzle(int _papanSudoku[9][9]) {
}
void solvingSudokuPuzzle(int [9][9]);
or
void solvingSudokuPuzzle(int [][9]);
or
void solvingSudokuPuzzle(int (*)[9]);
are the right ways to declare the function prototype.
You can only skip the first part of a two dimensional array, so change it to
void solvingSudokuPuzzle(int [][9]);
^
you can skip a value here
If you don't want to specify dimensions, you can use VLA's (Variable-length arrays):
#include <stdio.h>
void solvingSudokuPuzzle(int, int(*)[]);
int main()
{
return 0;
}
void solvingSudokuPuzzle(int size, int (*_papanSudoku)[size]) {
}
Call it using:
solvingSudokuPuzzle(9, arr);
But the correct way is:
void solvingSudokuPuzzle(int (*_papanSudoku)[9]) {
or
void solvingSudokuPuzzle(int _papanSudoku[][9]) {
If size is not fixed, then use a double pointer like:
(int **myMatrix)
This is a simple test program where I am trying to get the func function to modify the variables a and b which are then used in the main function. Is there a way to get func to return the modified variables so they can be used? (preferably without using struct as I don't understand how it works)
#include <stdio.h>
void func(int a, int b)
{
a=a+1;
b=b+1;
}
void main(void)
{
int a=0, b=0;
while (1)
{
func(a,b);
printf("%d\n",a);
}
}
If you want to modify variables in the calling function, you have to pass their address to func (i.e. pass a pointer to these variables
void func(int* a, int* b)
{
*a=*a+1;
*b=*b+1;
}
func(&a,&b);
Your code passes its arguments by value. This means that a and b are copied into new variables which are initialised with the values of the calling variables but only exist for the duration of func.
I agree. The best way to do this is using pointers. But are you aware that your code will never terminate? You should use a condition that can end excution of the while loop.
Also, if you wanna perfom the exact same action to both variables you could just use the return statement like this:
#include <stdio.h>
void func(int a, int b)
{
return a+1;
}
void main(void)
{
int a=0, b=0;
while (a < 5)
{
func(a);
func(b);
printf("%d\n",a);
}
}
We can achieve through pointers . Don't use While(1) because it will go to infinite loop. I think following program help to you.
#include <stdio.h>
void func(int* a, int* b)
{
*a=*a+1;
*b=*b+1;
}
void main(void)
{
int a=0, b=0;
//while (1)
// {
func(&a,&b);
printf("%d\n",a);
// }
}