for example
double t[]={0.12,0.3,7.0,4,4};

int t[]={1,2,3,4}

i wanna ask what "t+1"stand for respectively.if t is the first adress of a array,then t+1 will get to another physical adress right?a back to back address?thanks.

Recommended Answers

All 2 Replies

t + 1 moves t over one position, based on the width of the datatype t is addressing.

For example, if t is a pointer to doubles (which usually take eight bytes of memory), t + 1 will return a memory address eight bytes away from t. If t is a pointer to ints (which usually take four bytes of memory), t + 1 will return a memory address four bytes over. This means that (t + 1) is the next element of the array.

So, when you dereference t, t+1, and friends, here's what you get:

double t[] = {10.0, 20.0, 30.0, 40.0, 50.0};
double a = *t;
double b = *(t + 1);
double c = *(t + 2);
double d = *(t + 4);

Now 'a' is equal to 10.0, 'b' is equal to 20.0, c is equal to 30.0, and d is equal to 50.0.

The idiom, *(t + n) is so useful that there is special syntax for it: t[n]. Array subscripting is just a synonym for adding to a pointer and dereferencing. (C++ allows this operation to be overridden to mean other things, though.)

a back to back address?

Yes, and since the items in an array are stored back to back, this is useful.

thanks for the detailed explanation.
that's really help.thank you rashakil fol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.