• C

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
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.