I Hate Pointers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: I Hate Pointers

  1. #1
    Join Date
    Mar 2009
    Posts
    13

    I Hate Pointers

    I don't understand how pointers work, I think the trouble I am running into is using chars for the variables. In any case I am trying to move a few variables out of one function ReverseIt() to another function Write() in any case I am running into a million errors.


    Header:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    //Prototypes pointing to multiple functions
    void Obj_Screen();
    //must pass Reverse from ReverseIt and to Driver
    void ReverseIt(char *Reverse, char *Name);
    void Write(char Reverse, char Name);
    Driver:
    Code:
    #include <iostream>
    #include <string>
    #include "Prototypes.h"
    using namespace std;
    
    int main()
    {
    	char Reverse[50], Name[50];
       	Obj_Screen();
    	//recieve here
    	ReverseIt(&Reverse, &Name);
    	Write(Reverse, Name);
    }
    Functions:
    Code:
    #include <iostream>
    #include <string>
    #include "Prototypes.h"
    using namespace std;
    
    void Obj_Screen()
    {
    		cout << "This Program will completly reverse the words you input into it.";
    	cout << "Why don't you try typing your name in first! \n\n";
    	system("pause");
    	system("cls");
    }
    
    void ReverseIt(char *Reverse, char *Name)
    {
    	
    	int i;
    	//pointer reated here, not sure if thats correct.
    	getline(cin, *Name);
    	
    for (i=*Name.size(); i>0; --i)
    	{
    		//need to bring this to main()	
    		*Reverse=*Reverse+*Name[i];
    	}
    }
     void Write(char Reverse, char Name)
     {
    	 cout << "Your name in Reverse is "<<Resverse <<"!";
    
     }

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: I Hate Pointers

    Code:
    for (i=*Name.size(); i>0; --i)
    A char* is not the same as a string. A char* doesn't has a size function. A char* is a pointer to a array of characters, nothing more. Use strlen to get the length of your text.

    Code:
    *Reverse=*Reverse+*Name[i];
    'Reverse' points to the first character, and you are taking to content of that character. It should look something like this.
    Code:
    Reverse[j]=Reverse[j]+Name[i];
    j++;
    Code:
     void Write(char Reverse, char Name)
    The 2 vars are char, not char*... also, you aren't use 'name' at all, so why put it there at all.
    Last edited by Skizmo; April 2nd, 2009 at 06:07 PM.

  3. #3
    Join Date
    Mar 2009
    Posts
    13

    Re: I Hate Pointers

    A char* is not the same as a string. A char* doesn't has a size function. A char* is a pointer to a array of characters, nothing more. Use strlen to get the length of your text.
    really, will the loop in the function still work if I change the variables to Strings instead of arrays?


    'Reverse' points to the first character, and you are taking to content of that character. It should look something like this.

    can I have two variables in a loop? how would that look?
    Last edited by ArmlessBastard; April 2nd, 2009 at 06:26 PM.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,444

    Re: I Hate Pointers

    Quote Originally Posted by ArmlessBastard View Post
    really, will the loop in the function still work if I change the variables to Strings instead of arrays?
    Your program already includes the header for std::string, namely <string>. So why are you resorting to using char*? Why not just use std::string in your entire program?

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center