• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

what is a+j

note: I change i to j because i is italic in ee

#include <stdio.h>
#include <string.h>

main()
{
//int A[];
int A[]={2,4,5,8,1};
printf("%d\n",A);
printf("%d\n",&A[0]);
printf("%d\n",A[0]);
printf("%d\n",*A);
//int *p = A;
int j;
for(j = 0;j<5;j++)
{
    printf("variable a[j]: address=%d value=%d\n",&A[j],A[j]);
    //printf("variable a:::: address=%d value=%d\n",&A,*A);
    printf("variable a+j:: address=%d value=%d\n",&A+j,*(A+j));
}
}

Open in new window



output
-1524265408
-1524265408
2
2
variable a[j]: address=-1524265408 value=2
variable a+j:: address=-1524265408 value=2
variable a[j]: address=-1524265404 value=4
variable a+j:: address=-1524265388 value=4
variable a[j]: address=-1524265400 value=5
variable a+j:: address=-1524265368 value=5
variable a[j]: address=-1524265396 value=8
variable a+j:: address=-1524265348 value=8
variable a[j]: address=-1524265392 value=1
variable a+j:: address=-1524265328 value=1

Open in new window


what is a 1

address =&A[j] = (A+j)
value= A[j] = *(A+j)


what is A+J
0
rgb192
Asked:
rgb192
  • 5
  • 3
  • 2
  • +2
8 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Rgb,

C allows you to add integers to pointers.  At first glance, you might expect the result to be the original address plus the integer value.  But C actually increments the pointer by the length of the item that is the target of the pointer so the new value points to another item of the same type.

float *F;
int  *I;
char *C;

If you add 1 to any of the pointers F, I, or C, the result will be the next float, int, or character.
0
 
ozoCommented:
if A is a pointer and j is  int
(A+j) == &(A[j]) == &(j[A])
0
 
rgb192Author Commented:
If you add 1 to any of the pointers F, I, or C, the result will be the next float, int, or character.
could you give example please

(A+j) == &(A[j]) == &(j[A])
i do not understand  &(j[A])
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ozoCommented:
&(j[A])  == &(A[j])
(A+j) == (j+A)
0
 
n2fcCommented:
You need to check your precedence of operators:
http://en.cppreference.com/w/c/language/operator_precedence

In line  18 of your code:
printf("variable a+j:: address=%d value=%d\n",&A+j,*(A+j));

Try it THIS way, instead:
printf("variable a+j:: address=%d value=%d\n",&(A+j),*(A+j));

This is why (after the first iteration) you show DIFFERENT addresses in each pair!
"&" binds tighter than "+"

A+j is identical to A[j] and is "the jth element in array A"
0
 
ozoCommented:
*(A+j) is identical to A[j]
0
 
n2fcCommented:
BUT...
&A+j   is NOT identical to &(A+j)     !!!
0
 
ozoCommented:
Did someone claim that it was?
0
 
rgb192Author Commented:
&(j[A])  == &(A[j])
(A+j) == (j+A)

what is j[A]

is j an array or list?
is A an array or list?
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi rgb,

To get a good handle on this, think past the syntax.  C doesn't support lists, and arrays are merely extensions of pointers.

  int  A[100];

To the programmer, A is an array with 100 integers in it.  To the C language (and compiler) A is an address where 100 integers can be stored.  There's a big distinction.

a[10] and *(A+10) return the value in the 11th location in the array.  (11th because references are relative 0.)

a[10] is syntax common to most languages that support arrays.

*(A+10) interprets as "Using the location of the item A, advance 10 places, then return the value at that location".

Either syntax will likely result in the exact same machine instructions, depending on optimization.


Kent
0
 
ozoCommented:
what is j[A]
j[A] is A[j]


is j an array or list?
is A an array or list?
What is the difference between what you think of as an "array" and what you think of as a "list"?
0
 
rgb192Author Commented:
Thanks.

i did not fully understand your answers so just need to trust and move on to a debugger local window which is my next question

http://www.experts-exchange.com/Programming/Languages/C/Q_28499114.html
0
 
phoffricCommented:
>> j[A] is A[j]
It is rare to see j[A]. A[j] is the norm.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now