REGARDING POINTERS AND MATH ----- ( TUTORIAL 41 )

0 saivivek reddy · May 24, 2015
Instead of giving 5 values in the array if some other number of values are given then the addresses are not shown as it was in the video . some one help me with this issue . 

Post a Reply

Replies

Oldest  Newest  Rating
0 Laura Lee · May 24, 2015
I have not seen the video and your question is ambiguous. I assume you're referring to pointer arithmetic and accessing values from an array. 

int arr[10] = {0};
//An array is a static pointer who's position in memory
// should not be changed.

int* parr = arr;
//parr points to the address arr points to


arr[0] = 2;
// Arbitrary value chosen for the first element of arr

printf("%i\n",*parr);//Dereferencing parr will give the value at arr[0]:2

++parr;//We've increased the position in memory of parr ////because parr is an int array, the position increased four bytes


arr[1] = 4;
//Arbitrary value


printf("%i\n",*parr);
//Dereferencing parr will give the value at arr[1] :4


parr+=1;
//We can also increase our pointer like this.
//Since parr is an int pointer, increasing by n would be n*sizeof(int);


arr[2] = 8;

printf("%i\n",*parr);
//will print 8


parr+=2;
//2*sizeof(int) == 8 == two int elements


*parr = 32;
//Set arr[4] to 32


printf("%i\n",arr[4]);
//We skipped past arr[3] ; will print 32
  • 1

C++

107,158 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator