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)
Related
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);
}
}
}
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.
I am a Beginner in Functions.I wanna integrate that Functions in my main program. The program should scan a int number and then square it(sum=b*b). Then the program should output the result.
#include <stdio.h>
#include <stdlib.h>
void funktion(int);
void out(int);
int main(int)
{
int sum,b,v,w,z;
{
funktion();
calculator();
out();
printf("%i",sum);
}
return 0;
}
void funktion(int v)
{
printf("Enter any number that is to be squared!");
}
void calculator(int w) //calculate b*b
{
scanf("%i",&b);
sum=b*b;
}
void out(int z)
{
printf("Sum:");
}
Please give me some tips. ;)
Thx & Best regards!
Are you looking for this kind of solution: i just made little changes. you may need to do some initializing things if u gonna use this code
#include <stdio.h>
#include <stdlib.h>
void funktion(int);
void out(int);
int main()
{
int sum,b;
{
printf("Enter any number that is to be squared!");
scanf("%i",&b);
sum = funktion(b);
out(sum);
}
return 0;
}
int funktion(int b)
{
return b*b;
}
void out(int sum)
{
printf("Sum:%d",sum);
}
void funktion(int);
void out(int);
These two functions need not contain any arguments as you declared to be int's, because you are not performing any computation on sum, since they are just prints inside those functions.
So make them as,
void funktion(void);
void out(void);
and call those routines as,
funktion();
out();
void funktion(int)
{
printf("Enter any number that is to be squared!");
}
void out(int)
{
printf("Sum:");
}
to call these functions you are passing some value so you receive them with parameters.
void funktion(int a)
{
printf("Enter any number that is to be squared!");
}
void out(int b)
{
printf("Sum:");
}
You can make use of the passed value in functions using these variables, a nd b in corresponding functions.
If you dont want to access those passed values in the called functins then you dont need to pass the value. So change the functions to receive nothing, it can be
#include <stdio.h>
#include <stdlib.h>
void funktion();
void out();
int main()
{
int sum,b;
{
funktion();
scanf("%i",&b);
sum=b*b;
out();
printf("%i",sum);
}
return 0;
}
void funktion()
{
printf("Enter any number that is to be squared!");
}
void out()
{
printf("Sum:");
}
make the call to function without passing arguments. you can even specify void as type to indicate that function doesn't take any parameters.
To calculate in other function and return the value use return statement.
int calculator() //calculate b*b
{
scanf("%i",&b);
sum=b*b;
return sum;
}
change the function call statement to,
sum=calculator();
As above, say I have a 3 dimensional array, a[][][], and I want to pass this to a function; how should I declare the function parameter?
void function1(int array[][3][4])
{
...use array here...
}
void function2(void)
{
int array[20][3][4];
...load array...
function1(array);
}
Just declare a triple pointer
int functionName(int*** arrayPtr, int x, int y, int z){
return arrayPtr[z][y][x];
}
I would send a pointer to pointer to pointer with all dimensions.
void foo(int ***ar, size_t l, size_t m, size_t n)
{ /* ... */ }
How would I create an array of ten function pointers? What I have is a for loop, and I want to set a function pointer to a different function on each iteration. so:
//pseudocode
for i (0..10)
function = array_of_functions[i];
//...
// Define alias for function pointer type for convenience
typedef void (*action)(int);
// Example function
void print(int) { ... }
action fs[10] = { print, ... };
for (int i = 0; i < 10; ++i)
{
action f = fs[i];
// Call it somehow
f(i * i);
}
This code:
return_t (*array_of_functions[10])(arg1_t, arg2_t);
Declares "array_of_functions" as a 10-element array of function pointers where each pointed-to function takes two arguments of type arg1_t and arg2_t and returns type return_t. Replace types and adjust the number of arguments as appropriate.
Any time you have to deal with ugly function pointer syntax it's better to use a typedef.
#include <iostream>
void a(int i)
{
std::cout<<"a: "<<i<<std::endl;
}
void b(int i)
{
std::cout<<"b: "<<i<<std::endl;
}
typedef void (*fn)(int);
int main(int argc, char**argv)
{
fn foo[2];
foo[0] = a;
foo[1] = b;
for(size_t i = 0; i < sizeof(foo) / sizeof(foo[0]); ++i)
{
foo[i](i);
}
return 0;
}
The simplest way to do it is to create a typedef for your function, and then declare an array with that type. To create a typedef for the function: typedef returntype (*typedefname)(argtype1,argtype2,...,argtypeN); EX:
#include <stdio.h>
#include <stdlib.h>
typedef void (*functype)();
void func1()
{
//...
}
void func2()
{
//..
}
//...
void func10()
{
//...
}
int main(int argc, char* argv[])
{
functype array[] =
{
&func1,
&func2,
&func3,
&func4,
&func5,
&func6,
&func7,
&func8,
&func9,
&func10
};
// Use the array...
return 0;
}
T (*array_of_functions[10])();
Where T is the return type of each function (all functions return the same type, naturally). Things get tricky if you want to store pointers to functions with different numbers/types of parameters:
int foo(void) {...}
int bar(int x) {...}
int bletch(double y, double z) {...}
...
int (*array_of_functions[10])() = {foo, bar, bletch, ...};
If so, you'll have to keep track of what number and types of parameters each function requires somehow so you can call it correctly.
I'm actually kind of down on typedefs for function pointer types; they tend to obscure as much as they simplify.