Thread: Problems with a pointer

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    4

    Question Problems with a pointer

    Hi there,
    I am starting out with the C++ (like everyone else) and I am experimenting trying to become a little more familiar with it.

    Right now I am playing with pointers. From what I understand, a pointer can be used to refer to a variable in memory by either the data or memory location.

    What I am trying to do is assign a pointer to a string and read the information back through the pointer. Is this even possible?

    Here is my code..... Any input would be greatly appreciated

    Code:
    #include <iostream>
    
    using namespace std;
    
    char MyString[15];
    int Some_Function();
    char *pointer;
    
    int main(int argc, char *argv[])
    {
      Some_Function();
      
      cout << endl;
      system("PAUSE");	
      return 0;
    }
    
    int Some_Function()
    {
        pointer = &MyString[15];
        char MyString[15] = "This is a test";
        
        cout << "Original string: " << MyString << endl;
        cout << "String from pointer: " << *pointer;
        
        return 0;
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Your cod eis a bit weird. First you have a global and a local variable named the same. Though not wrong, it can be confusing. Second, when you make the point point at the string you point at element 15 which is out of bounds. Point at elemetn 0 instead.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Along with what Magos had to say...an array is a pointer as well. So when you make the assignement, you don't need to take the address of myString.

    Code:
    pointer = &MyString[15]; 
    //this is wrong as Magos pointed out
    
    pointer = &MyString; 
    //this would also be wrong.  your taking the address of a pointer
    //so pointer would have to be a char **
    
    pointer = myString;
    //pointer is now pointing to the same memory block as MyString
    That last one is the one you want. You won't have to dereference pointer later on.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    Cool thanks.

    I think that solved a lot of confusion for me.

    So, when I use a pointer for a char array I can put and remove data from it by using the pointer as opposed to playing with the array itself? Hope Im on the right track.

    I redid my function to:
    Code:
    int Some_Function()
    {
        pointer = MyString;
        pointer = "This is a test";
        
        cout << "String from pointer: " << pointer << endl;
        
        return 0;
    }

    Now. If I take this one step further.....
    I want to have the function itself return my string.

    I tried something like this, but its not working for me:
    Code:
    char Some_Function()
    {
        pointer = MyString;
        pointer = "This is a test";
        return pointer;
    }
    Thanks again in advance.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    2
    im not too sure but i guess ur prob is with the return type.. ur function's return type is char.. while ur trying to return a char * .. try changing tht n see if it works.. coz i dont see why else it shudnt work.. im assuming ur Mystring variable is still globally defined.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    pointer = "This is a test";
    This line repoints pointer to the string, it does not copy it into the array. You have to use strcpy. Or use std::string and you don't have to worry about char pointers at all.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    Thanks for all the input.
    I have this now as my code and it seems to do what I was looking for.

    Code:
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    char MyString[15];
    char * Some_Function();
    
    int main(int argc, char *argv[])
    {
      cout << Some_Function() << endl;
      
      system("PAUSE");	
      return 0;
    }
    
    char * Some_Function()
    {
        strcpy(MyString, "Hello world");
        return MyString;
    }

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    One thing you should do instead:
    Code:
    #include <string>
    //(instead of)
    //#include <string.h>
    Generally the standard headers don't have the .h extension...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by JaWiB
    One thing you should do instead:
    Code:
    #include <string>
    //(instead of)
    //#include <string.h>
    Generally the standard headers don't have the .h extension...
    You mean:
    Code:
    #include <cstring>
    //(instead of)
    //#include <string.h>
    Since <string> is different than <string.h>/<cstring>.

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Heh right ...Once again you caught me not thinking
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    Thanks again :-D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM