Pointer Problem

This is a discussion on Pointer Problem within the C++ Programming forums, part of the General Programming Boards category; I'm having this problem in solving the transfer of alphabet into pointer. Below is my code: Code: #include <iostream> using ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    9

    Pointer Problem

    I'm having this problem in solving the transfer of alphabet into pointer.
    Below is my code:

    Code:
    #include <iostream>
    using namespace std;
    
    void main()
    {
    	char* ReadChain;
    	
    	*ReadChain = 'A';
    	*(ReadChain + 1) = 'B';
    	*(ReadChain + 2) = 'C';
    
            cout<<"Result = "<<ReadChain<<endl;
    }
    I know it's easier to use array and cstring but I purposely write it in pointer form, which will be incorporated into my serial communication code later.

    Any help and suggestion is well appreciated. Thanks!

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,800
    1. It's int main not void main

    2. You need to allocate memory for the pointer, for example
    Code:
    char * ReadChain = new char[3];
    //Code here
    delete ReadChain;
    Woop?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    9
    I've just tried to do what you have told me but the result is "ABC" and there is some unknown symbol behind it. How to eradicate this unwanted symbol?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,800
    Post the latest code.
    Woop?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    You must allocate an extra character and append a null character to properly terminate such a string. Outputting a C-style string (null terminated character array) writes characters until such time as a null is encountered. If you don't take that into account you get odd results.
    I used to be an adventurer like you... then I took an arrow to the knee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21