fork download
  1. #include <stdio.h>
  2.  
  3. // a structure to hold the employee info
  4. struct employee {
  5. long int clockNum;
  6. float wageRate;
  7. float hours;
  8. };
  9.  
  10. int main(void) {
  11.  
  12. float * floatPtr; // pointer to a floating point variable
  13. float wageRate; // hourly wage rate for employee
  14.  
  15. floatPtr = &wageRate; // set pointer to address of WageRate
  16. // Note: The contents of a pointer
  17. // variable is an address
  18.  
  19. *floatPtr = 10.60; // set wageRate indirectly with a pointer
  20. // or
  21. wageRate = 10.60; // set wageRate directly via variable name
  22.  
  23. int clockNum; // unique ID for an employee
  24. int * intPtr; // pointer to an integer variable
  25.  
  26. intPtr = &clockNum; // set intPtr to point to clockNum
  27.  
  28. *intPtr = 98401; // set clockNum using the pointer
  29.  
  30. char firstName [6] = {"Tim"}; // char array with a string
  31. char * charPtr; // pointer to a character
  32.  
  33. charPtr = firstName; // set pointer to first element in the array
  34. // … same as setting it to: &firstName[0]
  35. // Note that charPtr can point to any array
  36. // element in firstName since each is a char type
  37.  
  38. struct employee myEmp; // structure variable to hold employee
  39. struct employee * empPtr; // pointer to structure of that type
  40.  
  41. empPtr = &myEmp; // set empPtr to point to the structure
  42.  
  43. myEmp.clockNum = 98401; // set this member directly
  44.  
  45. (*empPtr).wageRate = 10.60; // use the pointer to set this member
  46.  
  47. empPtr->hours = 51.0; // use short hand notation with the
  48. // pointer to set this member
  49.  
  50. printf ("\nType\t\tByte Size");
  51. printf ("\n----------\t---------");
  52. printf ("\n%-10.10s %i", "int", sizeof(int));
  53. printf ("\n%-10.10s %i", "long int", sizeof(long int));
  54. printf ("\n%-10.10s %i", "float", sizeof(float));
  55. printf ("\n%-10.10s %i", "double", sizeof(double));
  56. printf ("\n%-10.10s %i", "char", sizeof(char));
  57.  
  58.  
  59. printf ("\n\n\nVariable\t\t\tBytes\tValue\t\t\tStart Address");
  60. printf ("\n--------\t\t\t-----\t--------------\t--------------");
  61. printf ("\n%-20.20s%i\t\t%6i\t\t\t%p", "clockNum", sizeof(int), clockNum, &clockNum);
  62. printf ("\n%-20.20s%i\t\t%p\t%p", "intPtr", sizeof(int *), intPtr, &intPtr);
  63. printf ("\n%-20.20s%i\t\t%5.2f\t\t\t%p", "wageRate", sizeof(float), wageRate, &wageRate);
  64. printf ("\n%-20.20s%i\t\t%p\t%p", "floatPtr", sizeof(float *), floatPtr, &floatPtr);
  65. printf ("\n%-20.20s%i\t\t%p\t%p", "charPtr", sizeof(char *), charPtr, &charPtr);
  66. printf ("\n%-20.20s%i\t\t%6i\t\t\t%p", "myEmp", sizeof(struct employee), myEmp, &myEmp);
  67. printf ("\n%-20.20s%i\t\t%p\t%p", "empPtr", sizeof(struct employee *), empPtr, &empPtr);
  68. printf ("\n%-20.20s%i\t\t%6i\t\t\t%p", "empPtr->clockNum", sizeof(long int), empPtr->clockNum, &empPtr->clockNum);
  69. printf ("\n%-20.20s%i\t\t%5.2f\t\t\t%p", "empPtr->wageRate", sizeof(float), empPtr->wageRate, &empPtr->wageRate);
  70. printf ("\n%-20.20s%i\t\t%5.1f\t\t\t%p", "empPtr->hours", sizeof(float), empPtr->hours, &empPtr->hours);
  71.  
  72. // use array index
  73. printf ("\n\n***Print using the array indexes***");
  74. printf ("\nArray");
  75. printf ("\nElement\tValue\tAddress");
  76. printf ("\n-------\t-----\t--------------");
  77. for (int i = 0; i< 6; ++i)
  78. {
  79. printf("\n%i\t\t%c\t\t%p",i, firstName[i], &firstName[i]);
  80. } // for
  81.  
  82. printf("\n\nUsing the array name, the string is %s", firstName);
  83.  
  84. // use the pointer to print just the String and info
  85. printf ("\n\n***Print just the String using pointers***");
  86. printf ("\nArray");
  87. printf ("\nElement\tValue\tAddress");
  88. printf ("\n-------\t-----\t--------------");
  89. for (int index = 0; *charPtr != '\0'; ++charPtr, ++index)
  90. {
  91. printf("\n%i\t\t%c\t\t%p",index, *charPtr, charPtr);
  92. } // for
  93.  
  94. charPtr = firstName; // reset pointer to start of array
  95. printf("\n\nUsing a pointer to the start of the array, the string is %s", charPtr);
  96.  
  97. return 0;
  98. }
  99.  
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
Type		Byte Size
----------	---------
int         4
long int    8
float       4
double      8
char        1


Variable			Bytes	Value			Start Address
--------			-----	--------------	--------------
clockNum            4		 98401			0x7ffe44b242cc
intPtr              8		0x7ffe44b242cc	0x7ffe44b242d8
wageRate            4		10.60			0x7ffe44b242c8
floatPtr            8		0x7ffe44b242c8	0x7ffe44b242d0
charPtr             8		0x7ffe44b24302	0x7ffe44b242e0
myEmp               16		 98401			0x7ffe44b242f0
empPtr              8		0x7ffe44b242f0	0x7ffe44b242e8
empPtr->clockNum    8		 98401			0x7ffe44b242f0
empPtr->wageRate    4		10.60			0x7ffe44b242f8
empPtr->hours       4		 51.0			0x7ffe44b242fc

***Print using the array indexes***
Array
Element	Value	Address
-------	-----	--------------
0		T		0x7ffe44b24302
1		i		0x7ffe44b24303
2		m		0x7ffe44b24304
3				0x7ffe44b24305
4				0x7ffe44b24306
5				0x7ffe44b24307

Using the array name, the string is Tim

***Print just the String using pointers***
Array
Element	Value	Address
-------	-----	--------------
0		T		0x7ffe44b24302
1		i		0x7ffe44b24303
2		m		0x7ffe44b24304

Using a pointer to the start of the array, the string is Tim