Pointer with Structure

Pointer to structures

If you want a pointer to a structure you have to use the -> (infix operator) instead of a dot.
Take a look at the following example:

 #include<stdio.h>

 typedef struct telephone
 {
  char *name;
  int number;
 }TELEPHONE;

 int main()
 {
  TELEPHONE index;
  TELEPHONE *ptr_myindex;

  ptr_myindex = &index;

  ptr_myindex->name = "Jane Doe";
  ptr_myindex->number = 12345;
  printf("Name: %s\n", ptr_myindex->name);
  printf("Telephone number: %d\n", ptr_myindex->number);

  return 0;
 }

Note: The -> (infix operator) is also used in the printf statement.


Spread the word! By sharing this Post!

Pageviews This Week