声明指向2-D数组指针的指针

时间:2017-07-07 20:12:48

标签: c pointers

#include<stdio.h>
#include<conio.h>

void main()
{
  int a[5][5];
  int (*p)[5];

  clrscr();

   p=a;
  a[0][0]=5;


  printf("%d\n",p[0][0]);  //This prints 5.

  getch();
}

我的问题是,如果我们想要一个指向p本身的指针(双指针),然后访问这些元素,那么我们如何声明它?

2 个答案:

答案 0 :(得分:2)

@SpringUI(path = "/person") @Theme("valo") public class PersonForm extends UI{ @Autowired PersonService personService; List<Person> entries = new ArrayList<Person>(); private TextField firstName = new TextField("First Name"); private TextField lastName = new TextField("Last Name"); private TextField email = new TextField("Email Address"); private TextField salary = new TextField("Salary"); private Person person = new Person(); private Grid<Person> grid = new Grid<>(Person.class); final private FormLayout layout = new FormLayout(); @Override protected void init(VaadinRequest request){ grid.getEditor().isEnabled(); grid.setColumnOrder("firstName", "lastName", "email", "salary"); layout.addComponent(firstName); layout.addComponent(lastName); layout.addComponent(email); Button save = new Button("Save", e-> { addPersonToGrid(new Person(firstName.getValue(), lastName.getValue(), email.getValue(),salary.getValue())); Notification.show("PERSON WAS SAVED"); }); layout.addComponent(save); layout.addComponent(grid); layout.setMargin(true); setContent(layout); } public void addPersonToGrid(Person person){ entries.add(person); grid.setItems(entries); } } 是一个2D数组。

a

int a[5][5]; pointer to a 2D array

p
  

如果我们想要一个指向p本身的指针(双指针),然后访问这些元素,那么我们如何声明它呢?

int (*p)[5][5] = &a;

答案 1 :(得分:0)

这是您可以使用typedef

的完美示例
typedef int fiveByFiveGrid[5][5];
typedef int (*rowPtr)[5];
typedef rowPtr *rowPtrPtr;

void main()
{
  fiveByFiveGrid a;
  rowPtr p;
  rowPtrPtr pp;

  p=a;
  pp=&p;
  a[0][0]=5;

  printf("%d\n",*pp[0][0]);  //This prints 5.                                                                                                                                                                      
}

如果您需要非常复杂的声明,这项技术非常有价值。您可以使用它逐步构建声明。调用rowPtrPtr ptr将与int (**ptr)[5][5]

相同