Thread: pointers

  1. #1

    Question pointers

    How would I allocate a string? example:

    char * myvar;
    char * myvar2;


    I need to allocate myvar so that if I change myvar2, myvar won't change, even though its pointing to it.

    Thanks for anyhelp.

  2. #2
    junior member mix0matt's Avatar
    Join Date
    Aug 2001
    you question doesn't make much sense....really...

    the statement: myvar = myvar2 assigns the value in myvar2, which is an address of a memory location to the pointer variable myvar. this causes both pointer to point to the same address. myvar2 does not point to myvar.

    char** myvar2 = &myvar; //now myvar2 points to myvar.

    i really can't understand what your trying to do. i need more explination.

  3. #3
    Registered User
    Join Date
    Nov 2001
    use strcpy to allocate a string from another;

    char *str1 = "something";
    char *str2;

    int size = strlen(str1) + 1;
    str2 = (char*) malloc(size);
    strcpy(str2, str1);

    By the way, do you want to mean that changing myvar is changing the content of the string or the pointer?
    The experimenter who does not know what he is looking for will not understand what he finds.
    - Claude Bernard

  4. #4
    Registered User
    Join Date
    Sep 2001
    Yes, it causes them to point to the same adress. myvar will always equal myvar2. If myvar2 changes, then myvar will change to whatever myvar2 is changed to. I needed to correct this, whilst using a pointer for the char. mix0matt i think gave me a possibly explination on how to cprrect this.

    thank you both for your help. (btw, name changed to Strut)

    I lay on my bed watching the stars, and I thought to myself... Where the hell is my roof?

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. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of
    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