Pointer Question (resumed)

See: http://www.cplusplus.com/forum/general/83165/

I had some troubles in this time so I couldn't reply at all. But I want to thank to those ones that replied.

1) It is not an homework. I do programs only for fun... so when I decide to do a thing, sometimes, I do it in order to learn more. (I am not a programmer nor I study programming. I do it only becouse I like it and I like to improve my knowledge).

So I repeat my question...

assuming I have declared "int val = 4".....

My question is....

char *p += val;

is correct? I will obtain, as desired, the same result as

char *P += 4 ???
I think that the compiler shall issue an error because there is no such syntax of declaring a variable. You may not use the compound assignment in a variable declaration.
Yeah you are right. I wrongly re-write the code.
The right code is the one reported here: http://www.cplusplus.com/forum/general/83165/

Here I tried to clarify better the point of the question. However, in order to avoid misunderstandings, I will re-write "right" sourcecode

1
2
3
4
char array [10] = {0,1,2,3,4,5,6,7,8,9};
int value = 4;
char * p = &array;
p += value;


Is possible to do such thing to go at array[4]?

-----------

Then... I will not report the other question in that discussion, but if someone can reply it I will appreciate it.

I will repeat the link one last time: http://www.cplusplus.com/forum/general/83165/
Again this code shall not be compiled.:)

char * and char ( * )[10] (that is the type of &array) are different types.

The correct code will be

char *p = array;
p += value;

Now p points to the fifth element of the array that is to the element with index 4.

Take into account that you can access this element the following way (without incrementing p by value)

std::cout << p[value] << std::endl;

or

std::cout << value[p] << std::endl;

Of course it is better do not use the second variant.:)

And one more records

std::cout << *( p + value ) << std::endl;
std::cout << *( p += value ) << std::endl;
Last edited on
Yeah you are right. I've mantained the error I wrote while typing code example xD.

However thank for clarifing my doubt :)

Any suggestion about "recursive search" question in the topic I linked? (thank again)
Topic archived. No new replies allowed.