954,219 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ arrays

what does the following do?

int array[6] = { 5, 3, 4, 17, 22, 19 };

int* ptr = array[0]
*ptr = -1;
thank you!

dsuh06
Newbie Poster
15 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

The first line initializes the values of the array.
The second line is a syntax error. It should be int* ptr = &array[0];
The last line sets the value of te first array element to -1.

Ancient Dragon
Retired & Loving It
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

wow thank you!

then in addition, what does

ptr+= 2
and ptr[1] =9

do?

thank you!!

dsuh06
Newbie Poster
15 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

>>then in addition, what does
Think about it and post your answer (or guess)

Ancient Dragon
Retired & Loving It
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

okies..

so this is what i thought:

ptr+= 2
increases the pointer by two.. so the ptr will now point to array[2]

and ptr[1] =9

the value of index 1 is equal to 9..

is this correct?

thank you!

dsuh06
Newbie Poster
15 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

yes, but in the original array which element is set to 9?

Ancient Dragon
Retired & Loving It
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

umm.. is it array[1] which is origially 3 set to 9?

dsuh06
Newbie Poster
15 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

nope -- try again ?

Ancient Dragon
Retired & Loving It
Team Colleague
30,046 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,342
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You