Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointers and Arrays. 1

Status
Not open for further replies.

Disruptive

Programmer
Mar 8, 2005
40
0
0
GB
Peraps this one is obvious. I have te following and wish to create a pointer to point at a two dimensional array which is declared as below. I then wish to send this function in the following way to a function. Pointer asignment does not work.

I have:


void f1(int ***ptrCell)
{
(*ptrCell)[0][1] = 7777;

}

int Cell[27][2];
int **ptrCell;

ptrCell = &Cell;
f1(&ptrCell);
 
Code:
#include <stdio.h>

void f1(int (*ptrCell)[27][2])
{
   (*ptrCell)[0][1] = 7777;

}

int main(void)
{
   int Cell[27][2] = {0};
   int (*ptr)[27][2] = &Cell;
   f1(ptr); /* or f1(&Cell); */
   printf("Cell[0][1] = %d\n", Cell[0][1]);
   return 0;
}

/* my output
Cell[0][1] = 7777
*/
 
Whats also the best method of sending the parameters from f1 to another function?

Also what is the type of prototype for f1 above called?

Thanks
 
As usually in C (with its horrible declaration syntax), use typedefs:
Code:
typedef int (*ArrPtr)[27][2];
/* then... */
void f1(ArrPtr);
/* and so on: */
ArrPtr ptr = Cell;
...
 
You mean something like this?
Code:
#include <stdio.h>

[blue]void f0(int (*ptrCell)[27][2])
{
   (*ptrCell)[0][0] = 3333;
}[/blue]

void f1(int (*ptrCell)[27][2])
{
   (*ptrCell)[0][1] = 7777;
   [blue][b]f0(ptrCell);[/b][/blue]
}

int main(void)
{
   int Cell[27][2] = {0};
   int (*ptr)[27][2] = &Cell;
   f1(ptr); /* or f1(&Cell); */
   [blue]printf("Cell[0][0] = %d\n", Cell[0][0]);[/blue]
   printf("Cell[0][1] = %d\n", Cell[0][1]);
   return 0;
}

/* my output
[blue]Cell[0][0] = 3333[/blue]
Cell[0][1] = 7777
*/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top