473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How does the compiler calculates the address of the last element in the below array?

Say I have an array of a[5] , and base address is 3000 now , and I have to get the address of the last element a[4] and I have a pointer b , and I do
b=a+n-1 , so I have one confusion in this that first a+n would be executed like
a+n*sizeof(a) -1 ,now a+n*sizeof(a)= 3010 ,now when we do -1 from it , how does it evaluate to 3008 , the address of last element since contiguous memory cells are from address 3000 to 3008 so then how come 3010-1 evaluates to 3008 ,how does compiler come to know how much bytes it has to move backwards since 3010 is a random location .
Sep 16 '15 #1
6 1568
weaknessforcats
9,208 Expert Mod 8TB
All you need do is:

Expand|Select|Wrap|Line Numbers
  1. b = a+4;
In doing pointer arithmetic, the compiler will add 4 times the sizeof the array type. You don't have to worry about that.

In your example, if n is number of elements in the array, and b is a pointer to the array type, then you would:

Expand|Select|Wrap|Line Numbers
  1. b =  a + n - 1;
  2.  
You might also read: http://bytes.com/topic/c/insights/77...rrays-revealed
Sep 16 '15 #2
Sorry but couldn't get clear with this concept that how 3010 -1 =3008 when that is some random location in the memory as array is contiguously located from 3000 to 3008 only .
As u said that compiler will add 4 times the sizeof the array type but no of elements in the array are 5 and not 4 so then ?

Sorry but seriously I am stucked at this point .
Sep 17 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
The first element of an array is element 0. The name of the array is the address of that element. Therefore, that address plus 4 is the address of the fifth element,which is what you asked about.

Did you read the article?
Sep 17 '15 #4
Yes , I read and agree to your explanation but what I am saying ,plz see it once, I am confused in that point.
Sep 17 '15 #5
Since b=a+n-1 =3010-1=3008 , how's that evaluating to 3008 when 3010 is a random location.
Sep 17 '15 #6
donbock
2,426 Expert 2GB
Let's be very specific. Let's assume for a moment that data pointers can be cast to integer type unsigned long.
Expand|Select|Wrap|Line Numbers
  1. #define N 5
  2. int main()
  3. {
  4.   int a[N];
  5.   int *b;
  6.   unsigned long p;
  7.  
  8.   b = a;
  9.   p = (unsigned long)a;
  10.   printf("1a. %p\n", b);
  11.   printf("1b. %ld\n", p);
  12.  
  13.   b = a + N;
  14.   p = (unsigned long)a + N*sizeof(int);
  15.   printf("2a. %p\n", b);
  16.   printf("2b. %ld\n", p);
  17.  
  18.   b = a + N - 1;
  19.   p = (unsigned long)a + (N-1)*sizeof(int);
  20.   printf("3a. %p\n", b);
  21.   printf("2b. %ld\n", p);
  22. }
If sizeof(int) is 4, and if array a happens to start at address 3000, then I expect the output to be:
Expand|Select|Wrap|Line Numbers
  1. 1a. 3000
  2. 1b. 3000
  3. 2a. 3020
  4. 2b. 3020
  5. 3a. 3016
  6. 3b. 3016
Pointer arithmetic involves an arbitrary but well-defined pointer value and an arbitrary but well-defined integer expression. Nothing is random.

Pointer arithmetic works as if it were implemented in accordance with the p expressions above.
Sep 17 '15 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
7
by: ashu | last post by:
look at code #include<stdio.h> int *mult(void); int main(void) { int *ptr,i; ptr=mult; for(i=0;i<6;i++) { printf("%d",*(ptr++));
5
by: junky_fellow | last post by:
Hi, I discussed about this earlier as well but I never got any satisfactory answer. So, I am initiating this again. Page 84, WG14/N869 "If both the pointer operand and the result point to...
11
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the...
11
by: desktop | last post by:
How do I find the last element in the list numsx defined below? int* myfind(int* arr_start, int* arr_end, int& s) { int not_found = 666; int* result = &not_found; while (arr_start !=...
16
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
3
by: jerrygarciuh | last post by:
Hi folks, As part of an app I am validating and repopulating user input from check boxes. $responses is posted checkbox input, $answers is the data used to create and recreate the checkboxes. ...
1
by: umarsulur | last post by:
Undefined index for the last element of array while fetching datas from db through an array variable. I use for each loop to seperate and also in_array()...kindly help pls..... Here is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.