9 Replies - 921 Views - Last Post: 23 April 2009 - 10:22 AM Rate Topic: -----

#1 zmikeb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-March 08

what's wrong with my pointer program?

Posted 09 March 2008 - 12:50 PM

I wrote this program that points to an array, and changes the value of each element through the use of a for loop.

#include <iostream>
using namespace std;

int main()
{
	char array[5];
	char* pointer;
	pointer = array;

	for( int i = 0; i < 5; i++ )
	{
		*pointer = 3;
		pointer++;
	}
	for( int i = 0; i < 5; i++ )
	{
		cout << array[i];
	}

	return 0;
}


The problem is that when I debug this, it outputs strange characters that curiously resemble hearts...........
I have no clue what's wrong with this code...Please help

Edit: By the way, I am using Visual Studio Express 2008

This post has been edited by zmikeb: 09 March 2008 - 12:50 PM


Is This A Good Question/Topic? 0
  • +

Replies To: what's wrong with my pointer program?

#2 rahaitprog  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-March 08

Re: what's wrong with my pointer program?

Posted 09 March 2008 - 01:01 PM

you have declared the pointer..
but not given the address..

pointer=&array
this will do it..
previously it was pointing to any arbitrary location..
Was This Post Helpful? 0
  • +
  • -

#3 zmikeb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-March 08

Re: what's wrong with my pointer program?

Posted 09 March 2008 - 01:08 PM

View Postrahaitprog, on 9 Mar, 2008 - 01:01 PM, said:

you have declared the pointer..
but not given the address..

pointer=&array
this will do it..
previously it was pointing to any arbitrary location..


this does not do the trick. I modified the code and the compiler generated the following error: .\main.cpp(8) : error C2440: '=' : cannot convert from 'char (*)[5]' to 'char *'
Was This Post Helpful? 0
  • +
  • -

#4 rahaitprog  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-March 08

Re: what's wrong with my pointer program?

Posted 09 March 2008 - 01:26 PM

pointer=&array[0];
then inside the loop..
array[i]=3;
declare the main as void..
comment the return statement..because then two extra arguments have to passed then..
Was This Post Helpful? 0
  • +
  • -

#5 zmikeb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-March 08

Re: what's wrong with my pointer program?

Posted 09 March 2008 - 01:44 PM

View Postrahaitprog, on 9 Mar, 2008 - 01:26 PM, said:

pointer=&array[0];
then inside the loop..
array[i]=3;
declare the main as void..
comment the return statement..because then two extra arguments have to passed then..


This changes the whole program though. I want to change the array values through the use of the pointer, not directly through the array....
Was This Post Helpful? 0
  • +
  • -

#6 zmikeb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-March 08

Re: what's wrong with my pointer program?

Posted 09 March 2008 - 03:19 PM

Please I really need help with this.........
Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,341
  • Joined: 20-August 07

Re: what's wrong with my pointer program?

Posted 09 March 2008 - 03:26 PM

Everything that rahaitprog has told you is completely wrong I'm afraid, please disregard everything suggested in the thread so far, it'll only send you in the wrong direction.

The reason you're seeing those characters, is because you're assigning the integral value 3 to your char objects.

if you'd like to see the character 3 appear on the screen, then you could do one of two things.

Either change the first loop to assign character '3' (add single quotes around it)
    for( int i = 0; i < 5; i++ )
    {
        *pointer = '3';
        pointer++;
    } 


Or use a cast, to cause cout to use the integer representation, not the character representation
    for( int i = 0; i < 5; i++ )
    {
        cout << static_cast<int> (array[i]);
    } 

This post has been edited by Bench: 09 March 2008 - 03:31 PM

Was This Post Helpful? 0
  • +
  • -

#8 zmikeb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 01-March 08

Re: what's wrong with my pointer program?

Posted 09 March 2008 - 03:34 PM

View PostBench, on 9 Mar, 2008 - 03:26 PM, said:

Everything that rahaitprog has told you is completely wrong I'm afraid, please disregard everything suggested in the thread so far, it'll only send you in the wrong direction.

The reason you're seeing those characters, is because you're assigning the integral value 3 to your char objects.

if you'd like to see the character 3 appear on the screen, then you could do one of two things.

Either change the first loop to assign character '3' (add single quotes around it)
    for( int i = 0; i < 5; i++ )
    {
        *pointer = '3';
        pointer++;
    } 


Or use a cast, to cause cout to use the integer representation, not the character representation
    for( int i = 0; i < 5; i++ )
    {
        cout << static_cast<int> (array[i]);
    } 


Oh my god. I'm kicking myself in the ass for this. I am soo stupid. Wow, no wonder........You know, I spent about 3 hours just to find out it was a simple error regarding quotes. How could i ever thank you?
Did you notice this right away?

This post has been edited by zmikeb: 09 March 2008 - 03:35 PM

Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: what's wrong with my pointer program?

Posted 09 March 2008 - 05:27 PM

Quote

Everything that rahaitprog has told you is completely wrong I'm afraid, please disregard everything suggested in the thread so far, it'll only send you in the wrong direction.


lol, as I scrolled though the post I was really hoping that someone caught this.

rahaitprog,

Your intentions were in the right place, but you need to brush up on your pointers and arrays. The variable 'array' actually holds the address of the first element of the array -- so pointer = array; is all that was needed. This is the same as pointer = &array[0];.
Was This Post Helpful? 0
  • +
  • -

#10 itengineer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 23-April 09

Re: what's wrong with my pointer program?

Posted 23 April 2009 - 10:22 AM

[quote name='zmikeb' date='9 Mar, 2008 - 11:50 AM' post='322922']
I wrote this program that points to an array, and changes the value of each element through the use of a for loop.

#include <iostream>
using namespace std;

int main()
{
	char array[5];
	char* pointer;
	pointer = array;

	for( int i = 0; i < 5; i++ )
	{
		*pointer = 3;
		pointer++;
	}
	for( int i = 0; i < 5; i++ )
	{
		cout << array[i];
	}

	return 0;
}


the problem is that you are assingning 3 number which will be interpreted as ascii value and thats why you are getting heart symbol displayed because it's ascii value is 3........if you want to display 3 on the screen write..........

*pointer = '3';

this will work...........best of luck.....Enjoy pointers

Regards
ITENGINEER
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1