Pointers Example19

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

9/27/22, 12:21 AM www.josephboutros.org/ecen210/classroom/pointers_example1.

/*

Last modified: 6 September 2022

Created: 4 September 2022

*/

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main()

int age;// 4 bytes booked in the memory for this integer

double weight;// 8 bytes in memory for the double-precision floating-point number

int *ptr1;// a pointer to age

double* ptr2;// a pointer to weight

int x[10];// static array of size 10 integers ( a total of 40 bytes)

// x has type int*

// Indeed "type *varname" is the same as "type varname[]"

// The address of x[0] is x which is also x+0

// The address of x[1] is x+1

// The address of x[2] is x+2

age=20;

printf("age=%d address of age = %lld \n", age, (long long)(&age));

/* The address is the address of the first byte in the variable age */

ptr1=&age;

printf("age=%d address of age (from ptr1) = %lld \n",

age, (long long)ptr1);

weight=65.5;

ptr2=&weight;

printf("weight=%1.1f address of weight (from ptr2) = %lld \n",

weight, (long long)ptr2);

printf("age (from ptr1) is %d \n", *ptr1);

printf("weight (from ptr2) is %1.1f \n", *ptr2);

*ptr1=2*(*ptr1)+1;

printf("new age (from ptr1) is %d \n", *ptr1);

(*ptr2) +=1.0;

printf("new weight (from ptr2) is %1.1f \n", *ptr2);

for(int i=0; i<10; i++) x[i]=i*i;


printf("x[3]=%d %d \n", x[3], *(x+3));

return(0);

}/* end of main() */

www.josephboutros.org/ecen210/classroom/pointers_example1.c 1/1

You might also like