Thread: Needing assistance with pointers

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    Needing assistance with pointers

    Hey, I'm trying to break in to my OWN computer so I can make it more secure and develop skills but I am getting confused with pointers...
    Code:
    #include <iostream>
    #include <windows.h>
    #define cmd.exe "C;\windows\system32\cmd.exe"
    using namespace std;
    int main()
    {
        char *Input[128];
        char Command[128];
        Input=&Command;
        cin>>Command;
        cout<<*Input;
        Command="MSG.exe * Hi";
        system("cmd.exe"+" "+Command);
    
    
    }
    Is there also any other way I can substitute system() for winexec()?

  2. #2
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33
    i don't know abt the later half of ur code , but u can improve the first half quite a lot ..
    Code:
    #include <iostream.h>
    #include <stdio.h>
    using namespace std;
    int main(){
        char Command[128];
        cin.get(Command,128);
        cout<<Command;
        return 0;
    }

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    You don't need to use pointer there. Your program will not even compile. You should do it this way:
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    int main()
    {
    	char input[128];
    	cout<<"Enter command> ";
    	cin>>input;
    	system(input);
    	return 0;
    }
    Last edited by siavoshkc; 03-01-2006 at 10:32 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    agarwaga, it's great that you know about the std namespace, but it really doesn't help to use it if you aren't using libraries that are defined under that namespace. <iostream.h> should be <iostream>, <stdio.h> should be <cstdio>, and siavoshkc, <stdlib.h> should be <cstdlib>
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33
    i am returning back to C++ after quite some time ...

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    i want to use pointers though, yes their is an easier way of doing it, but i am not challenged from that

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    With pointer:
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    int main()
    {
             char input[128];
             char* ptr=&input[0];
             cout<<"Enter command> ";
             cin>>ptr;
             system(ptr);
             return 0;
    }
    Or better:
    Code:
    int main()
    {
              char *ptr = new char[128];
              cout<<"Enter command> ";
              cin>>ptr;
              system(ptr);
              return 0;
    }
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by siavoshkc
    With pointer:
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    int main()
    {
             char input[128];
             char* ptr=&input[0];
             cout<<"Enter command> ";
             cin>>ptr;
             system(ptr);
             return 0;
    }
    Or better:
    Code:
    int main()
    {
              char *ptr = new char[128];
              cout<<"Enter command> ";
              cin>>ptr;
              system(ptr);
              return 0;
    }
    better still
    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    int main()
    {
             string input;
             cout<<"Enter command> ";
             cin>>input;
             system(input.c_str());
             return 0;
    }
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Try using pointers, ChaosEngine.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #10
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by siavoshkc
    Try using pointers, ChaosEngine.
    No. Pointers are completely unneccessary for this task. Anytime you can avoid them, you should. C++ provides several features (references, auto_ptr, vector, string) to avoid using pointers, which are notoriously error-prone.

    Learn to use them.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Yes, but starter of the thread wants to practice pointers I think. Look at the posts before sending new one please.
    i want to use pointers though, yes their is an easier way of doing it, but i am not challenged from that
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I believe there are better ways to learn pointers than the problem given For example, the OP could learn pointers by creating his own linked list (something which inevitably all programmers end up doing at least once in their lives)

  13. #13
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I agree. pointer should be used in its own place, minimum required.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by bikr692002
    Hey, I'm trying to break in to my OWN computer so I can make it more secure and develop skills but I am getting confused with pointers...
    Code:
    #include <iostream>
    #include <windows.h>
    #define cmd.exe "C;\windows\system32\cmd.exe" // improper format 1
    using namespace std;
    int main()
    {
        char *Input[128];  // problem #2
        char Command[128];
        Input=&Command; 
        cin>>Command;
        cout<<*Input;
        Command="MSG.exe * Hi";
        system("cmd.exe"+" "+Command);  // problem #3
    
    
    }
    Is there also any other way I can substitute system() for winexec()?
    #1
    #define cmd.exe "C;\windows\system32\cmd.exe"
    the . is illegal, cmd is not a structure
    the ; should be :
    each \ should be \\

    #2
    char *Input[128];
    why create 128 pointers? simply
    char *Input;
    should suffice.

    #3
    system("cmd.exe"+" "+Command);
    character arrays cannot be added. Use strcat() to combine them then call the function.

    Other errors can be fixed after these, unless everyone else has pulled you away from fixing your original project.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  15. #15
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    There are more errors than those.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM